Guest User

PowerCLI reboot-cycling script

a guest
Jun 5th, 2018
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. clear
  2.  
  3. function Format-Color([hashtable] $Colors = @{}, [switch] $SimpleMatch) {
  4. $lines = ($input | Out-String) -replace "`r", "" -split "`n"
  5. foreach($line in $lines) {
  6. $color = ''
  7. foreach($pattern in $Colors.Keys){
  8. if(!$SimpleMatch -and $line -match $pattern) { $color = $Colors[$pattern] }
  9. elseif ($SimpleMatch -and $line -like $pattern) { $color = $Colors[$pattern] }
  10. }
  11. if($color) {
  12. Write-Host -ForegroundColor $color $line
  13. } else {
  14. Write-Host $line
  15. }
  16. }
  17. }
  18.  
  19. # Input VM names you want to include in the reboot-cycle, in quotes separated by comma
  20. $testvms =  ""
  21.  
  22. $i = 0
  23.  
  24. Write-Host "Running looping-restart script on " -Fore White -NoNewline
  25. write-Host $testvms.Count -Fore Red -NoNewline
  26. Write-Host " VMs on vCenter " -Fore White -NoNewline
  27. Write-Host $global:defaultviserver.name -Fore Green
  28. Write-Host "`n"
  29.  
  30. while(1)
  31. {
  32.  
  33. $i = $i+1
  34.  
  35. # Adjust the sleeptimer to accomodate your environment.
  36. # 20 seconds is enough for VMs on all-flash storage, but should be set higher for VMs on HDD-based storage or for VMs with long boot times.
  37. $sleeptimer = 20
  38.  
  39. $bmsg = ""
  40. $binfo = ""
  41. $breakmsg = ""
  42.  
  43. if($i % 3 -eq 0) {$bmsg += " - BOOT FAILURE EXPECTED! - "}
  44. if($i % 3 -eq 0) {$binfo += "(added 10 seconds to reboot-cycle delay)"}
  45. if($i % 3 -eq 0) {$sleeptimer = 30}
  46. if($i % 3 -eq 0) {$breakmsg += "Press Ctrl+C to stop the script"}
  47.  
  48. Write-Host "Restart #$i" -Fore Cyan -NoNewline
  49. Write-Host "$bmsg" -Fore Red -NoNewline
  50. Write-Host "$binfo" -Fore Magenta
  51.  
  52. foreach ($vm in $testvms){
  53.     $currenttime = Get-Date -UFormat "%H:%M:%S"
  54.  
  55.     Restart-VM -VM $vm -RunAsync -Confirm:$false | Out-Null
  56.  
  57.     Write-Host "[" -Fore Gray -NoNewline
  58.     Write-Host "$currenttime" -Fore White -NoNewline
  59.     Write-Host "]" -Fore Gray -NoNewLine
  60.     Write-Host ": " -Fore White -NoNewline
  61.     Write-Host "Issued restart command to VM " -Fore Yellow -NoNewline
  62.     Write-Host "'$vm'" -Fore Green
  63.     }
  64.  
  65. Write-Host "$breakmsg" -Fore Red
  66. start-sleep -seconds $sleeptimer
  67. }
Add Comment
Please, Sign In to add comment