Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- $servers = @('SVR1','SVR2','SVR3','SVR4')
- #Get all VM's with Configuration Ver. 8.0
- $VMs = Get-VM -ComputerName $servers | Where-Object {$_.Version -eq '8.0'}
- foreach ($VM in $VMs)
- {
- #Get Current VM running state and name
- $state = $VM.State
- $VMName = $VM.Name
- try
- {
- #If VM is running, try to stop it
- if ($State -eq 'Running')
- {
- Stop-VM -VM $VM
- Write-Host "Stopping VM $VMName" -BackgroundColor Red
- }
- else
- {
- Write-Host "$VMName is Offline"
- }
- }
- catch
- {
- Write-host "$VMName was unable to be stopped"
- }
- $maxRepeat = 20
- #Check every 60 seconds to see if the VM has stopped running, or break.
- while ( ($VM.State -eq 'Running') -and ($maxRepeat -ne 0) )
- {
- Write-Host "VM is still running..." -ForegroundColor Red
- Stop-VM -VM $VM
- $maxRepeat--
- Start-Sleep -Seconds 60
- }
- if ($VM.State -eq 'Off')
- {
- try
- {
- Update-VMVersion -Name $VMName -ComputerName $VM.ComputerName -Confirm:$false
- Write-Host "$VMName Configuration has been updated to 9.0, starting VM"
- Start-VM -Name $VMName -ComputerName $VM.ComputerName
- }
- catch
- {
- Write-Host "$VMName Configuration has failed updating" -ForegroundColor Red
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment