Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #ESXi server being targeted
- $target = Read-Host "Target host"
- Disconnect-VIServer -Confirm:$false
- #$creds | Get-Credential
- Connect-VIServer -Server $target -User root -Password PCSuser01.13
- $vms = Get-VM -Location $target -Name *
- #$dStore = Get-Datastore -Name *OS* -Server pcs-lab2h1.cenet.coned.uah.edu
- ####This is only here is you want it to happen well after the script is started
- ####Added the still waiting just for some confirmation that it is running
- ####Adjust time or comment out as needed
- for($i=1; $i -le 5;$i++)
- {
- Write-host "Still waiting......"
- Start-Sleep -Seconds 3600 -Verbose
- }
- foreach ($vm in $vms)
- {
- if($vm.PowerState -eq "PoweredOn")
- {
- Shutdown-VMGuest $vm.Name -Verbose -Confirm:$false
- do
- {
- Start-Sleep -s 5 -Verbose
- Write-Host "Sleeping 5"
- $vm = Get-VM $vm
- }
- until($vm.PowerState -eq "PoweredOff")
- }
- }
- $targetHost = Get-VMHost -Server $target #problem here or below
- Set-VMHost -State Maintenance -Confirm $false #-Server ####problem with server name
- while($targetHost.ConnectionState -ne "Maintenance")
- {
- Start-Sleep -Seconds 5 -Verbose
- $targetHost = Get-VMHost -Server $target -Verbose
- Write-Host "Waiting for host to enter maintenance mode"
- }
- Restart-VMHost -Confirm $false -Verbose
- Start-Sleep -Seconds 500 -Verbose
- while(!(Test-Connection pcs-esxi1 -Count 1))
- {
- Start-Sleep -Seconds 5 -Verbose
- Write-Host "Waiting for pingback"
- }
- Set-VMHost -State Connected -Verbose
- while($targetHost.ConnectionState -ne "Connected")
- {
- Start-Sleep -Seconds 5 -Verbose
- $targetHost = Get-VMHost -Server $target
- Write-Host "Waiting for server to exit maintenance mode"
- }
- #turn the VMs that were previously on back on
- foreach ($vm in $vms)
- {
- Start-VM $vm.Name -Verbose -Confirm:$false -Verbose
- }
Add Comment
Please, Sign In to add comment