Powershell Script to get the allocated and used space of all the disk drives attcahed to an Hyper-V machine in different columns in an Excel. RRS feed

  • Question

  • Hi,

    I am trying to get the Disk rives attached to all the VMs under different Hosts along with their allocated and used space in individual columns in an excel sheet. Below is the script I am using. However the excel displays only 'System.Object[]' under "Maximum_Disk_Size_inGB" and "Used_Disk_Size_inGB" columns. I am providing the part of code trying to fetch this data. Please let me know your inputs. 

      < -----Script------>

    $vm = Get-SCVirtualMachine TESTHPVM | Get-SCVirtualHardDisk

    [array]$ServiceReport = @()

    Foreach($Item in $vm)

       $Maximum = ($Item.MaximumSize /1024 /1024 /1024)
       $Size = ($Item.Size /1024 /1024 /1024)
       $array_list_max+= $Maximum -join ','
       $array_list_used+= $Size -join ','


     $DiskReport = New-Object -TypeName PSObject 

     For($i=0;$i -lt $array_list_max.Count;$i++)
        $DiskReport| Add-Member NoteProperty -Name "Maximum_Disk_Size_inGB" -Value $array_list_max -Force 


     For($i=0;$i -lt $array_list_used.Count;$i++)

            $DiskReport| Add-Member NoteProperty -Name "Used_Disk_Size_inGB" -Value $array_list_used -Force


     $Servicereport = $Servicereport + $DiskReport

     $ServiceReport | Export-Csv -Path "D:\Scripts\ServiceReport.csv" -NoTypeInformation

     $FinalReport = "D:\Scripts\ServiceReport.csv"

      Write-Host "Disk report : $FinalReport" 
    • Moved by Bill_Stewart Monday, October 2, 2017 9:48 PM This is not "fix/debug/rewrite this script I found on the Internet" forum
    Thursday, August 24, 2017 9:37 AM

All replies

  • Why so complicated?

    Get-SCVirtualMachine | 
        Get-SCVirtualHardDisk |
            Select-Object -Property Name,
                @{Name='MaximumSizeGB';Expression={[MATH]::Round($_.MaximumSize /1GB,2)}},
                @{Name='SizeGB';Expression={[MATH]::Round($_.size /1GB,2)}},
                Location |
                    Format-Table -AutoSize

    Instead of Format-Table you can use Export-CSV  ;-)

    .. and please format your code as code with the according option on the edit bar.


    Grüße - Best regards

    PS:> (79,108,97,102|%{[char]$_})-join''

    • Edited by BOfH-666 Thursday, August 24, 2017 2:10 PM
    Thursday, August 24, 2017 2:09 PM