Now that you have successfully prepared for the script…here it is. This script will power down the previous clone if there is one, delete the old clone, create a new clone, change the network, and then power on the clone. Please make sure you test this in first in a test environment before deploying to production.

########################################################################
# Clone DC VM and set network to Test Bubble (clonedc.ps1)
#
# This script is used to clone a DC and then set the network port group # to the #Test Bubble group.
# The purpose of this script is to have a replica of our primary DC for # use #when testing an SRM failover. This will remove the previous clone # before #creating a new one.
#
# Created By: Matt Jones, 2014
#
# Variables: $newVMName – Name of the new clone made from the original VM
# $sourceVMName – Name of the target DC VM that will be cloned
# $VM — Name of VM that was cloned earlier
# $DestGroup — This is the port group that the network will be changed to.
#
# Usage: ./clonedc.ps1
#
#######################################################
add-pssnapin VMware.VimAutomation.Core
Connect-viserver (Your vCenter Server)
#Clone Script#
$newVMName = “DCClone”
$sourceVMName = “SourceDC”
Get-VM $newVMName | Stop-VM -Confirm:$false
Start-Sleep -s 5
Get-VM -Name $newVMName | Remove-VM -DeletePermanently -Confirm:$false
Start-Sleep -s 10
New-VM -Name $newVMName -VM $sourceVMName -Location Foldernamecreatedearlier -vmhost hostname
Start-Sleep -s 120
#Set Test Network#
$VM = get-vm “DCClone”
$DestGroup = “Test Bubble”
$VM | get-networkadapter | set-networkadapter -NetworkName $DestGroup -confirm:$false
Start-Sleep -s 30
$VM | Start-VM
taskkill /F /IM powershell.exe
#######################################################
Once you have created the clonedc.ps1 script we need to get it set up as a scheduled task on the physical machine. Log in using the script AD account that you created earlier, then go to Control Panel –> System and Security –> Administrative Tools –> Task Scheduler. Click Create Task.


Under the General Tab Name your new task, change this to Run whether user is logged on or not, and check the box Run with highest privileges. Click the Triggers tab

Click New and then choose the frequency that you would like this to run. Make sure to check the Stop task if it runs longer than box and then set it for 30 minutes. This makes sure that the script stops and doesn’t continually run. Make sure you check the Enabled box or the script won’t run automatically! Click the Actions Tab.

Click New and then from the drop down menu choose Start a program. In the Program/Script box type C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe and in the Add arguments (optional) box type -psc “C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI\vim.psc1” -noe -c “. \”C:\scripts\clonedc.ps1\””. Click the Settings Tab.

If you want to allow this to run on demand then check the Allow task to be run on demand box. Check the Stop the task if it runs longer than box and choose 1 hour from the drop down. Check the box If the running task does not end when requested, force it to stop. Click OK. You will be prompted to enter the credentials for the account running the task and then click OK.


You might have to do a refresh for your new task to appear. I am guessing this might be a bug. Manually run your task and monitor the VIC to make sure it is working properly.

That should be it. Now you can run an SRM test fail over and have a DC available. Make sure that you have removed all of your DCs from SRM if you had them in there.
