PowerCLI C:\> $Clusters = Get-Cluster -location xx
PowerCLI C:\>
PowerCLI C:\> foreach ($Cluster in $Clusters){
>>
>> $VMHosts = Get-Cluster $Cluster | (Get-VMHost).count
>> $VMs = Get-Cluster $Cluster | (Get-VM).count
>> }
>>
Expressions are only allowed as the first element of a pipeline.
At line:2 char:57
+ $VMHosts = Get-Cluster $Cluster | (Get-VMHost).count <<<<
+ CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : ExpressionsMustBeFirstInPipeline
Anyway i can get the values in a variable here ? I tried above gives error.
Try like this
$Clusters = Get-Cluster -location xx
foreach ($Cluster in $Clusters){
$VMHosts = (Get-VMHost -Location $Cluster).count
$VMs = (Get-VM -Location $Cluster).count
}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Ya 1 more work-around fund was
$Clusters = Get-Cluster -location xx
foreach ($Cluster in $Clusters){
$VMHosts = Get-Cluster $Cluster | Get-VMHost
$VMs = Get-Cluster $Cluster | Get-VM
$TotalVs = $VMs.count
Write-Host "$Cluster has $TotalVs guests "
}
There is no need to a Get-Cluster inside the ForEach loop, you already have the cluster object in the $cluster variable
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference