Author Posts

April 11, 2014 at 12:11 pm

Hello,

I'm trying to put subexpressions inside WMI query and it refuses to work. If I put result of my subexpression as plain string then it works just fine. What am I doing wrong?

PS C:\Users\a> Get-WmiObject -ComputerName Computer1 -Query "SELECT * FROM CIM_DataFile WHERE Drive ='E:' and path='\\logfiles\\W3SVC1395239964\\' AND extension='zip' and Creationdate>'{(Get-Date).AddDays(-30).ToShortDateString().toString()}'"
Get-WmiObject : Invalid query "SELECT * FROM CIM_DataFile WHERE Drive ='E:' and path='\\logfiles\\W3SVC1395239964\\' AND extension='zip' and
Creationdate>'{(Get-Date).AddDays(-30).ToShortDateString().toString()}'"
At line:1 char:1
+ Get-WmiObject -ComputerName DALWEBEXTESVC1 -Query "SELECT * FROM CIM_DataFile WH ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Get-WmiObject], ManagementException
+ FullyQualifiedErrorId : GetWMIManagementException,Microsoft.PowerShell.Commands.GetWmiObjectCommand

April 11, 2014 at 12:15 pm

As a tip, a descriptive subject like "Help with subexpression in WMI query" can help make sure the right folks are looking at your post :).

You're not using a subexpression.


Get-WmiObject -ComputerName Computer1 -Query “SELECT * FROM CIM_DataFile WHERE Drive ='E:' and path='\\logfiles\\W3SVC1395239964\\' AND extension='zip' and Creationdate>'$((Get-Date).AddDays(-30).ToShortDateString().toString())'”

Is a subexpression. Subexpressions look like $(), not {}. {} is a script block, and WMI didn't know what to do with it. Subexpressions get parsed by the shell before the query is sent to WMI.