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()
}
Try something like this:
while (get-vmhost XXX | get-vm | where { $_.PowerState -ne "PoweredOff" }) { # Wait a while }
Try something like this:
while (get-vmhost XXX | get-vm | where { $_.PowerState -ne "PoweredOff" }) { # Wait a while }
That worked beautifully! Thank you.
Where do you get the commands / syntax for what you used in the "where" clause?
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.
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.
[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
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"}
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