amurph
Contributor
Contributor

script to shut down vm's on specific host

Jump to solution

I'm trying to write a script to gracefully shutdown all the vm's on a specific host. I can make it work just fine by connecting directly to the host with:

get-esx myesxhost -user root -password password

shutdown-vmguest *

that works really well. But what I want to do is connect via VC to use AD accounts rather than using host root accounts.

so:

connect-viserver (or get-vc) myvcserver -user me -password password

shutdown-vmguest * -server myesxhost

gets me an error saying "cannot bind parameter 'Server". Cannot convert "myesxhost" to "VMware.VimAutomation.Types.VIServer."

I looked around and found some info that when you're connected to VC, a host doesn't count as a "server" when using the -Server parameter. Or something like that. I'm new to scripting so apologies if I'm getting that wrong.

So I tried something I found on the forum here, where you create a variable for $server that's tied to a host

$server = get-vmhost myesxhost

And then I tried:

shutdown-vmguest * -server $server

and I get the same error as before, including the correct hostname. So I know that I was successful in creating $server, but apparantly that's not the right solution to the problem.

Help please?

Thanks!

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership

You can do it with this one-liner.

It only shuts down guests that are actually powered on.

Get-VMHost -Name <ESX-hostname> | Get-VM | ? { $_.PowerState -eq 'PoweredOn' } | Shutdown-VMGuest


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

0 Kudos
3 Replies
LucD
Leadership
Leadership

You can do it with this one-liner.

It only shuts down guests that are actually powered on.

Get-VMHost -Name <ESX-hostname> | Get-VM | ? { $_.PowerState -eq 'PoweredOn' } | Shutdown-VMGuest


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

0 Kudos
ykalchev
VMware Employee
VMware Employee

Hi,

You can shut down vm's on specific host using following command:

Get-VMHost <myhost> | Get-VM | Shutdown-VMGuest

Notes:

1. Server parameter is used to specify different connections returned by the Connect-VIServer. You can used it when writing script managing multiple VI servers or multiple esx hosts not manged by 1 VC

2. Using Location parameter of the Get-VM you can specify not only VMHost but resource pools,datacenters, cluster and folders

Regards,

Yasen

Yasen Kalchev, vSM Dev Team
amurph
Contributor
Contributor

Thanks a lot, everyone! Got it working with your help.

Andy

0 Kudos