How to revert virtual machines in your lab

Last week I attented a Microsoft training on Windows 8 (22688A) in a local training center.

I wrote a few lines of code at the beginning of the session to revert a specific VM to its initial state as we had to repeat this task at the end of each lab.

Get-VM -Name 22688A-LON-CL1 |
Get-VMSnapshot | Sort CreationTime |
Out-GridView -PassThru |
Restore-VMSnapshot -Confirm:$false -Passthru -Verbose |
ForEach-Object -Process {
	Start-VM -Name $_.VMName -Verbose
}


When you revert a VM to its initial state, the trust relationship with its domain is broken. The password of the computer needs just to be reset.

I didn’t use the Test-ComputerSecureChannel although it was quickly mentioned at the end of chapter 3.

Test-ComputerSecureChannel -Repair:$true

As we log on the client computer with domain admin credentials (a very bad practice), I just did:

Notice that no reboot is required 😀

Both Test-ComputerSecureChannel and Reset-ComputerMachinePassword exist in Powershell 2.0 but as of version 3.0, you’ve now the ability to specify credentials used to perform the reset. Very handy, isn’t it?

At the end of the week to revert all the virtual machines, I used the following code 😎

Get-VM | Out-GridView -PassThru -Title "Select Virtual Machines" | % {            
    $_ | Get-VMSnapshot | Sort CreationTime |             
    Out-GridView -PassThru -Title "Select one snapshot for $($_.Name)"            
} | Restore-VMSnapshot -Confirm:$false -PassThru -Verbose

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s