What am I doing wrong?

Welcome Forums General PowerShell Q&A What am I doing wrong?

This topic contains 1 reply, has 2 voices, and was last updated by

 
Keymaster
4 years, 7 months ago.

  • Author
    Posts
  • #14494
    GS

    Participant
    Points: 1
    Rank: Member

    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

  • #14495

    Keymaster
    Points: 1,619
    Helping HandTeam Member
    Rank: Community Hero

    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.

The topic ‘What am I doing wrong?’ is closed to new replies.