PeetersOnline.nl
Set-SoftPowerOff
Helpful function of the day:
This one was requested by jfk8680 on the VMware Toolkit Community.
It allows you to change what happens when you press the power off button for a vm. It sets it to “soft” which means a gentle Guest OS Shutdown. Usage: Set-SoftPowerOff $VMs , where $VMs is a collection of one or more VMs, grabbed by Get-VM. Oh, don’t forget to Connect-VIServer first!
Set-SoftPowerOff (rename to .ps1)
Enjoy!
No related posts.
9 Responses to Set-SoftPowerOff
Leave a Reply Cancel reply
Tags
Active Directory API bind order cleanup cluster CPU Custom Fields datastores description device management directory tree errors Event Log file name filter Fun function HA IT known issues License Server LUN multipath NIC objects Oneliner portgroups PowerCLI PowerShell profile recursive Registry Scripts security session share snapshots SQL Stat VI Toolkit VMware vSphere WMI WSUS ZenRecent Comments
- Ben @ Geekswing on Helpful script of the day: HA calculations
- Jo Lambrecht on WSUS Cleanup with Powershell
- Luke on Powershell Open File Dialog Box
- Umapathi on WSUS Cleanup with Powershell
- Yomodo on User Confirmation in Powershell
Archives
- July 2012
- July 2011
- February 2011
- January 2011
- December 2010
- May 2010
- October 2009
- September 2009
- August 2009
- July 2009
- June 2009
- May 2009
- April 2009
- March 2009
- February 2009
- January 2009
- December 2008
- November 2008
- October 2008
- September 2008
- August 2008
- July 2008
- June 2008
- May 2008
- April 2008
- March 2008





Hi Hugo,
I’m pretty new to this scripting stuff
. I’ve installed powershell and the VI toolkit and can run basic commands such as Get-VM succesfully.
Could you please explain how I can run this script so it modifies all my VMs?? I promise I’l dive into the powershell documentation asap but I’m in need of bit of a quick fix at the moment
Thanks a lot for your help!
Jeffrey
»crosslinked«
Hi Jeffrey,
Welcome to my blog!
To use this script, first save it to disk.
Then start Powershell / VI Toolkit.
run the script by typing the full path to the script file (use TAB for autocomplete). NOTE: you should precede the path with a dot and a space (it’s called Dot-Sourcing and allows you to use the scripts functions and variables after closing the script), e.g.: . “d:\scripts\Set-SoftPowerOff.ps1″ . No output is shown.
Connect to VC: Connect-VIServer “MyVCServer”.
Then get the vms you want to run against, e.g.: $MyVMs = Get-VM to get all vms or $SomeVM = Get-VM “MyVMName”.
Then run the function, feeding it the collection of vms: Set-SoftPowerOff $MyVMs.
Alternatively, you could edit the script and put the commands I listed above into the file itself. Then you could run the script by typing the path to the script.
Hope that clarifies things for you. If not, please respond.
Hugo
Hugo,
It worked flawlessly. Thanks for taking me through it step by step. This is really a powerfull way to manage a virtual infrastructure. Great stuff!!
Jeffrey
[...] Set-SoftPowerOff [...]
[...] te passen. Dit kun je ook via Powershell doen. Hugo Peeters heeft daar een blogpost over gemaakt http://www.peetersonline.nl/index.php/vmware/set-softpoweroff/. Hoe je dit script kunt gebruiken lees je in deze [...]
Suggestion, can you have your function check the value of PowerOffType and not make the change and call ReconfigVM again if it is already set to “soft”.
I did find that it has changed the behavior of the Power Off button in VIC, but the Stop-VM cmdlet still calls a hard power off.
Great suggestion, working on it right now. I’ll post it when it’s finished.
The Stop-VM cmdlet will always do a hard power off. You can use Shutdown-VMGuest for a sof power off. We are only changing the behaviour of the Power Off button from Stop-VM to Shutdown-VMGuest.
Okay, here’s how to check the setting before modifying it:
function Set-SoftPowerOff
{
param($VMs)
ForEach ($VM in $VMs)
{
$VMadv = $VM | Get-View
If ($VMadv.Config.DefaultPowerOps.DefaultPowerOffType -ne “soft”)
{
$VMConfigSpec = New-Object VMware.Vim.VirtualMachineConfigSpec
$VMConfigSpec.PowerOpInfo = New-Object VMware.Vim.VirtualMachineDefaultPowerOpInfo
$VMConfigSpec.PowerOpInfo.PowerOffType = “soft”
$VMadv.ReconfigVM($VMConfigSpec)
}
}
}
Thanks Hugo, and thanks for pointing me at the right cmdlet