VMware Cloud Community
mattssi
Contributor
Contributor
Jump to solution

How to count the number of VM's that are powered on.

Trying to learn this PowerShell stuff....oh what fun.

Since maintenance mode doesn't work unless you manually VMotion the servers off or shut them down, I'm trying to program a loop to check to make sure that all VM's on the host are off.

How do I get the value of "#PoweredOn" in this statment so that I can compare it? I want to exit the loop when the number of powered on VM's = 0.

function shutdownGuests()

{

Get-VMHost HOST1| Get-VM | Shutdown-VMGuest

$num_powered_on = $null

do {

Write-Host $num_powered_on

Write-Host $PoweredOn

$num_powered_on = Get-VMHost HOST1 | Select-Object Name, @{Name="#PoweredOn"; Expression={($_ | Get-VM | Where-Object {$_.PowerState -eq "PoweredOn"} | Measure-Object).count}}

Start-Sleep -Seconds 10

} while($num_powered_on > 0)

#Loop and check to see if all VM's are shut down, then return to main()

}

Reply
0 Kudos
1 Solution

Accepted Solutions
admin
Immortal
Immortal
Jump to solution

Try something like this:

while (get-vmhost XXX | get-vm | where { $_.PowerState -ne "PoweredOff" }) {
    # Wait a while
}

View solution in original post

Reply
0 Kudos
7 Replies
admin
Immortal
Immortal
Jump to solution

Try something like this:

while (get-vmhost XXX | get-vm | where { $_.PowerState -ne "PoweredOff" }) {
    # Wait a while
}

Reply
0 Kudos
mattssi
Contributor
Contributor
Jump to solution

That worked beautifully! Thank you.

Reply
0 Kudos
mattssi
Contributor
Contributor
Jump to solution

Where do you get the commands / syntax for what you used in the "where" clause?

Reply
0 Kudos
admin
Immortal
Immortal
Jump to solution

It's really a matter of experience more than anything. If you look at the help for Where-Object there is a -filterscript argument that takes a scriptblock. It turns out -filterscript is positional at argument 1 (which is why I didn't have to specify -filterScript) and script blocks are created by surrounding a script with { }. So it all makes sense and is documented, if you know how to interpret it, but I don't really feel it's something you could just figure out yourself without some examples.

Also, try checking out "help about_filter" and "help about_logical_operator" for some pointers.

Reply
0 Kudos
halr9000
Commander
Commander
Jump to solution

The way the conditional statements like if, while, and the Where-Object cmdlet work, is that if the stuff in between the parens evaluates to true, or outputs anything, then what's in the braces will "fire". e.g.

while ( $true ) { write-host "infinite loop!" }

while ( -not get-vm ) { write-hsot "we have a VM" } # This will wait for VMs to be added to an empty VI

Then there's the switch statement which should wait for another day. Smiley Happy






[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
Reply
0 Kudos
mattssi
Contributor
Contributor
Jump to solution

Thanks again for the input...

Can you explain which language this is, or where I can find some documentation on this in particular? (The PowerState property)

$_.PowerState -eq "PoweredOn"}

Reply
0 Kudos
halr9000
Commander
Commander
Jump to solution

Why, it's PowerShell. That is the point of this very forum. As to the PowerState property documentation, I'm working on it. I don't if VMware has any detailed descriptions of the objects emitted by their Get cmdlets anywhere. There's an ebook around somewhere, perhaps someone can find the link to it, I cannot at the moment.






[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
Reply
0 Kudos