Skip to content

Instantly share code, notes, and snippets.

@coza73
Last active August 29, 2015 14:03
Show Gist options
  • Save coza73/b7c2662d6a1d7bfca0aa to your computer and use it in GitHub Desktop.
Save coza73/b7c2662d6a1d7bfca0aa to your computer and use it in GitHub Desktop.
vSphere power off all running VM's in a cluster
#############################################################################################
# Power off or on all running vm's in a cluster
# Script must be run from same place for power on to work properly
# It writes to file the names of all the powered on VM's, this is to not power on VM's that
# were previously powered off. It then checks for that file for the power on cycle.
#
# http://vspherepowershellscripts.blogspot.com.au/2014/07/power-onoff-all-running-virtual.html?view=classic
#
#################################################################################################
#File Storage Path for list of powered on VM's
$filePath = "c:\temp\"
if ( (Get-PSSnapin -Name VMware.VimAutomation.Core -ErrorAction SilentlyContinue) -eq $null )
{
Add-PsSnapin VMware.VimAutomation.Core
}
# Enter the virtualcenter you wish to connect to
$vCenter = Read-Host "Enter vCenter Name or IP"
Connect-VIServer $vCenter
$clusters = @(Get-Cluster)
# List out all the clusters in virtualcenter for selection
if($clusters.count -gt 1){
for($clusterCount=0;$clusterCount -lt $clusters.count; $clusterCount++){
$optionvalue = $clusterCount + 1
Write-Host $optionvalue ")" $clusters[$clusterCount].Name
}
$input = Read-Host "Select a Cluster"
$selectedCluster = $clusters[$input-1]
}
else{
$selectedCluster = Get-Cluster
}
function storeEnvironment
{#Store the powered VM list to a simple file
#Use Streamwriter for simpler output, just a string name
$PowerFile = New-Object System.IO.StreamWriter($filePath+"MTS_ON.txt")
#Shutdown the powered on VM's
#If vmtools is running send shutdown signal, if no vmtools hard poweroff
$PoweredVMs += Get-Cluster $selectedCluster | Get-VM | where {$_.Powerstate -eq "PoweredON" }
foreach($objVM in $PoweredVMs)
{
$PowerFile.WriteLine($objVM)
$vminfo = get-view -Id $objVM.ID
#Get Vmware tools info
if ($vminfo.config.Tools.ToolsVersion -eq 0)
{
Write-Host "$objVM doesn’t have vmware tools running. Hard power-off" -foregroundcolor red
#Hard power Off
$objVM | Stop-VM -confirm:$false -RunAsync
}
else
{
write-host "$objVM has vmware tools running. Gracefull shutdown " -foregroundcolor green
#Power off gracefully
$objVM | Shutdown-VMGuest -confirm:$false
}
}
#}
$PowerFile.close()
}
function restoreEnvironment
{
[array] $VMs = Get-Content -Path $filePath"MTS_ON.txt"
foreach($VM in $VMs)
{
write-host "Starting $VM"
Get-VM -Name $VM | Start-VM -RunAsync -confirm:$false
}
#Delete the configuration file
Remove-Item $filePath"MTS_ON.txt"
}
#MAIN
#Test to see if the config file exists
if(Test-Path $filePath"MTS_ON.txt")
{
Write-Host "Restore from file? [Y]es or [N]o"
$response = Read-Host
if($response -eq "Y")
{
#Use file to restore VMs
Write-Host "Restore Environment"
restoreEnvironment
}
else
{
#Delete the configuration file
Remove-Item $filePath"MTS_ON.txt"
}
}
else
{#Save the powered VMs to a file
Write-Host "Saving Environment"
storeEnvironment
}
Disconnect-VIServer -Server $vCenter -Confirm:$false
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment