VMware Cloud Community
tdubb123
Expert
Expert
Jump to solution

script not giving all VMs output

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

0 Kudos
1 Solution

Accepted Solutions
cjscol
Expert
Expert
Jump to solution

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

   }

Calvin Scoltock VCP 2.5, 3.5, 4, 5 & 6 VCAP5-DCD VCAP5-DCA http://pelicanohintsandtips.wordpress.com/blog LinkedIn: https://www.linkedin.com/in/cscoltock

View solution in original post

0 Kudos
5 Replies
ccalvetTCC
Enthusiast
Enthusiast
Jump to solution

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.

Blog: http://thecrazyconsultant.com/ | Twitter: @ccalvetTCC
0 Kudos
tdubb123
Expert
Expert
Jump to solution

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?

0 Kudos
cjscol
Expert
Expert
Jump to solution

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

   }

Calvin Scoltock VCP 2.5, 3.5, 4, 5 & 6 VCAP5-DCD VCAP5-DCA http://pelicanohintsandtips.wordpress.com/blog LinkedIn: https://www.linkedin.com/in/cscoltock
0 Kudos
ccalvetTCC
Enthusiast
Enthusiast
Jump to solution

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

Blog: http://thecrazyconsultant.com/ | Twitter: @ccalvetTCC
0 Kudos
tdubb123
Expert
Expert
Jump to solution

Thanks for your help. That was it.

0 Kudos