3 Replies Latest reply on Dec 2, 2011 2:11 AM by LucD

    Script to extract VM data. Almost there....

    StuartB201110141 Novice

      Hi,

       

      I have to find the following data from several hundred VMs on specific hosts.

       

      VM Name

      Host it belongs to

      Memory allocated

      VCpu

      Disk space used

      Network Port group details (ie name)

       

      Now the first few bits do seem to work with this script. However getting port group details can only be done with a get-networkapaters. I need to kind of join the two, but not sure how to do it:

       

      Connect-VIserver "myserver";

       

      foreach ($srv in Get-Content "d:\hostlist.csv") {

       

          Get-VMHost $srv | Get-VM | Select-Object "name","version","vmhost","guest","powerstate","numcpu","usedspacegb";

       

      }

       

       

      The hostlist.csv is actually just a text file and contains host names.

       

      Any help appreciated.

       

       

       

       

       

      I managed to scratch up

        • 1. Re: Script to extract VM data. Almost there....
          LucD Guru
          vExpertCommunity WarriorsUser Moderators

          That's where a calculated property comes in handy.

          You can get the portgroupname(s) like this

           

          Connect-VIserver "myserver";
          foreach ($srv in Get-Content "d:\hostlist.csv") {
              Get-VMHost $srv | Get-VM | 
                  Select-Object name,version,vmhost,guest,powerstate,numcpu,usedspacegb,
                      @{N="Portgroups";E={[string]::Join(',',(Get-NetworkAdapter -VM $_ | %{$_.NetworkName}))}}
          }
          
          
          • 2. Re: Script to extract VM data. Almost there....
            StuartB201110141 Novice

            Hi Luc,

             

            Thanks for that. I see what you did with tacking the sql style join on the end. One quick question though. I know I can use export-csv to export it. Where should it go?

             

            Many thanks

             

            Stuart

            • 3. Re: Script to extract VM data. Almost there....
              LucD Guru
              vExpertCommunity WarriorsUser Moderators

              Hi Stuart,

              The Join is there to avoid problems with VMs that have more than 1 NIC, it just converts an array of portgroupnames into a single string.

              The Export-Csv is a bit trickier since you have the Select in a foreach loop.

              That can be solved like this

               

              Connect-VIserver "myserver";
              $report = foreach ($srv in Get-Content "d:\hostlist.csv") {
                  Get-VMHost $srv | Get-VM | 
                      Select-Object name,version,vmhost,guest,powerstate,numcpu,usedspacegb,
                          @{N="Portgroups";E={[string]::Join(',',(Get-NetworkAdapter -VM $_ | %{$_.NetworkName}))}}
              }
              
              $report | Export-Csv "C:\report.csv" -NoTypeInformation -UseCulture