I’ve been banging my ahead away at this one since I’m still a beginner in my opinion. When I run the script it just says completed even thought defrag did not kick off.
- I’d like to add a .txt file with machines to run this against. Thanks to anyone that can shed some light on what I’m doing wrong
- I was also hoping to not just run this again the c drive I was hoping to run it on all drives
- I’ve also tried running the script from within powershell and also from the PS prompt and still nothing…
- I’m grateful for any help I can get.
function Start-Defrag {
[CmdletBinding(SupportsShouldProcess=$true)]
param ([string]$computer=“.”,
[string]$drive
)
if ($drive -notlike “c:”){ # anyway to do all drives?#
Throw “Drive should be submitted as letter and colon e.g. C:”}
$filt = "Name='" + $drive + "\\'"
$vol = Get-WmiObject -Class Win32_Volume -Filter $filt -ComputerName $computer
-ComputerName $computer
$res = $vol.Defrag($false)
if ($res.ReturnValue -eq 0) {
Write-Host "Defrag succeeded"
$res.DefragAnalysis |
Format-List AverageFileSize, AverageFragmentsPerFile,
AverageFreeSpacePerExtent, ClusterSize,
ExcessFolderFragments, FilePercentFragmentation,
FragmentedFolders, FreeSpace, FreeSpacePercent,
FreeSpacePercentFragmentation,
LargestFreeSpaceExtent, MFTPercentInUse,
MFTRecordCount, PageFileSize, TotalExcessFragments,
TotalFiles, TotalFolders, TotalFragmentedFiles,
TotalFreeSpaceExtents, TotalMFTFragments,
TotalMFTSize, TotalPageFileFragments,
TotalPercentFragmentation, TotalUnmovableFiles,
UsedSpace, VolumeName, VolumeSize
}
else {Write-Host "Defrag failed Result code: " $res.ReturnValue}
}