Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- clear
- function Format-Color([hashtable] $Colors = @{}, [switch] $SimpleMatch) {
- $lines = ($input | Out-String) -replace "`r", "" -split "`n"
- foreach($line in $lines) {
- $color = ''
- foreach($pattern in $Colors.Keys){
- if(!$SimpleMatch -and $line -match $pattern) { $color = $Colors[$pattern] }
- elseif ($SimpleMatch -and $line -like $pattern) { $color = $Colors[$pattern] }
- }
- if($color) {
- Write-Host -ForegroundColor $color $line
- } else {
- Write-Host $line
- }
- }
- }
- # Input VM names you want to include in the reboot-cycle, in quotes separated by comma
- $testvms = ""
- $i = 0
- Write-Host "Running looping-restart script on " -Fore White -NoNewline
- write-Host $testvms.Count -Fore Red -NoNewline
- Write-Host " VMs on vCenter " -Fore White -NoNewline
- Write-Host $global:defaultviserver.name -Fore Green
- Write-Host "`n"
- while(1)
- {
- $i = $i+1
- # Adjust the sleeptimer to accomodate your environment.
- # 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.
- $sleeptimer = 20
- $bmsg = ""
- $binfo = ""
- $breakmsg = ""
- if($i % 3 -eq 0) {$bmsg += " - BOOT FAILURE EXPECTED! - "}
- if($i % 3 -eq 0) {$binfo += "(added 10 seconds to reboot-cycle delay)"}
- if($i % 3 -eq 0) {$sleeptimer = 30}
- if($i % 3 -eq 0) {$breakmsg += "Press Ctrl+C to stop the script"}
- Write-Host "Restart #$i" -Fore Cyan -NoNewline
- Write-Host "$bmsg" -Fore Red -NoNewline
- Write-Host "$binfo" -Fore Magenta
- foreach ($vm in $testvms){
- $currenttime = Get-Date -UFormat "%H:%M:%S"
- Restart-VM -VM $vm -RunAsync -Confirm:$false | Out-Null
- Write-Host "[" -Fore Gray -NoNewline
- Write-Host "$currenttime" -Fore White -NoNewline
- Write-Host "]" -Fore Gray -NoNewLine
- Write-Host ": " -Fore White -NoNewline
- Write-Host "Issued restart command to VM " -Fore Yellow -NoNewline
- Write-Host "'$vm'" -Fore Green
- }
- Write-Host "$breakmsg" -Fore Red
- start-sleep -seconds $sleeptimer
- }
Add Comment
Please, Sign In to add comment