• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

PowerShell help.

smakme7757

Golden Member
Ok, i am trying to use the output of:
Code:
$Volume = Get-Volume
$Volume.DriveLetter
Which is
C
E
G
D
In the following line:

Code:
$drive = Get-WmiObject -Class Win32_Volume -Filter "DriveLetter = 'e:'"
Obviously i want to replace the hardcoded e: with a variable.

This is what i have come up with:
Code:
Get-Volume | ForEach-Object{
$volumeletter = $_.DriveLetter
$drive = Get-WmiObject -Class Win32_Volume -Filter ("DriveLetter=" + $volumeletter)
$drive.DefragAnalysis().DefragRecommended
}
That code should return, for each drive letter, either True or False. Which it does if i hard code.

However I'm getting an error with the script which i can't get past. My guess is it's something simple, so instead of using another hour on it i gave in and made this post 🙂.

Error:
Code:
Get-WmiObject : Invalid query "select * from Win32_Volume where DriveLetter=C"
At E:\test.ps1:3 char:10
+ $drive = Get-WmiObject -Class Win32_Volume -Filter ("DriveLetter=" + $volumelett ...
+          ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Get-WmiObject], ManagementException
    + FullyQualifiedErrorId : GetWMIManagementException,Microsoft.PowerShell.Commands.GetWmiObjectCommand

ForEach-Object : You cannot call a method on a null-valued expression.
At E:\test.ps1:1 char:14
+ Get-Volume | ForEach-Object{
+              ~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [ForEach-Object], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull,Microsoft.PowerShell.Commands.ForEachObjectCommand
It seems like it should work, but it doesn't. Any suggestions?
This seems to be the culprit
select * from Win32_Volume where DriveLetter=C

Maybe i need it to be like this:
select * from Win32_Volume where DriveLetter="C"?
 
Last edited:
Back
Top