
close
close
Chance to win $250 in Petri 2023 Audience Survey
In a previous PowerShell Problem Solver, I tackled the problem of documenting something that isn’t there, namely a specific service. The original online post I came across had a second problem that falls into the same category. Although it’s probably true that all volumes on all servers are configured for NTFS, he had to prove it. I took this also as a challenge to identify volumes and servers that don’t meet the criteria. As with all of these articles, pay more attention to techniques and ideas than necessarily to the final result.
As in my previous article, I have a variable with a list of server names that I know are running and that I can access. You would need to include error handling into your solution, but I don’t want that to complicate the situation or distract you. I’ve also decided that I’m only interested in logical drives and not necessarily all volumes. This means I will use the Win32_Logicaldisk class with Get-CimInstance, but you could easily use Win32_Volume in its place.
I’ll start with a simple command like this:
Get-CimInstance win32_logicaldisk -computer $servers | select Caption,FileSystem,Volumename,PSComputername
Listing logical disks with WMI and PowerShell (Image Credit: Jeff Hicks)
Get-CimInstance win32_logicaldisk -computer $servers -filter "Drivetype=3" | select DeviceID,FileSystem,Volumename,PSComputername
Filtered results (Image Credit: Jeff Hicks)
Get-CimInstance win32_logicaldisk -computer $servers -filter "Drivetype=3" | Sort FileSystem,DeviceID,PSComputername | Format-Table -GroupBy FileSystem -Property DeviceID,Volumename,PSComputername
The end result is a formatted table grouped by the FileSystem property. I always recommend sorting on the property prior to formatting to get the best results.
Grouped and Formatted results with PowerShell (Image Credit: Jeff Hicks)
Get-CimInstance win32_logicaldisk -computer $servers -filter "Drivetype=3 AND FileSystem<>'NTFS'" | select DeviceID,FileSystem,Volumename,PSComputername
Remember that with WMI filters we use the legacy operators like the equal sign instead of the newer PowerShell operators. The output is now limited to potential problems.
Finding the odd result (Image Credit: Jeff Hicks)
Get-CimInstance win32_logicaldisk -computer $servers -filter "DeviceID = 'C:'" | select DeviceID,FileSystem,PSComputername
Listing drive C and NTFS (Image Credit: Jeff Hicks)
Get-CimInstance win32_logicaldisk -computer $servers -filter "DeviceID = 'C:' AND FileSystem<>'NTFS'" | select DeviceID,FileSystem,PSComputername
In my case, I get no results, which is a good thing.
To wrap things up, let’s bring back a concept from the previous article. The scenario now is that all of my servers should have a fixed disk for drive D. I need to verify which servers have this drive. It’s simple enough to find those that have the drive.
Get-CimInstance win32_logicaldisk -computer $servers -filter "DeviceID = 'D:' AND Drivetype=3"
Listing Drive D (Image Credit: Jeff Hicks)
$servers | foreach { if (Get-CimInstance win32_logicaldisk -computer $_ -filter "DeviceID = 'D:' AND Drivetype=3") { $Verified = $True } else { $Verified = $False } [pscustomobject]@{ Computername = $_ Verified = $Verified } } | Sort Verified
With equally useful results:
Verified Results (Image Credit: Jeff Hicks)
$servers | where { -NOT (Get-CimInstance win32_logicaldisk -computer $_ -filter "DeviceID = 'D:' AND Drivetype=3") }
If the Get-CimInstance command fails, then it won’t write an object to the pipeline, which would be a $False result. Using –Not turns it around to $True so that the computername is passed on to the pipeline.
Negative filtering with Where-Object and -Not (Image Credit: Jeff HIcks)
More in PowerShell
Most popular on petri