WINDOWS SERVER VOLUME FRAGMENTATION PERCENT MONITOR using SAM

Version 1

    Hi all. I am not a powershell script person so it has taken me a week to get this working. It gets the fragmentation percent using the powershell script monitor in x64 and remote host mode. This one is for C and D only. If you have suggestions to make it better please share.

    You have to get remote powershell working to the external node.

     

    $dfaC = Invoke-WmiMethod -Path ((Get-WmiObject -Class Win32_Volume -Filter “DriveLetter = 'C:'”).__PATH) -Name DefragAnalysis

    $VolC = $dfaC | %{$_.DefragAnalysis | add-member -membertype noteproperty DefragRecommended $_.DefragRecommended -passThru | add-member -membertype noteproperty vname $_.vname -passThru} | Out-String

    $ResultC = $VolC -match 'FilePercentFragmentation\s*:\s(\d*)'

    $PercentC = $matches[1]

     

     

    $dfaD = Invoke-WmiMethod -Path ((Get-WmiObject -Class Win32_Volume -Filter “DriveLetter = 'D:'”).__PATH) -Name DefragAnalysis

    $VolD = $dfaD | %{$_.DefragAnalysis | add-member -membertype noteproperty DefragRecommended $_.DefragRecommended -passThru | add-member -membertype noteproperty vname $_.vname -passThru} | Out-String

    $ResultD = $VolD -match 'FilePercentFragmentation\s*:\s(\d*)'

    $PercentD = $matches[1]

     

     

    if ($PercentC -and $PercentD) {Write-Host "Message.C: Executes 1 time daily"; Write-host "Statistic.C: $PercentC"; Write-Host "Message.D: Executes 1 time daily"; Write-host "Statistic.D: $PercentD" ; Exit 0;}

    Else

    {Write-Host "Message.C: Command failed to run!"; Write-Host "Statistic.C: 1"; "Message.D: Command failed to run!"; Write-Host "Statistic.D: 1" Exit 3;}