smakme7757
Golden Member
Ok, i am trying to use the output of:
Which is
C
E
G
D
In the following line:
Obviously i want to replace the hardcoded e: with a variable.
This is what i have come up with:
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:
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"?
Code:
$Volume = Get-Volume
$Volume.DriveLetter
C
E
G
D
In the following line:
Code:
$drive = Get-WmiObject -Class Win32_Volume -Filter "DriveLetter = 'e:'"
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
}
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
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: