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
Joined
1 Apr 2014
Posts
18,865
Location
Aberdeen
You've got a typo in the select-object line: Computer, not Comptuer. And shouldn't the Get-WmiObject command be at the start of the loop rather than the end?
 
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 :(
 
Associate
Joined
2 Jul 2003
Posts
2,442
Probably better ways of doing it but I like to keep things as easy to read as possible!

Can export the second array $OverLimit as it should have any drivers over 75%

Assumes the account running has permission to access the servers. If not will need to use the -credential parameter which is commented out at the moment.
Also ignores the A and D drives for what it's worth!

Code:
$ServerList = Get-Content -Path "C:\Scripts\DiskSpace\ServerList.txt" 

$DriveList = @()
$OverLimit = @()

ForEach ($Server in $ServerList)
{
    $Disks = Get-WmiObject Win32_LogicalDisk -ComputerName $Server #-Credential $Cred

    ForEach ($Disk in $Disks)
    {
        If ($Disk.DeviceID -Ne "A:" -And $Disk.DeviceID -Ne "D:")
        {
            $Row = "" | Select ServerName, DeviceID, VolumeName, Size, FreeSpace, Used, UsedPercentage
    
            $Row.ServerName = $Server
            $Row.DeviceID = $Disk.DeviceID
            $Row.VolumeName = $Disk.VolumeName
            $Row.Size = [Math]::Round($Disk.Size / 1GB, 2) #Convert to gb
            $Row.FreeSpace = [Math]::Round($Disk.FreeSpace / 1GB, 2) #Convert to gb
            $Row.Used = $Row.Size - $Row.FreeSpace
            If ($Row.Used -gt 0)
            {
                $Row.UsedPercentage = [Math]::Round(100-(($Row.FreeSpace/$Row.Size)*100), 2)
            }   
    
            $DriveList += $Row
        }
    }
}

$OverLimit = $DriveList | Where {$_.UsedPercentage -Gt 75}

$DriveList | Format-Table -AutoSize
$OverLimit | Format-Table -AutoSize
 
Back
Top Bottom