$Target="targetmachine"$FilePath="C:\temp\RunningVMs.txt"$Session=New-PSSession-ComputerName$Target#Check if any updatesInvoke-Command-Session$Session-ScriptBlock{$Updates=Start-WUScan#If there are updates then install themif($Updates-ne$null){Install-WUUpdates-Updates$Updates}}#Check if target needs reboot after installing updates$IsPendingReboot=Invoke-Command-Session$Session-ScriptBlock{#$Updates = Start-WUScanGet-WUIsPendingReboot}if($IsPendingReboot-eq$true){write-host"true"}if($IsPendingReboot-eq$true){#If target needs reboot then save running VMs, reboot, start previously running VMsInvoke-Command-Session$Session-ScriptBlock{if(!(Get-ItemC:\temp-eaignore)){mkdirC:\temp}(Get-VM|Where-Object{$_.State-eq"Running"}).Name|Out-File-FilePath$using:FilePathSave-VM-Name(Get-Content-Path$using:FilePath)}#Restart the serverRestart-Computer-ComputerName$Target-Wait-Force#Create new session, and restart previously running VMs$Session=New-PSSession-ComputerName$TargetInvoke-Command-Session$Session-ScriptBlock{Do{Start-Sleep-Seconds30}Until((get-servicevmms).status-eq"Running")Start-VM-Name(Get-Content-Path$using:FilePath)Remove-Item-Path$using:FilePath}}