any idea why the below script is not giving me all my output of my VMs? I am missing VMs
$clusters = get-cluster
foreach ($cluster in $clusters) {
$vmhosts = $cluster | get-vmhost
foreach ($vmhost in $vmhosts){
$vms = $vmhost | get-vm
foreach ($vm in $vms) {
$vminfo = New-Object -TypeName PSObject
$vminfo | Add-Member -MemberType NoteProperty -Name VM -Value $vm.Name
$vminfo | Add-Member -MemberType NoteProperty -Name Powerstate -Value $vm.PowerState
$vminfo | add-member -membertype NoteProperty -name VMHost -value $vmhost.Name
$vminfo | add-member -MemberType NoteProperty -Name Clustername -Value $cluster.Name
}
$report += $vminfo
}
}
write-host $report
Try moving the $report += $vminfo inside the foeach VM loop, i.e.
foreach ($vm in $vms) {
$vminfo = New-Object -TypeName PSObject
$vminfo | Add-Member -MemberType NoteProperty -Name VM -Value $vm.Name
$vminfo | Add-Member -MemberType NoteProperty -Name Powerstate -Value $vm.PowerState
$vminfo | add-member -membertype NoteProperty -name VMHost -value $vmhost.Name
$vminfo | add-member -MemberType NoteProperty -Name Clustername -Value $cluster.Name
$report += $vminfo
}
any idea why the below script is not giving me all my output of my VMs? I am missing VMs
Could you please clarify what do you mean by that?
If it means, some VMs are missing, it could be due to the fact that yoiur script will only identify VMs located in a VMware cluster.
If a VM is on an ESXi host, not part of a cluster, the VM will be missing.
hi
so when do
$report | mesure
It shows only 29 vms
but
get-vm | measure
shows a lot more
The script should go through each cluster and vm host and put the add the vms into the empty report array correct?
Try moving the $report += $vminfo inside the foeach VM loop, i.e.
foreach ($vm in $vms) {
$vminfo = New-Object -TypeName PSObject
$vminfo | Add-Member -MemberType NoteProperty -Name VM -Value $vm.Name
$vminfo | Add-Member -MemberType NoteProperty -Name Powerstate -Value $vm.PowerState
$vminfo | add-member -membertype NoteProperty -name VMHost -value $vmhost.Name
$vminfo | add-member -MemberType NoteProperty -Name Clustername -Value $cluster.Name
$report += $vminfo
}
Please try to move the line $report += $vminfo
$clusters = get-cluster
foreach ($cluster in $clusters) {
$vmhosts = $cluster | get-vmhost
foreach ($vmhost in $vmhosts){
$vms = $vmhost | get-vm
foreach ($vm in $vms) {
$vminfo = New-Object -TypeName PSObject
$vminfo | Add-Member -MemberType NoteProperty -Name VM -Value $vm.Name
$vminfo | Add-Member -MemberType NoteProperty -Name Powerstate -Value $vm.PowerState
$vminfo | add-member -membertype NoteProperty -name VMHost -value $vmhost.Name
$vminfo | add-member -MemberType NoteProperty -Name Clustername -Value $cluster.Name
$report += $vminfo
}
}
}
write-host $report
Thanks for your help. That was it.