PowerShell Defrag Analysis returning NULL values
$computer = "Server"
$drives = Invoke-Command -ComputerName $computer -ScriptBlock { Get-PSDrive -PSProvider FileSystem | Select-Object @{n = "Name"; e = { $_.Root.TrimEnd("\") } } }
$drives.Name | ForEach-Object {
Write-Host $_
Invoke-Command -ComputerName $computer -ScriptBlock { param($_)
$volume = Get-WmiObject -Class Win32_Volume -Filter "DriveLetter = '$_'"
Write-Host $volume
$fragmentation = $volume.DefragAnalysis()
$Props = @{
FragmentationStatusFor = $($volume.Name)
TotalFragmentedFiles = $($fragmentation.TotalFiles)
TotalFragmentedBytes = $($fragmentation.TotalBytes)
}
New-Object -TypeName PSObject -Property $Props
} -ArgumentList $_, $volume
} | Select-Object * | Write-DbaDataTable -SqlInstance SomeServer -Database SomeDB -Schema GET -Table DbaDefragAnalysis -AutoCreateTable -Confirm -KeepNulls -bulkCopyTimeOut 180 -ErrorAction Continue -Verbose -EnableException;
TotalFragmentedFiles returns no value TotalFragmentedBytes returns no value
You are trying to retrieve a property of the Win32_DefragAnalysis
WMI class that do not exist, check the Microsoft Documentation for Win32_DefragAnalysis, there is no property called TotalBytes
, that's why you are getting no return values.
To get the values that you are trying to fetch, you should probably follow this article of the MS documentation