Automation

 View Only
  • 1.  script not giving all VMs output

    Posted Dec 07, 2015 01:58 PM

    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



  • 2.  RE: script not giving all VMs output

    Posted Dec 07, 2015 02:18 PM

    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.



  • 3.  RE: script not giving all VMs output

    Posted Dec 07, 2015 02:25 PM

    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?



  • 4.  RE: script not giving all VMs output
    Best Answer

    Posted Dec 07, 2015 02:44 PM

    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

       }



  • 5.  RE: script not giving all VMs output

    Posted Dec 07, 2015 02:47 PM

    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



  • 6.  RE: script not giving all VMs output

    Posted Dec 07, 2015 03:12 PM

    Thanks for your help. That was it.