-
-
Save phyoewaipaing/d319b0abd77afabf372fdbb590477109 to your computer and use it in GitHub Desktop.
Automatically Power On VMs in PowerCLI
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[CmdletBinding()] | |
param() | |
$PowerOffServers=@() | |
######## test if the backup csv file exists and if not, create a new one with the current powerstate of All VMs##### | |
if(Test-Path ToPowerOnVMs.csv) | |
{ | |
import-csv ToPowerOnVMs.csv | where { $_.PowerState -eq "PoweredOn"}| foreach { | |
try { | |
$vminfo=get-vm $_.Name -Ea stop | |
if ( $vminfo.PowerState -eq "PoweredOff") | |
{ | |
$PowerOffServers+=$vminfo | select Name, PowerState | |
} | |
} | |
catch { | |
Write-Host -Fore Red "Unable to get-vm state for $_.VM" | |
} | |
} | |
if ($PowerOffServers.count) | |
{ | |
Write-Host "The list of Poweroff Servers are" | |
$PowerOffServers | ft Name, PowerState -auto | |
Write-Host -Fore red "$($PowerOffServers.count) VMs are currently Powered Off" | |
[console]::ForeGroundColor="yellow" | |
$PowerOnConfirm=Read-Host "Do you want to start all Powered Off Servers ?(y/n)" | |
[console]::ForeGroundColor="white" | |
if ($PowerOnConfirm -eq 'y') | |
{ | |
$PowerOnServers=0 | |
foreach ($VM in $PowerOffServers) | |
{ $result=Start-VM $VM.Name | |
if ($result.PowerState -eq "PoweredOn"){ $PowerOnServers++ } | |
$Percent=[math]::round(($PowerOnServers/$PowerOffServers.count) * 100) | |
write-progress -id 1 -activity "Powering On VMs" -percentComplete $Percent -CurrentOperation "$Percent% Complete. Powering On $($VM.Name)" | |
} | |
} | |
} | |
else | |
{ | |
$VMsAlreadyPoweredOn=1; | |
Write-Host -fore green "All Necessary VMs have been already Powered On" | |
} | |
############ Re-test if there are any servers left powered off ############ | |
$PowerOffServers=$null ##### reset the value to count the poweroff servers again | |
import-csv ToPowerOnVMs.csv | where { $_.PowerState -eq "PoweredOn"}| foreach { | |
try { | |
$vminfo=get-vm $_.Name -Ea stop | |
if ( $vminfo.PowerState -eq "PoweredOff") | |
{ | |
$PowerOffServers+=$vminfo | select Name, PowerState | |
} | |
} | |
catch { | |
Write-Host -Fore Red "Unable to get-vm state for $_.VM" | |
} | |
} | |
####### Test if the Powered On servers are actually running ######## | |
if ($PowerOffServers.count) | |
{ Write-Host -fore Red "The following Servers cannot be Powered On" } | |
elseif ($VMsAlreadyPoweredOn) | |
{ } | |
else {Write-Host -fore Green "All VMs have been successfully Powered On" } | |
} | |
else | |
{ | |
Write-Host -fore yellow "No VM Powerstate backup file found. Getting the current Powerstate info and saving to file..." | |
$VMs=Get-VM | select Name,Powerstate | |
$VMs | Export-Csv ToPowerOnVMs.csv -NoTypeInformation | |
Get-ChildItem ToPowerOnVMs.csv | select name,Length,@{Name="No. of VMs";Exp={$VMs.count}} | ft -auto | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment