Usinb subexpression operator in Get-CIMInstance filter

Welcome Forums General PowerShell Q&A Usinb subexpression operator in Get-CIMInstance filter

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

 
Participant
2 years, 3 months ago.

  • Author
    Posts
  • #49812

    Participant
    Points: 0
    Rank: Member

    I'm working through the lab of advanced functions in PowerShell Toolmaking, and one of the answers to the lab questions uses the subexpression operator in the filter of Get-CimInstance.

    Get-CimInstance win32_process -filter "ProcessId = '$($Service.ProcessId)'"
    

    The command returns an error if I don't use the subexpression operator.

    Why is that?

  • #49834

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

    You mean, if you do Get-CimInstance win32_process -filter "ProcessId = '$Service.ProcessId'" ?

    Because Only $service is seen as a variable. Do this:

    Write-Host "ProcessId = $Service.processid"

    And you'll see why that doesn't form a legal query. Covered this in "Learn Windows PowerShell in a Month of Lunches," FWIW.

  • #49951

    Participant
    Points: 0
    Rank: Member

    Thanks Don.

    This explanation clears things up.
    I remember you covering it in the first book, and to be honest, I have always struggled with this concept.
    Hoping this exercise drills it in for good now!

The topic ‘Usinb subexpression operator in Get-CIMInstance filter’ is closed to new replies.