VMware

Shutdown script didn’t work as expected

Hi all, this was a last minute script for a shutdown I slapped together with some things I found on google (mainly LucD on the vmware forums).

Basically I was trying to shutdown everything in our cluster except three servers, it worked kind of worked but ended up taking out the vcenter server although I (think!) asked it not to.

Could I get another set of eyes on this to see what I’ve missed? Basically I want the DC/Vcenter/Backup server to stay on.

`Get-Cluster -Name $clusterName | Get-VM | Where-Object {$_.Powerstate -eq “PoweredOn” -and $_.Name -notlike “DCNAME” -or $_.Name -notlike “vCenter” -or $_.Name -notlike “BACKUPNAME”} | Shutdown-VMGuest -Confirm:$false`

Thanks!



View Reddit by Renegade-PervertView Source

 

To see the full content, share this page by clicking one of the buttons below

Related Articles

6 Comments

  1. Did you test this?

    If you ran your code just without the shutdown part did it return the right servers?

    If you used the `-whatif` switch what did it say

  2. Two issues. One is, as said by someone else, the lack of wildcards with the likes/not likes are a problem.

    Second your Boolean logic is bad. You are mixing ‘and’s and ‘or’s and I am not sure you understand what you’re saying. Please separate the and and ors you think you are pairing together with parentheses.

    If (name is blah1 -or name is blah2) -and (powerstate -eq on)

    By you writing it without parens, you’ve basically said if on but not a DC then shutdown otherwise shutdown regardless of power state

  3. To add to what everyone else said, one liner’s are fun but can very quickly become confusing to read or figure out order of logic. Use the Powershell ISE, and break it out.

    ​

    [https://pastebin.com/XZwqZp6w](https://pastebin.com/XZwqZp6w)

    As a quick example, this will skip any VMs in the listed array, as long as the name matches exactly. Tweak as desired.

  4. You could simplify the filter logic by using the `-notin` operator:

    `Get-Cluster -Name $clusterName | Get-VM | Where-Object {$_.Powerstate -eq “PoweredOn” -and $_.Name -notin @(‘DCNAME’, ‘vCenter’, ‘BACKUPNAME’)}`

    And see if the list of VMs returned matches your expectations

Leave a Reply