I was looking for a solution to automate the control of my home VMware servers to save on the power draw by booting them only when needed (vs leaving them running 24x7). The average UCS C-series server costs between $40 to $60 per month to run continuously. In general M5s draw more power than M4s or M3s.
I knew there were ways to control both the server hardware and ESXi via Powershell. Not being a Powershell scripter, I spend way too much time searching for commands and arguments, looking at examples, and using trial and error until I got it right. I wanted to post my results to help others save some time and eliminate frustration for any Powershell newbies like me attempting to figure this out.
I use the UCS IMC Powertools for Windows Powershell via the CIMC to power up the server I use the most. Other servers/routers/etc I have on a Web-based PDU so I can quickly hit a web page and power those up. A CIMC draws about 0.2A continuously, and the cost of that too adds up over the course of a year. I set all of my VMs on the ESXi hosts to autostart. I also set the power policy for the BIOS/Compute on the servers to Power On (so when PDU applies power they boot up).
References:
Cisco UCS PowerTool Suite Installation and Configuration Guide, Release 2.x
https://www.cisco.com/c/en/us/td/docs/unified_computing/ucs/sw/msft_tools/installation_guide/powertool /b_Pwrtool_Install_and_Config/b_Install_and_Config_chapter_010.html
Cisco Integrated Management Controller (IMC) PowerTool User Guide, Release 2.x
https://www.cisco.com/c/en/us/td/docs/unified_computing/ucs/sw/msft_tools/C-Series/powertools/ucs_powertool_book/2x/b_Cisco_IMC_PowerTool_UG_2x/b_Cisco_IMC_PowerTool_UG_2x_chapter_01.html
Download and install the UCS Powershell Modules
https://software.cisco.com/download/home/286282679/type/284574017/release/1.4.2
VmwarePowerCLI Community
https://communities.vmware.com/community/vmtn/automationtools/powercli
This is a basic script that connects, authenticates, powers up the server, the pings the last VM just to let me know when all VMs have booted.
Create Filename: esx-101-Power-On.ps1
$password = ConvertTo-SecureString 'myCIMCpassword' -AsPlainText -Force
$credential = New-Object System.Management.Automation.PSCredential ('myCIMClogin', $password)
Connect-IMC -Name C240-m5-101.collabpoc.net -Credential $credential | Out-Default
Get-ImcRackUnit | Start-ImcServer -Force | Out-Default
Write-Output ""
Write-Output "Powering on C240-M5-101 (ESX-101)....."
Write-Output ""
#Start-ImcKvmSession
Write-Output ""
Write-Output "Success when last VM startup is complete:"
Write-Output ""
ping CiscoWKST4.collabpoc.net -t
Then I use Vmware's PowerCLI to shut the system down. Instead of shutting the VMs down, I suspend them. The results in very fast VM shutdown and bootup times. Using this method I have 23 VMs that autostart faster than the time it takes for the ESXi just to boot.
Open a Powershell prompt as Administrator:
Set-ExecutionPolicy RemoteSigned
Install-Module -Name VMware.PowerCLI
Set-PowerCLIConfiguration -Scope User -ParticipateInCEIP false
Import-Module VMware.VimAutomation.Core
Set-PowerCLIConfiguration -InvalidCertificateAction Ignore -Confirm:$false
Set-PowerCLIConfiguration -DisplayDeprecationWarning
Create Filename: esx-101-Suspend-Shutdown.ps1
(the # is a comment delimiter, script will ignore the text that follows)
#Set-PSDebug -Trace 1 #Echos all commands to console for help with debugging output
Write-Output "Connecting to the following ESXi Host:"
Write-Output ""
Connect-VIServer -Server esx-101.collabpoc.net -User myESXiLogin-Password myESXiPassword | Out-Default
Write-Output ""
Write-Output "Suspending the Running VMs....."
Write-Output ""
# Parse all PoweredOn VMs and write to a file
$vmservers=Get-VM | Where-Object {$_.powerstate -eq ‘PoweredOn’}
$vmservers | select Name | export-csv ‘C:VmwareScripts\CSV-Output\esx-101-PoweredOn.csv’ -NoTypeInformation
# Pipe the file into the Suspend command
$vmservers | Suspend-VM -Confirm:$false
Write-Output ""
Write-Output "Confirming the Power State of the VMs Before Shutdown:"
Write-Output ""
# This will show/sort the VMs by powerstate so you can ensure they are suspended
Get-VM | where-object {$_.PowerState -eq "PoweredOn"}
Get-VM | where-object {$_.PowerState -eq "PoweredOff"}
Get-VM | where-object {$_.PowerState -eq "Suspended"}
#Start-Sleep -s 60 # Wait time is not necessarily needed, but here as an example
Write-Output ""
Write-Output "Prompting to shutdown the host:"
Write-Output ""
#-Confirm:$true will prompt and wait for you to confirm ESX shutdown. Change to $false to skip prompt.
Stop-VMHost -Confirm:$true -Force | Out-Default
Write-Output ""
Write-Output "Monitoring the Host to Ensure Shutdown:"
Write-Output ""
#when the pings stop the server is shutdown
ping esx-101.collabpoc.net -t
So now I have two Powershell Scripts in a directory, and I just double click each file in Windows Explorer to run the script. Here are a few more tips you may need to make this work successfully on Windows:
https://blog.danskingdom.com/fix-problem-where-windows-powershell-cannot-run-script-whose-path-contains-spaces/
https://blog.danskingdom.com/keep-powershell-console-window-open-after-script-finishes-running/
Hope this helps add convenience and decrease your home energy costs!