28
Aug/087
Aug/087
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.
11:13 on August 29th, 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
11:29 on August 29th, 2008
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
13:35 on August 29th, 2008
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
21:00 on October 16th, 2008
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.
7:34 on October 17th, 2008
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.
7:39 on October 17th, 2008
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)
}
}
}
16:44 on October 17th, 2008
Thanks Hugo, and thanks for pointing me at the right cmdlet :)