Skip to content

Instantly share code, notes, and snippets.

@PatrickTerlisten
Created December 1, 2015 16:21
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PatrickTerlisten/4eff77f8dfbee5445136 to your computer and use it in GitHub Desktop.
Save PatrickTerlisten/4eff77f8dfbee5445136 to your computer and use it in GitHub Desktop.
A script to shutdown a vSphere Cluster.
<#
.SYNOPSIS
No parameters needed. Just execute the script.
.DESCRIPTION
This script will shut down all VMs, the ESXi hosts that were running the VMs, the vCenter VM
and, at the end, the ESXi host that was running the vCener VM.
Please modify all necessary variables. Make sure that you read the comments!
.EXAMPLE
Shutdown-vSphereCluster
.NOTES
Author: Patrick Terlisten, patrick@blazilla.de, Twitter @PTerlisten
This script is provided “AS IS” with no warranty expressed or implied. Run at your own risk.
This work is licensed under a Creative Commons Attribution NonCommercial ShareAlike 4.0
International License (https://creativecommons.org/licenses/by-nc-sa/4.0/).
.LINK
http://www.vcloudnine.de
#>
# vCenter Server
# I recommend to create a new VICredentialStoreItem and use this to connect to the vCenter Server
$VIServer = 'vcenter.domain.tld'
$VIServerVMName = 'NameofvCenterVM'
# ESXi Host that is running vCenter Server Appliance
$ESXHost = 'esx.domain.tld'
# To create an encrypted password file, execute the following command
# Read-Host -AsSecureString | ConvertFrom-SecureString | Out-File securestring.txt
# Fill $CredFileVIServer and $CredFileESXHost with path to the securestring file.
$CredFileVIServer = 'C:\Scripts\securestring-vcenter.txt'
$UsernameVIServer = 'Administrator@ad.domain.tld'
$PasswordVIServer = (Get-Content $CredFileVIServer | ConvertTo-SecureString)
$CredVIServer = New-Object System.Management.Automation.PSCredential ($UsernameVIServer, $PasswordVIServer)
$CredFileESXHost = 'C:\Scripts\securestring-esxi.txt'
$UsernameESXHost = 'root'
$PasswordESXHost = (Get-Content $CredFileESXHost | ConvertTo-SecureString)
$CredESXHost = New-Object System.Management.Automation.PSCredential ($UsernameESXHost, $PasswordESXHost)
# Connect to vCenter Server
Write-Host `n
Write-Host "Connecting to vCenter Server $VIServer" -ForegroundColor Green
Connect-VIServer -Server $VIServer -Credential $CredVIServer | Out-Null
# Name of the folder which contains VMs that should be shut down
$VMFolderName = 'PRODUKTION'
# Build an array with all VMs that should be shut down
$ShutdownVMs = (Get-Folder $VMFolderName | Get-VM)
# For every object in the array...
$ShutdownVMs | ForEach-Object {
# Tell me what you're doing...
Write-Host "### Shutdown VM $_ ###" -ForegroundColor Green
Stop-VMGuest $_ -Confirm:$false | Out-Null
}
# Wait 5 minutes
Write-Host '### Waiting 5 Minutes to let the VMs shutdown gracefully ###' -ForegroundColor Green
Start-Sleep 300
# Put hosts into maintenenace mode. Duplicate lines for each ESXi you wish to shutdown.
Write-Host '### Bringing hosts into maintenance mode ###' -ForegroundColor Green
Set-VMHost -VMHost esx.domain.tld -State Maintenance -Confirm:$false | Out-Null
# Shutdown ESXi hosts
Write-Host '### Initiate host shutdown ###' -ForegroundColor Green
Stop-VMHost -VMHost esx.domain.tld -Confirm:$false -Force | Out-Null
# Disconnect from vCenter Server
Write-Host "Disconnecting from vCenter Server $VIServer" -ForegroundColor Green
Disconnect-VIServer -Server $VIServer -Force -Confirm:$false | Out-Null
# Connect to ESXi Host which is running the vCenter VM
Write-Host "`n"
Write-Host "Connecting to ESXi Host $ESXHost which is running vCenter Server Appliance" -ForegroundColor Green
Connect-VIServer -Server $ESXHost -Credential $CredESXHost | Out-Null
# Tell me what you're doing...
Write-Host "### Shutdown vCenter Server Appliance $VIServerVMName ###" -ForegroundColor Green
Stop-VMGuest $VIServerVMName -Confirm:$false | Out-Null
# Wait 2 minutes
Write-Host '### Waiting 2 Minutes to let the vCenter VM shutdown gracefully ###' -ForegroundColor Green
Start-Sleep 120
Write-Host "### Bringing host $ESXHost into maintenance mode ###" -ForegroundColor Green
Set-VMHost -VMHost $ESXHost -State Maintenance -Confirm:$false | Out-Null
Write-Host "### Initiate shutdown of $ESXHost ###" -ForegroundColor Green
Stop-VMHost -VMHost $ESXHost -Confirm:$false -Force | Out-Null
Write-Host "Disconnecting from ESXi Host $ESXHost" -ForegroundColor Green
Disconnect-VIServer -Server $ESXHost -Force -Confirm:$false | Out-Null
Write-Host 'Script finished! Hope everything was shut down properly...' -ForegroundColor Green
@ivan-kishka
Copy link

Thank a lot!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment