Description

I thought I had uploaded this before.

I saw the class from the HeyScriptingGuy post:

http://blogs.technet.com/b/heyscriptingguy/archive/2011/03/15/use-powershell-to-determine-fragmentation-of-your-drive.aspx

I wrote this script to check the Defragmentation status on many servers and then report the status.

I left in -credential remove it if you are connecting to the same domain.

This is how I use the cmdlet/function in a script, which is in a scheduled task to run once per week.

$HomeDir = "C:\ps\DefragInfo" 
Import-Csv -Path $HomeDir\Servers.csv | Foreach-Object {$_.Computer} |
Test-Online | Get-VolumeFragmentation | Where-Object {$_.DefragRecommended -eq $True} |
Sort-Object TotalPercentFrag | Export-Csv -Path $HomeDir\DefragInfo.csv -NoTypeInformation

Test-Online is available here (it just pings each machine before connecting)

http://gallery.technet.microsoft.com/scriptcenter/2789c120-48cc-489b-8d61-c1602e954b24

 

If you are looking for Volume information without the Defrag info, this will run a lot faster:

http://gallery.technet.microsoft.com/scriptcenter/74884d85-c0b5-446a-be04-3d411a6dce2f

 

Script

PowerShell
Edit|Remove
function Get-VolumeFragmentation { 
 
#Requires -Version 2.0 
[CmdletBinding()] 
 Param  
   ([Parameter(Mandatory=$true, 
               Position=0, 
               ValueFromPipeline=$true, 
               ValueFromPipelineByPropertyName=$true)] 
    [String[]]$ComputerName, 
    [Parameter(Mandatory=$false, 
               Position=1, 
               ValueFromPipeline=$true, 
               ValueFromPipelineByPropertyName=$true)] 
    # Receive-Credential available for download   
    # http://gallery.technet.microsoft.com/scriptcenter/2ff11fd5-67f6-44e4-8816-28edb984d63a 
    $Creds = (Receive-Credential -NYUMC) 
   )#End Param 
Begin 
{ 
    Write-Host "`nRetrieving Volume Fragmentation Info . . ." 
} 
Process 
{ 
  $ComputerName | ForEach-Object { 
        $Computer = $_ 
        Get-WmiObject -class "win32_volume" -namespace "root\cimv2" -computername $Computer -Credential $Creds |  
        Where-Object {$_.name -notmatch "\\\\" -and $_.DriveType -eq "3"|  
        ForEach { 
                $defragresult = $_.DefragAnalysis() 
                $Item = $defragresult.DefragAnalysis 
                $hash =  @{ 
                    ComputerName          = $Computer 
                    VolumeName        = $Item.VolumeName 
                    VolumeSizeGB      = [Math]::Round($Item.VolumeSize/1GB,2) 
                    UsedSpaceGB       = [Math]::Round($Item.UsedSpace/1GB,2) 
                    FreeSpaceGB       = [Math]::Round($Item.FreeSpace/1GB,2) 
                    FreeSpacePercent  = $Item.FreeSpacePercent 
                    TotalPercentFrag  = $Item.TotalPercentFragmentation 
                    DefragRecommended = $defragresult.DefragRecommended 
                    FileSystem        = $_.FileSystem 
                    DriveLetter       = $_.DriveLetter 
                } 
                New-Object PSOBJECT -Property $hash         
        } | Select ComputerName, Driveletter , Filesystem, VolumeName, VolumeSizeGB, UsedSpaceGB ,FreeSpaceGB, FreeSpacePercent, TotalPercentFrag, DefragRecommended         
    } 
}#Process 
End 
{ 
 
}#End 
 
}#Get-VolumeFragmentation