VMware PowerCLI VM Shutdown Control

Dear friends, I have a 2-line script (in bold, below) that will find all powered-on VMs and shut them down.
QUESTION: How can I ensure that the vCenter server (to which I am connected) is the last VM to shutdown?
$vmservers=Get-VM | Where-Object {$_.powerstate -eq ‘PoweredOn’}
$vmservers| Shutdown-VMGuest-Confirm:$false

Yours truly,
waltforbesSenior IT SpecialistAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Zephyr ICTCloud ArchitectCommented:
There are several ways you might tackle this, for an environment I manage we use a folder structure in VMware.
VMs are placed into folders according to their function, e.g. Database Servers are placed into the folder DB_VMs, File and Print servers in PF_VMs, vCenter VM(s) are placed into VC_VMs, and so on ...

That way you can shut down folder by folder, example:

$vmservers=get-vm -location “DB_VMs” | Where-Object {$_.powerstate -eq ‘PoweredOn’}

$vmservers |select Name | export-csv c:\ScriptDB\vms.csv -NoTypeInformation

$vmservers | Shutdown-VMGuest -Confirm:$false

Open in new window

I export the VMs I'm bringing down so I always know which ones where powered on in case confusion sets in, and it's also easy to use it to start the VMs using PowerCLI.
waltforbesSenior IT SpecialistAuthor Commented:
Hi Spravek, I have more than 30 (thirty) folders! Is there a way to modify this script to have it exempt a specific folder, or exempt just a specific VM?

Thank you for your attention, and I look forward to your reply.

Kind regards,
Zephyr ICTCloud ArchitectCommented:

You could do a one liner like this:

Get-VM | Where-Object {$_.name -ne $NameofvCenterServer } | Shutdown-VMGuest -Confirm:$false

Open in new window

If you have a separate Database server for vCenter you can add this (just after the $NameofvCenterServer):

-and $_.name -ne $NameofvCenterDatabase

Open in new window

Would that be a better solution?

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
waltforbesSenior IT SpecialistAuthor Commented:
This is as beautiful as a fairy-tale princess and as charming as her prince! Love it, love it, love it! Thank you so much, spravtek!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.