Guest User

Untitled

a guest
Nov 19th, 2017
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.84 KB | None | 0 0
  1. #ESXi server being targeted
  2. $target = Read-Host "Target host"
  3.  
  4. Disconnect-VIServer -Confirm:$false
  5. #$creds | Get-Credential
  6. Connect-VIServer -Server $target -User root -Password PCSuser01.13
  7.  
  8. $vms = Get-VM -Location $target -Name *
  9. #$dStore = Get-Datastore -Name *OS* -Server pcs-lab2h1.cenet.coned.uah.edu
  10.  
  11. ####This is only here is you want it to happen well after the script is started
  12. ####Added the still waiting just for some confirmation that it is running
  13. ####Adjust time or comment out as needed
  14. for($i=1; $i -le 5;$i++)
  15. {
  16. Write-host "Still waiting......"
  17. Start-Sleep -Seconds 3600 -Verbose
  18. }
  19.  
  20. foreach ($vm in $vms)
  21. {
  22. if($vm.PowerState -eq "PoweredOn")
  23. {
  24. Shutdown-VMGuest $vm.Name -Verbose -Confirm:$false
  25. do
  26. {
  27. Start-Sleep -s 5 -Verbose
  28. Write-Host "Sleeping 5"
  29. $vm = Get-VM $vm
  30. }
  31. until($vm.PowerState -eq "PoweredOff")
  32. }
  33. }
  34.  
  35. $targetHost = Get-VMHost -Server $target #problem here or below
  36.  
  37. Set-VMHost -State Maintenance -Confirm $false #-Server ####problem with server name
  38.  
  39. while($targetHost.ConnectionState -ne "Maintenance")
  40. {
  41. Start-Sleep -Seconds 5 -Verbose
  42. $targetHost = Get-VMHost -Server $target -Verbose
  43. Write-Host "Waiting for host to enter maintenance mode"
  44. }
  45.  
  46. Restart-VMHost -Confirm $false -Verbose
  47.  
  48. Start-Sleep -Seconds 500 -Verbose
  49.  
  50. while(!(Test-Connection pcs-esxi1 -Count 1))
  51. {
  52. Start-Sleep -Seconds 5 -Verbose
  53. Write-Host "Waiting for pingback"
  54. }
  55.  
  56. Set-VMHost -State Connected -Verbose
  57.  
  58. while($targetHost.ConnectionState -ne "Connected")
  59. {
  60. Start-Sleep -Seconds 5 -Verbose
  61. $targetHost = Get-VMHost -Server $target
  62. Write-Host "Waiting for server to exit maintenance mode"
  63. }
  64.  
  65. #turn the VMs that were previously on back on
  66. foreach ($vm in $vms)
  67. {
  68. Start-VM $vm.Name -Verbose -Confirm:$false -Verbose
  69. }
Add Comment
Please, Sign In to add comment