Skip navigation
VMware

This Question is Possibly Answered

1 "correct" answer available (10 pts) 2 "helpful" answers available (6 pts)
2,248 Views 6 Replies Last post: Feb 9, 2009 9:48 AM by spchurchill RSS
mattssi Enthusiast 165 posts since
Mar 17, 2004
Currently Being Moderated

Dec 8, 2008 1:14 PM

Possible to shut down a host via PowerShell?

 

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?

 

 

halr9000 Master vExpert 1,124 posts since
Jun 7, 2007
Currently Being Moderated
1. Dec 8, 2008 1:36 PM in response to: mattssi
Re: Possible to shut down a host via PowerShell?

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

My signature used to be pretty, but then the forum software broked it. vExpert. Microsoft MVP (Windows PowerShell). Author, Podcaster, Speaker. I'm @halr9000
halr9000 Master vExpert 1,124 posts since
Jun 7, 2007
Currently Being Moderated
3. Dec 8, 2008 6:32 PM in response to: mattssi
Re: Possible to shut down a host via PowerShell?

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

My signature used to be pretty, but then the forum software broked it. vExpert. Microsoft MVP (Windows PowerShell). Author, Podcaster, Speaker. I'm @halr9000
spchurchill Enthusiast 22 posts since
Feb 15, 2008
Currently Being Moderated
4. Feb 6, 2009 8:53 AM in response to: halr9000
Re: Possible to shut down a host via PowerShell?

 

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

 

 

LucD Guru User Moderators vExpert 8,981 posts since
Oct 31, 2005
Currently Being Moderated
5. Feb 6, 2009 11:30 AM in response to: spchurchill
Re: Possible to shut down a host via PowerShell?

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: http://lucd.info | Twitter: @LucD22 | Book co-author: http://powerclibook.com
spchurchill Enthusiast 22 posts since
Feb 15, 2008
Currently Being Moderated
6. Feb 9, 2009 9:48 AM in response to: LucD
Re: Possible to shut down a host via PowerShell?

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!

Bookmarked By (0)

Share This Page

Communities