locked
Where-Object RRS feed

  • Question

  • can someone help me here what is wrong with my syntax ? I'm trying to get multiple process name.. can someone help me here or is there be better method ? I'm specifying multiple computer name in the txt file

    [PS] C:\temp>Invoke-Command -ComputerName (Get-Content c:\temp\test-servers.txt) -ScriptBlock { get-wmiobject win32_process | where-object { ($_.name
    -like "powershell.exe") -and ($_.name -like "notepad.exe")}}} | select name, PSComputerName,@{n="owner";e={$_.getowner().user}}} | Format-Table name,P
    SComputerName, owner -AutoSize

    Unexpected token '}' in expression or statement.
    At line:1 char:199
    + Invoke-Command -ComputerName (Get-Content c:\temp\test-servers.txt) -ScriptBlock { get-wmiobject win32_process | where-object { ($_.name -like "pow
    ershell.exe") -and ($_.name -like "notepad.exe")}}} <<<<  | select name, PSComputerName,@{n="owner";e={$_.getowner().user}}} | Format-Table name,PSCo
    mputerName, owner -AutoSize
        + CategoryInfo          : ParserError: (}:String) [], ParentContainsErrorRecordException
        + FullyQualifiedErrorId : UnexpectedToken


    Jimmy Wang

    • Moved by Bill_Stewart Wednesday, September 4, 2019 7:54 PM Unanswerable drive-by question
    Thursday, April 4, 2019 7:54 PM

All replies

  • Please post your code correctly. It is unreadable. Use the code posting tool provided on the edit bar.

    Please edit your original post and fix this.


    \_(ツ)_/

    Thursday, April 4, 2019 8:19 PM
  • The error tells you what is wrong. You are either missing a `}` or you have an extra somewhere. Since I am nice, I copied your code into my editor and took a look. You had 2 extra brackets. Download VScode with the bracket colorizer extension to avoid these issues in the future. Here is what your code should look like.

    Invoke-Command -ComputerName (Get-Content c:\temp\test-servers.txt) -ScriptBlock {get-wmiobject win32_process | where-object {($_.name -like "powershell.exe") -and ($_.name -like "notepad.exe")}} | select name,PSComputerName,@{n="owner";e={$_.getowner().user}} | Format-Table name,PSComputerName,owner -AutoSize

    Break the code down to make it more readable like below:

    $scriptBlock = {
    
        get-wmiobject win32_process | where-object {($_.name -like "powershell.exe") -and ($_.name -like "notepad.exe")}
    
    }
    
    $computerNames = Get-Content c:\temp\test-servers.txt
    
    Invoke-Command -ComputerName $computerNames -ScriptBlock $scriptBlock | select name,PSComputerName,@{n="owner";e={$_.getowner().user}} | Format-Table name,PSComputerName,owner -AutoSize


    Friday, April 5, 2019 1:08 PM
  • First there is no need to use Invoke-Command with WMI as WMI does its own remoting. 

    Do not try to write everything on one line and avoid excess spaces in your code.  Use spaces and line breaks for clarity.

    The correct command for this is:

    $computers = Get-Content c:\temp\test-servers.txt
    $filter = "Name='powershell.exe' OR Name='notepad.exe'"
    Get-WmiObject win32_process -Filter $filter -ComputerName $computers
    

    Note that the logic MUST BE "OR" and not "AND" unless you only want systems that only have both running then use AND.

    Use the WMI filter or you will return every process in your network to filter for only two.


    \_(ツ)_/

    Friday, April 5, 2019 1:44 PM