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!

 

9 Responses to Set-SoftPowerOff

  1. jfk8680 says:

    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«

  2. admin says:

    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

  3. jfk8680 says:

    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

  4. […] 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 […]

  5. DGlynn says:

    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.

  6. admin says:

    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. admin says:

    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)
    }
    }
    }

  8. DGlynn says:

    Thanks Hugo, and thanks for pointing me at the right cmdlet :)

Leave a Reply