Restoring Virtual Machines with VMM using PowerShell

The first steps to restore your virtual machine are identical to the steps desribed in my previous post.

 

Add-PSSnapin Microsoft.SystemCenter.VirtualMachineManager
Get-VMMServer -ComputerName “<ComputerName> | out-null

 

To be able to restore using powershell you need to know your servername and the checkpoint name. The command to restore your checkpoint is ‘get-VMCheckpoint –VM <Server> | where{$_.Name -eq “<CheckPoint name>”} | restore-VMCheckpoint | out-null

The above command will silently restore your virtual machine.

image

You enter the following if you want to wait till your restore is finished before the script continues.

do
{
Write-Host "$ServerName$ServerNumber is restoring to last checkpoint" -foregroundcolor cyan
</span><span style="color: #000000;">$CheckPointStatus = $SelectedVM.MostRecentTask</span><span style="color: #000000;">start-sleep -seconds 3</span>

<span style="color: #000000;">}
until($CheckPointStatus.status -eq "Completed" -and $CheckPointstatus.name -eq "Restore from checkpoint")

Starting Virtual Machines with VMM using PowerShell

You can start, stop and restore virtual machines easily using PowerShell. In the next couple of posts I will start, stop and restore virtual machines using PowerShell.

You can add the VMM snapin with ‘Add-PSSnapin Microsoft.SystemCenter.VirtualMachineManager’ if you dont want to use ‘Windows PowerShell – Virtual Machine Machiner’

Before you can utilize PowerShell with VMM you need to connect to the server. You connect to the server with ‘Get-VMMServer -ComputerName “<ComputerName> | out-null”’

image

You now made connection with the VMM server and you are ready to start the virtual machine. Enter ‘start-VM –VM <ComputerName> | out-null’ to stop the virtual machine.

image

That’s all there is to start your virtual machines. This script is quite simple and there are no special things in this post.

 

foreach($Server in $serverarray)
{
$SelectedVM = get-VM -Name $Server </span><span style="color: #000000;">    #Start VM’s
Start-VM -VM $SelectedVM | out-null </span>

<span style="color: #000000;">    do
{
start-sleep -seconds 3
Write-Host “$server is starting” -foregroundcolor cyan
} </span>

<span style="color: #000000;">    until($selectedVM.status -eq “Running”)
Write-Host “$server turned on successfull” -foregroundcolor green </span>

<span style="color: #000000;">}

 

You can add additional features like an array with all your servers that you want to start, stop or restore.

You probably dont want to start multiple servers at the same time. With a simple do statement you can wait for every server to start properly.

do
{
Write-Host “$server is starting” -foregroundcolor cyan
start-sleep -seconds 3
}
until($selectedVM.status -eq “PowerOff”)