2 Replies Latest reply on May 23, 2019 5:07 PM by AlbertWT

    Unable to pull the Host, Cluster, IPAddress and Disk Used in GB for VM report?

    AlbertWT Master

      Hi All,

       

      I need some help in modifying and fixing the below script to display the column result properly in.CSV:

       

      The column Host, Cluster, IPAddress and Disk Used in GB somehow is not displaying at all in the exported .CSV

       

      $VCServerName = "PRDVC03-VM"
      Connect-VIServer $VCServerName
      $ExportFilePath = "C:\RESULT\Export-VMInfo_$($VC).csv"
      $Report = @()
      $VMs = Get-Datacenter | Get-VM
      $Datastores = Get-Datastore | Select-Object Name, Id
      $VMHosts = Get-VMHost | Select-Object Name, Parent
      ForEach ($VM in $VMs) {
      $VMView = $VM | Get-View
      $VMInfo = {} | Select-Object VMName,Powerstate,OS,Folder,IPAddress,ToolsStatus,Host,Cluster,Datastore,NumCPU,MemMb,DiskGb, DiskFree, DiskUsed
      $VMInfo.VMName = $vm.name
      $VMInfo.Powerstate = $vm.Powerstate
      $VMInfo.OS = $vm.Guest.OSFullName
      $VMInfo.IPAddress = $vm.Guest.IPAddress.ToString()
      $VMInfo.ToolsStatus = $VMView.Guest.ToolsStatus
      $VMInfo.Host = $vm.host.name
      $VMInfo.Cluster = $vm.host.Parent.Name
      $VMInfo.Datastore = ($Datastores | Where-Object {$_.ID -match (($vmview.Datastore | Select-Object -First 1) | Select-Object Value).Value} | Select-Object Name).Name
      $VMInfo.NumCPU = $vm.NumCPU
      $VMInfo.MemMb = [Math]::Round(($vm.MemoryMB),2)
      $VMInfo.DiskGb = [Math]::Round((($vm.HardDisks | Measure-Object -Property CapacityKB -Sum).Sum * 1KB / 1GB),2)
      $VMInfo.DiskFree = [Math]::Round((($vm.Guest.Disks | Measure-Object -Property FreeSpace -Sum).Sum / 1GB),2)
      $VMInfo.DiskUsed = $VMInfo.DiskGb - $VMInfo.DiskFree
      $Report += $VMInfo
      }
      $Report = $Report | Sort-Object VMName
      IF ($Report -ne "") {
      $report | Export-Csv $ExportFilePath -NoTypeInformation
      }
      $VC = Disconnect-VIServer -Confirm:$False

       

      Thanks in advance.

        • 1. Re: Unable to pull the Host, Cluster, IPAddress and Disk Used in GB for VM report?
          LucD Guru
          User ModeratorsvExpertCommunity Warriors

          Try with these changes I made.

           

          $VCServerName = "PRDVC03-VM"

          Connect-VIServer $VCServerName

          $ExportFilePath = "C:\RESULT\Export-VMInfo_$($VC).csv"

          $Report = @()

          $VMs = Get-Datacenter | Get-VM

          $Datastores = Get-Datastore | Select-Object Name, Id

          $VMHosts = Get-VMHost | Select-Object Name, Parent

          ForEach ($VM in $VMs)

          {

             $VMView = $VM | Get-View

             $VMInfo = { } | Select-Object VMName, Powerstate, OS, Folder, IPAddress, ToolsStatus, Host, Cluster, Datastore, NumCPU, MemMb, DiskGb, DiskFree, DiskUsed

             $VMInfo.VMName = $vm.name

             $VMInfo.Powerstate = $vm.Powerstate

             $VMInfo.OS = $vm.Guest.OSFullName

             $VMInfo.IPAddress = $vm.Guest.IPAddress -join ' | '

             $VMInfo.ToolsStatus = $VMView.Guest.ToolsStatus

             $VMInfo.Host = $vm.VMhost.name

             $VMInfo.Cluster = $vm.VMhost.Parent.Name

             $VMInfo.Datastore = ($Datastores | Where-Object { $_.ID -match (($vmview.Datastore | Select-Object -First 1) | Select-Object Value).Value } | Select-Object Name).Name

             $VMInfo.NumCPU = $vm.NumCPU

             $VMInfo.MemMb = [Math]::Round(($vm.MemoryMB), 2)

             $VMInfo.DiskGb = [Math]::Round(((Get-Harddisk -VM $vm | Measure-Object -Property CapacityKB -Sum).Sum * 1KB / 1GB), 2)

             $VMInfo.DiskFree = [Math]::Round((($vm.Guest.Disks | Measure-Object -Property FreeSpace -Sum).Sum / 1GB), 2)

             $VMInfo.DiskUsed = $VMInfo.DiskGb - $VMInfo.DiskFree

             $Report += $VMInfo

          }

          $Report = $Report | Sort-Object VMName

          IF ($Report -ne "")

          {

             $report | Export-Csv $ExportFilePath -NoTypeInformation

          }

          $VC = Disconnect-VIServer -Confirm:$False

          1 person found this helpful
          • 2. Re: Unable to pull the Host, Cluster, IPAddress and Disk Used in GB for VM report?
            AlbertWT Master

            Many thanks, LucD for your assistance.