We need the ability to easily shut down our virtual environment in case of a power outage.
After shutting down the guests on each host, how can I initiate the shutdown of a host?
Edit your $profile and add these to it. Restart PowerShell and then you can use them.
filter Reboot-VMHost { param ( [switch]$force = "$( throw 'You must specify the Force parameter to continue.' )" ) ( Get-View -VIObject $_ ).RebootHost_Task( $force ) } filter Shutdown-VMHost { param ( [switch]$force = "$( throw 'You must specify the Force parameter to continue.' )" ) ( Get-View -VIObject $_ ).ShutdownHost_Task( $force ) }
Usage:
get-vmhost | shutdown-vmhost -force
[PowerShell MVP|https://mvp.support.microsoft.com/profile=5547F213-A069-45F8-B5D1-17E5BD3F362F], VI Toolkit forum moderator
Author of the upcoming book: Managing VMware Infrastructure with PowerShell
Co-Host, PowerScripting Podcast (http://powerscripting.net)
Need general, non-VMware-related PowerShell Help? Try the forums at PowerShellCommunity.org
I will try this, thank you!
While searching (which I should have done before...), I found this syntax too
<code>Get-VMHost | %{Get-View $_.ID} | %{$_.ShutdownHost_Task($TRUE)}</code>
I sort of have an idea, but can you explain how this is actually working?
Get-VMHost | %{Get-View $_.ID} | %{$_.ShutdownHost_Task($TRUE)}
This is basically the same as mine, just a different way of getting there. Translated to English, it says:
- Get the host servers, and pass them down the pipeline
- For each host server (% is an alias for the Foreach-Object cmdlet), do what's in the curly braces
- Get a "managed view" of the current object in the pipeline by referencing its ID property, and "emit" it. Since this is the end of the script block, that managed view of the host server object gets passed down the pipeline.
- For each of those, invoke the ShutdownHost_Task() method, providing it with a single value, which is Boolean TRUE.
P.S. click on the plain text markup help button to read up on tags like . I've found the best combination is to use embedded inside of tags. I've inserted a space between the word and the curly brace for illustration, remove those in your posts.
[PowerShell MVP|https://mvp.support.microsoft.com/profile=5547F213-A069-45F8-B5D1-17E5BD3F362F], VI Toolkit forum moderator
Author of the upcoming book: Managing VMware Infrastructure with PowerShell
Co-Host, PowerScripting Podcast (http://powerscripting.net)
Need general, non-VMware-related PowerShell Help? Try the forums at PowerShellCommunity.org
Hi,
I've tried to use this script but it just sits there then times out. Would it be because I'm using it for only one ESX host as below rather than all of them in your example code?
Get-VMHost -Name "hostname" | %{Get-View $_.ID} | %{$_.ShutdownHost_Task($TRUE)}
Thanks, Sam
Could you try the following script with one of the ESX hosts that doesn't seem to reboot ?
It first displays if the host is rebootable or not.
It shows the state of the task.
And finally it shows the possible error and the result of the task.
$esx = Get-VMHost <ESX-hostname> | Get-View Write-Host "Host rebootable ?" $esx.Capability.RebootSupported $taskMoRef = $esx.ShutdownHost_Task($true) $task = Get-View $taskMoRef while ($task.Info.State -eq "running" -or $task.Info.State -eq "queued") { Write-Host $task.Info.State $task = Get-View $taskMoRef } Write-Host "Error:" $task.Info.Error Write-Host "Result:" $task.Info.Result
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Hmmm, I tried the original script again after a hard reboot of the ESX box and it worked fine. Thanks very much for your extra diagnosis script tho - I'm still learning PS so all of this sort of thing expands the possibilities!