VMware Cloud Community
bradley4681
Expert
Expert
Jump to solution

Guest Drive Space

I have a report i've been using for sometime now but i was asked to add some additional information, drive space and ESX host the guest is currently running on. I was trying to cobble together 2 scripts I had into one since I had another script that did just drive space but it's not working, suggestions?

$viservers = "myserver"


$HostReport = @()

foreach ($singleViserver in $viservers)
{

    Connect-VIServer $singleViserver -user USERNAME -password 'PASSWORD'
  
         $VCreport = @()
    Get-VM |Get-View | %{
        $Report = "" | select Name, VMuuid, ESXhost, VMos, CPU, RAM, IP, FQDN, Status, Customer, vServer
        $Report.vServer = $defaultVIServer
        $vmImpl = $_
        $Report.Name = $_.Name
        $Report.VMuuid = $_.Summary.Config.uuid
        $Report.ESXhost = $_.Runtime.Host.Name
        $Report.VMos = $_.Summary.Config.GuestFullName
        $Report.CPU = $_.Summary.Config.numCPU
        $Report.RAM = $_.Summary.Config.memorySizeMB
        $Report.IP = $_.Guest.IPAddress
        $Report.FQDN = $_.Guest.HostName
        $Report.Status = $_.Runtime.PowerState
        $Report.Customer = (Get-VIObjectByVIView $_).CustomFields["Customer"]
       
        $i = 0
    $disks = ($vmImpl | Get-VMGuest).Disks
    foreach($hd in (Get-HardDisk -VM $vmImpl)){
        $row | Add-Member -Name ("Disk " + $i + " Name" ) -Value $hd.Name -MemberType NoteProperty
        $row | Add-Member -Name ("Disk " + $i + " Path" ) -Value $hd.Filename -MemberType NoteProperty
        $row | Add-Member -Name ("Disk " + $i + " Root" ) -Value (&{if($disks){$disks[$i].Path}}) -MemberType NoteProperty
        $row | Add-Member -Name ("Disk " + $i + " Size (GB)" ) -Value ($hd.CapacityKB/1MB) -MemberType NoteProperty
        $i++
    }
   
        $HostReport += $Report
        $VCreport += $Report

    }
    #Disconnects after each vCenter
    Disconnect-VIServer -Confirm:$False
          $VCreport

}

$HostReport | Export-Csv ".\Full-HostReport.csv" -Delimiter "|" –NoTypeInformation

Cheers! If you found this or other information useful, please consider awarding points for "Correct" or "Helpful".
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Try it like this

$viservers = "myserver"

$HostReport = @()

foreach ($singleViserver in $viservers)
{

    Connect-VIServer $singleViserver -user USERNAME -password 'PASSWORD'
    $VCreport = @()
    Get-VM |%{
        $Report = "" | select Name, VMuuid, ESXhost, VMos, CPU, RAM, IP, FQDN, Status, Customer, vServer
        $Report.vServer = $defaultVIServer
        $Report.Name = $_.Name         $Report.VMuuid = $_.Extensiondata.Summary.Config.uuid         $Report.ESXhost = $_.Host.Name         $Report.VMos = $_.Extensiondata.Summary.Config.GuestFullName         $Report.CPU = $_.Extensiondata.Summary.Config.numCPU         $Report.RAM = $_.Extensiondata.Summary.Config.memorySizeMB         $Report.IP = $_.Extensiondata.Guest.IPAddress         $Report.FQDN = $_.Extensiondata.Guest.HostName         $Report.Status = $_.Extensiondata.Runtime.PowerState #         $Report.Customer = (Get-VIObjectByVIView $_).CustomFields["Customer"]
       $i = 0
        $disks = ($_ | Get-VMGuest).Disks         foreach($hd in (Get-HardDisk -VM $_)){             $Report | Add-Member -Name ("Disk " + $i + " Name" ) -Value $hd.Name -MemberType NoteProperty             $Report | Add-Member -Name ("Disk " + $i + " Path" ) -Value $hd.Filename -MemberType NoteProperty             $Report | Add-Member -Name ("Disk " + $i + " Root" ) -Value (&{if($disks){$disks[$i].Path}}) -MemberType NoteProperty             $Report | Add-Member -Name ("Disk " + $i + " Size (GB)" ) -Value ($hd.CapacityKB/1MB) -MemberType NoteProperty             $i++        }         $HostReport += $Report
       
$VCreport += $Report     } #Disconnects after each vCenter
   Disconnect-VIServer -Confirm:$False
    $VCreport } $HostReport | Export-Csv ".\Full-HostReport.csv" -Delimiter "|" –NoTypeInformation


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

0 Kudos
9 Replies
LucD
Leadership
Leadership
Jump to solution

Try it like this

$viservers = "myserver"

$HostReport = @()

foreach ($singleViserver in $viservers)
{

    Connect-VIServer $singleViserver -user USERNAME -password 'PASSWORD'
    $VCreport = @()
    Get-VM |%{
        $Report = "" | select Name, VMuuid, ESXhost, VMos, CPU, RAM, IP, FQDN, Status, Customer, vServer
        $Report.vServer = $defaultVIServer
        $Report.Name = $_.Name         $Report.VMuuid = $_.Extensiondata.Summary.Config.uuid         $Report.ESXhost = $_.Host.Name         $Report.VMos = $_.Extensiondata.Summary.Config.GuestFullName         $Report.CPU = $_.Extensiondata.Summary.Config.numCPU         $Report.RAM = $_.Extensiondata.Summary.Config.memorySizeMB         $Report.IP = $_.Extensiondata.Guest.IPAddress         $Report.FQDN = $_.Extensiondata.Guest.HostName         $Report.Status = $_.Extensiondata.Runtime.PowerState #         $Report.Customer = (Get-VIObjectByVIView $_).CustomFields["Customer"]
       $i = 0
        $disks = ($_ | Get-VMGuest).Disks         foreach($hd in (Get-HardDisk -VM $_)){             $Report | Add-Member -Name ("Disk " + $i + " Name" ) -Value $hd.Name -MemberType NoteProperty             $Report | Add-Member -Name ("Disk " + $i + " Path" ) -Value $hd.Filename -MemberType NoteProperty             $Report | Add-Member -Name ("Disk " + $i + " Root" ) -Value (&{if($disks){$disks[$i].Path}}) -MemberType NoteProperty             $Report | Add-Member -Name ("Disk " + $i + " Size (GB)" ) -Value ($hd.CapacityKB/1MB) -MemberType NoteProperty             $i++        }         $HostReport += $Report
       
$VCreport += $Report     } #Disconnects after each vCenter
   Disconnect-VIServer -Confirm:$False
    $VCreport } $HostReport | Export-Csv ".\Full-HostReport.csv" -Delimiter "|" –NoTypeInformation


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
bradley4681
Expert
Expert
Jump to solution

looks like it all works except the customer field...

$Report.Customer = (Get-VIObjectByVIView $_).CustomFields["Customer"]
Cheers! If you found this or other information useful, please consider awarding points for "Correct" or "Helpful".
0 Kudos
RvdNieuwendijk
Leadership
Leadership
Jump to solution

Try:

$Report.Customer = $_.CustomFields["Customer"]


Regards, Robert

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition
bradley4681
Expert
Expert
Jump to solution

also just noticed that its only exporting information on hard drisk 1 & 2, instead of 0,1,2,3,4, etc

Cheers! If you found this or other information useful, please consider awarding points for "Correct" or "Helpful".
0 Kudos
bradley4681
Expert
Expert
Jump to solution

that works, but still the issue with not exporting all the drive info

Cheers! If you found this or other information useful, please consider awarding points for "Correct" or "Helpful".
0 Kudos
LucD
Leadership
Leadership
Jump to solution

That's because the Export-Csv takes the first row in the report to calculate how many columns need to be in the CSV. Now if the first VM only has 2harddisks, the CSV will only have columns for 2 hard disks.  One solution is to take the $report array before you export it and order it by number of columns in descending order.

$HostReport = $HostReport | Sort-Object -Property {($_ | gm).count} -Descending

$HostReport | Export-Csv "c:\report.Csv" -NoTypeinfomation


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
RvdNieuwendijk
Leadership
Leadership
Jump to solution

I have tried the script and it returns only the hard disks that really exist. The hard disks normally start counting at "Hard disk 1". If a virtual machine has four hard disks, the script returns "Hard disk 1" up to "Hard disk 4". If a virtual machine has only two hard disks the script only returns "Hard disk 1" and "Hard disk 2".

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition
0 Kudos
bradley4681
Expert
Expert
Jump to solution

This actually seemed to work out better.

$HostReport | Sort-Object -Property {($_ | gm -Name "Disk * Name").Count} -Descending | Export-Csv ".\Full-HD-HostReport.csv" -Delimiter "|" -NoTypeInformation

Cheers! If you found this or other information useful, please consider awarding points for "Correct" or "Helpful".
0 Kudos
LucD
Leadership
Leadership
Jump to solution

That is indeed more the PowerShell way, a clean pipe-construct without any intermediate variables.

I know some people that would like if all script were like that, one long pipe construction Smiley Wink


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos