Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remote computer disk space report

Here is my code for gathering disk space for all remote drives:

$Computers = (Get-ADComputer -Filter {(OperatingSystem -like "*windows*")}-Server $domain).dnshostname

$report1 = Get-WmiObject Win32_LogicalDisk -computer $computers -Credential $Creds | Select SystemName,DeviceID,VolumeName,@{Name="Size(GB)";Expression={"{0:N1}" -f($_.size/1gb)}},@{Name="FreeSpace(GB)";Expression={"{0:N1}" -f($_.freespace/1gb)}}

And I get...

enter image description here

I want to arrange output so each machine is listed once on the left with drives in columns along the top:

enter image description here

Anyone know how?

like image 634
ahmoreish Avatar asked Aug 18 '26 20:08

ahmoreish


2 Answers

As mentioned by @Lee_Daily's comment you can use the Group-Object cmdlet to create a unique group for every SystemName. Below example groups the objects in $report1 by the SystemName property. Group-Object returns a GroupInfo object (or an array of GroupInfo's ), where we select the content of the Group property (SystemName is also included in the Group property). The Group property is a HashSet that can be converted via ConvertTo-Csv.

 $peport1 | Group-Object SystemName | Select -ExpandProperty Group | ForEach-Object { $_ | ConvertTo-Csv  -NoTypeInformation }

You can replace ConvertTo-Csv with Export-Csv:

 $peport1 | Group-Object SystemName | Select -ExpandProperty Group | ForEach-Object { $_ | Export-Csv "repor1.csv"  -NoTypeInformation -Append }

-Append will append several rows, where each row is one entry of the GroupInfo array.

Documentation about the Group-Object cmdlet can be found here.

Hope that helps.

like image 107
Moerwald Avatar answered Aug 20 '26 11:08

Moerwald


here is a somewhat different approach. it requires that you wrap the following code in a scriptblock and then use Invoke-Command to run it on the target systems. if you can't get that working, please let me know ... [grin]

# if you have no locally mapped/subst-ed drive letters, remove this section
$SubstLocalDiskList = @(subst.exe |
    ForEach-Object {
        $_[0]
        })
$DiskList = Get-PSDrive -PSProvider FileSystem |
    Where-Object {
        # read-only drives will show "0" used & "0" free 
        $_.Free -gt 0 -and
        $_.Used -gt 0 -and
        # if you have no locally mapped/subst-ed drive letters, remove this line
        $_.Name -notin $SubstLocalDiskList
        }

$FreeWarning_Pct = 10
$TempPropTable = [ordered]@{
    ComputerName = $env:COMPUTERNAME
    }
foreach ($DL_Item in $DiskList)
    {
    $Size = $DL_Item.Used + $DL_Item.Free
    $SizeFree_Pct = [math]::Round($DL_Item.Free / $Size * 100, 2)
    if ($SizeFree_Pct -ge $FreeWarning_Pct)
        {
        $SFP_Status = 'OK'
        }
        else
        {
        $SFP_Status = '__ Low __'
        }

    $TempPropTable.Add('{0}_Drive' -f $DL_Item.Name, $DL_Item.Name)
    $TempPropTable.Add('{0}_Size_GB' -f $DL_Item.Name, [math]::Round($Size / 1GB, 2))
    $TempPropTable.Add('{0}_Free_Pct' -f $DL_Item.Name, $SizeFree_Pct)
    $TempPropTable.Add('{0}_SFP_Status' -f $DL_Item.Name, $SFP_Status)

    }

[PSCustomObject]$TempPropTable

truncated output for one system ...

ComputerName C_Drive C_Size_GB C_Free_Pct C_SFP_Status D_Drive D_Size_GB D_Free_Pct D_SFP_Status E_Drive [*...snip...*] 
------------ ------- --------- ---------- ------------ ------- --------- ---------- ------------ ------- [*...snip...*] 
[MySysName]  C          931.41      79.22 OK           D          930.57      49.68 OK           E       [*...snip...*]
like image 34
Lee_Dailey Avatar answered Aug 20 '26 09:08

Lee_Dailey



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!