Guest User

Untitled

a guest
Aug 18th, 2021
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. $servers = @('SVR1','SVR2','SVR3','SVR4')
  2.  
  3. #Get all VM's with Configuration Ver. 8.0
  4. $VMs = Get-VM -ComputerName $servers | Where-Object {$_.Version -eq '8.0'}
  5.  
  6. foreach ($VM in $VMs)
  7. {
  8.     #Get Current VM running state and name
  9.     $state = $VM.State
  10.     $VMName = $VM.Name
  11.  
  12.     try
  13.     {
  14.         #If VM is running, try to stop it
  15.        if ($State -eq 'Running')
  16.        {
  17.            Stop-VM -VM $VM
  18.            Write-Host "Stopping VM $VMName" -BackgroundColor Red
  19.         }
  20.         else
  21.         {
  22.             Write-Host "$VMName is Offline"
  23.         }
  24.     }
  25.     catch
  26.     {
  27.         Write-host "$VMName was unable to be stopped"
  28.     }
  29.  
  30.     $maxRepeat = 20
  31.  
  32.     #Check every 60 seconds to see if the VM has stopped running, or break.
  33.     while ( ($VM.State -eq 'Running') -and ($maxRepeat -ne 0) )
  34.     {
  35.         Write-Host "VM is still running..." -ForegroundColor Red
  36.         Stop-VM -VM $VM
  37.         $maxRepeat--
  38.         Start-Sleep -Seconds 60
  39.     }
  40.     if ($VM.State -eq 'Off')
  41.     {
  42.         try
  43.         {
  44.             Update-VMVersion -Name $VMName -ComputerName $VM.ComputerName -Confirm:$false
  45.             Write-Host "$VMName Configuration has been updated to 9.0, starting VM"
  46.             Start-VM -Name $VMName -ComputerName $VM.ComputerName
  47.         }
  48.         catch
  49.         {
  50.             Write-Host "$VMName Configuration has failed updating" -ForegroundColor Red    
  51.         }
  52.     }
  53. }
Advertisement
Add Comment
Please, Sign In to add comment