Citrix Hypervisor PowerShell – Restart VM

This Citrix Hypervisor PowerShell script restarts a Citrix Hypervisor host and waits for the host to be in the running state before continuing by waiting until the Netlogon service on the VM is running.

function Restart-XenServerVM {
    [string] $Server

    #Power off VM 
    $VM = get-xenvm -name $Server
    Write-host (date -Format hh:mm:ss)   -   Shutdown VM $Server  -ForegroundColor Yellow
    Do {
         # If the VM is not reporting "Halted", then perform Shutdown action
         if ($VM.power_state -ne "Halted") {
            #shut down VM
            Invoke-XenVM -VM $VM -XenAction Shutdown -async
            sleep 10
            $VM = get-xenvm -name $Server
    } while ($VM.power_state -ne "Halted") #Loop until VM reports "Halted"

    #Start Mgmt VM
    Write-host (date -Format hh:mm:ss)   -   Starting VM $Server  -ForegroundColor Yellow
    $VM = get-xenvm -name $Server
    Invoke-XenVM -vm $VM -XenAction start

    #Wait for Management VM to boot by checking the Netlogon service of the VM
    do {
        #Write-host (date -Format hh:mm:ss)   -   Waiting for VM $Server to boot -ForegroundColor Yellow
        $VMServices = get-service -ComputerName $Server -name Netlogon -ErrorAction SilentlyContinue
        sleep 5
    } until ($vmservices.status -like "running")}


Leave a Reply

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

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

Google photo

You are commenting using your Google 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 )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.