Powershell Help

Soldato
Joined
27 Sep 2004
Posts
11,191
Location
The Ledge Beyond The Edge
Hi,

I'm trying to run a powershell script that checks the free space on server drives and only reports back on ones with more than 75% used. I can get it to report the information from the drives, but i can't get it to filter on anything over 75% or sort it.

Here is the function i have

Code:
Function GetDiskSpace
{

    Foreach ($s in $servers)

    {

        $FreeGB = @{Name="FreeSpace(GB)";expression={[math]::round(($_.FreeSpace / 1073741824),2)}}
        $TotalGB = @{Name="Capacity(GB)";expression={[math]::round(($_.Capacity/ 1073741824),2)}}
        $UsedGB = @{Name="Volume Used(GB)";expression={[math]::round(($_.Capacity/ 1073741824) - ($_.FreeSpace / 1073741824),0)}}        
        $UsedPerc = @{Name="Used %";Expression={[math]::round((((($_.Capacity/ 1073741824) - ($_.FreeSpace / 1073741824)) / ($_.Capacity / 1073741824)) * 100),0)}}
        
     
        Get-WmiObject -Class win32_volume -cn $s  | 
        Select-Object @{LABEL='Comptuer';EXPRESSION={$s}},driveletter, label,$FreeGB, $TotalGB,$UsedGB,$UsedPerc
        
    } #end foreach $s
}

I have tried running an additional pipe after the select-object with

Code:
where-object { $UsedPerc -gt 75 }

But it errors with Cannot compare "System.Collections.Hashtable" because it is not IComparable.

Any tips please?? :)
 
Soldato
OP
Joined
27 Sep 2004
Posts
11,191
Location
The Ledge Beyond The Edge
The computer was just a label so not really bothered.

Anyway, i have changed it totally to the following

Code:
Function DiskSpace
{
    
    Get-WmiObject -ComputerName SERVER1-Class Win32_volume |
    Sort-Object  @{Expression={'{0:P0}' -F (1 -  ($_.FreeSpace/$_.Capacity))}}  |
    ft DriveLetter, 
       Label,
       @{Label="Server";Expression={$_.SystemName}},
       @{Label="Free (GB)";Expression={'{0:N0}' -F ($_.FreeSpace/1GB)}},
       @{Label="Used (%)";Expression={'{0:P0}' -F (1 -  ($_.FreeSpace/$_.Capacity))}}
       
       
}

DiskSpace

But now my sort-object is showing the order as 1,10,2,20 etc instead of 1,2,10,20

Is there a way i can sort that on the fly?

I tried doing the following
Code:
Sort-Object  @{Expression={'{0:P0}' -F (1 -  ($_.FreeSpace/$_.Capacity)).toint32()}}

but it didn't like it :(
 
Back
Top Bottom