Hi, I am learning Powershell and I am stuck with the where-object cmdlet. I am trying to get number of Powered-on VMs which are there in a vCenter Server. but I want to avoid 2 clusters from the list.
With this example
get-cluster | where-object {$_.Name -ne "Cluster_Name"} | where-object ({$_.Powerstate -eq "Poweredon") | measure-object
This works fine and it gived me the number of poweredon VMs. But I would also like to
1) Give one more name of the cluster, it will be nice if where-object can have "and"
2) Is there any quick way to get this done.
Thanks
$count = 0
get-cluster | %{
if (( $_.Name -ne "cl1") -or ($_.Name -ne "cl2")) {
$count += ($_ | get-vm | ?{ $_.PowerState -eq "poweredOn" }).count
}
}
should do it.
No need for an -and operator in this case, you can use the -contains and -notcontains operators.
Something like this
Get-Cluster | where-object {"Cluster1","Cluster2" -notcontains $_.Name} | Get-VM | where-object {$_.Powerstate -eq "Poweredon"} | Measure-Object
In the sample you avoid the clusters Cluster1 and Cluster2
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference