5 Replies Latest reply on Jun 1, 2019 3:57 PM by LucD

    Utilizing Get-View to export results to CSV file

    blueten Lurker

      I'm connecting to multiple vCenters and obtaining the vm's and attempting to export results from Get-View to a CSV file. The only results that I get when I run my script is the headers.

      Could someone point out what I'm doing wrong in the code:

       

      #CSV Path and file name to be created

      $ExportFilePath = "c:\temp\VMInfo-MultipleVCs.csv"

       

      #Connect to the multiple vCenters

      Write-Host "Connecting to vCenters" -ForegroundColor Cyan

      $VIServers = Get-Content -Path C:\temp\vcenters.txt

      $c = Get-Credential

      $VIServers = Connect-VIServer $VIServer -Credential $c  -warningaction 0

       

      ForEach ($VIServer in $VIServers) {

          Write-Host "Gathering Statistics for VMs in $VIServer"

          $table = @()

          $vm = Get-VM

       

          ForEach ($vm in $VMs) {

              $Report = "" | Select VIServer,Name,DNS_Name,OS,PowerState,Template_Status,IPAddress,LastBootTime,Notes

              $Report.VIServer = $_.VIServer

              $Report.Name = (get-View $vm.Name)

              $Report.DNS_Name = (get-View $vm.Guest.HostName)

              $Report.OS = (get-View $vm.Guest.GuestFullName)

              $Report.PowerState = (get-View $vm.Summary.Runtime.powerstate)

              $Report.Template_Status = (get-View $vm.Summary.Config.Template)

              $Report.IPAddress = (get-View $vm.Summary.IpAddress)

             $Report.LastBootTime = (get-View $vm.Summary.Runtime.BootTime)

              $Report.Notes = (get-View $vm.config.annotation)

              $table += $Report

             }

      }

       

       

      $table | Export-Csv $ExportFilePath -NoTypeInformation

       

      Disconnect-VIServer -Confirm:$False

       

      Thanks so much!

        • 1. Re: Utilizing Get-View to export results to CSV file
          LucD Guru
          User ModeratorsvExpertCommunity Warriors

          You should probably initialise the resulting $table outside the loop.
          The resulting VMs per vCenter should be collected in $vms, not $vm.
          Not sure why you have a Get-View for most properties, this can also be reached via the ExtensionData property.

           

          #CSV Path and file name to be created

           

          $ExportFilePath = "c:\temp\VMInfo-MultipleVCs.csv"

          #Connect to the multiple vCenters

          Write-Host "Connecting to vCenters" -ForegroundColor Cyan

          $VIServers = Get-Content -Path C:\temp\vcenters.txt

          $c = Get-Credential

          $VIServers = Connect-VIServer $VIServer -Credential $c  -warningaction 0


          $table = @()


          ForEach ($VIServer in $VIServers) {

             Write-Host "Gathering Statistics for VMs in $VIServer"

             $vm = Get-VM -Server $VIServer

             ForEach ($vm in $VMs) {

             $row = "" | Select VIServer,Name,DNS_Name,OS,PowerState,Template_Status,IPAddress,LastBootTime,Notes

             $row.VIServer = $VIServer.Name

             $row.Name = $vm.Name

             $row.DNS_Name = $vm.Guest.HostName

             $row.OS = $vm.Guest.GuestFullName

             $row.PowerState = $vm.PowerState

             $row.Template_Status = $vm.ExtensionData.Summary.Config.Template

             $row.IPAddress = $vm.ExtensionData.Summary.IpAddress

             $row.LastBootTime = $vm.ExtensionData.Summary.Runtime.BootTime

             $row.Notes = $vm.ExtensionData.config.annotation

             $table += $row

             }

          }

          $table | Export-Csv $ExportFilePath -NoTypeInformation

          Disconnect-VIServer -Confirm:$False

          • 2. Re: Utilizing Get-View to export results to CSV file
            blueten Lurker

            Thanks so much for the quick reply.

             

            In response to "Not sure why you have a Get-View for most properties". It's because I'm a total novice with scripting and trying to make some progress.  aka rookie 

             

            The CSV file is now returning without any data. Not sure why. I've been trying to resolve it but not having any luck.

            • 3. Re: Utilizing Get-View to export results to CSV file
              LucD Guru
              Community WarriorsvExpertUser Moderators

              Try with this version

               

              #CSV Path and file name to be created

              $ExportFilePath = "c:\temp\VMInfo-MultipleVCs.csv"

              #Connect to the multiple vCenters

              Write-Host "Connecting to vCenters" -ForegroundColor Cyan

              $VIServerNames = Get-Content -Path C:\temp\vcenters.txt

              $c = Get-Credential

              $VIServers = Connect-VIServer -Server $VIServerNames -Credential $c  -warningaction 0


              $table = @()


              ForEach ($VIServer in $global:DefaultVIServers) {

                 Write-Host "Gathering Statistics for VMs in $($VIServer.Name)"

                 $vms = Get-VM -Server $VIServer

                 ForEach ($vm in $VMs) {

                 $row = "" | Select VIServer,Name,DNS_Name,OS,PowerState,Template_Status,IPAddress,LastBootTime,Notes

                 $row.VIServer = $VIServer.Name

                 $row.Name = $vm.Name

                 $row.DNS_Name = $vm.Guest.ExtensionData.HostName

                 $row.OS = $vm.Guest.OSFullName

                 $row.PowerState = $vm.PowerState

                 $row.Template_Status = $vm.ExtensionData.Summary.Config.Template

                 $row.IPAddress = $vm.Guest.IpAddress -join '|'

                 $row.LastBootTime = $vm.ExtensionData.Summary.Runtime.BootTime

                 $row.Notes = $vm.ExtensionData.config.annotation

                 $table += $row

                 }

              }

              $table | Export-Csv $ExportFilePath -NoTypeInformation

              Disconnect-VIServer -Confirm:$False

              • 4. Re: Utilizing Get-View to export results to CSV file
                blueten Lurker

                That's worked.

                 

                Thanks so much for your help. I really appreciate it!!

                 

                • 5. Re: Utilizing Get-View to export results to CSV file
                  LucD Guru
                  Community WarriorsUser ModeratorsvExpert

                  You're welcome.
                  If you have further questions, feel free to ask