0

I'd like to search through ten terminal servers after users that are running a specific program. I want the output to include process, computername and user.

This command basically does what I want:

Invoke-Command -ComputerName (Get-Content .\test-servers.txt) -ScriptBlock { get-wmiobject win32_process|where{$_.name -eq "iexplore.exe" }|select name, __SERVER,@{n="owner";e={$_.getowner().user}}} | Format-Table name, PSComputerName, owner -AutoSize

name         PSComputerName owner 
----         -------------- ----- 
iexplore.exe mrdsh-test     dojo03
iexplore.exe mrdsh-test     dojo03
iexplore.exe mrdsh-test     baob12
iexplore.exe mrdsh-test     baob12

But when I try to create a script which takes the process as a parameter I can't get it to work.

CmdletBinding()]
Param (
[parameter(mandatory=$true)][string]$process
)

Invoke-Command -ComputerName (Get-Content .\test-servers.txt) -ScriptBlock { get-wmiobject win32_process | where{param($process)$_.name  -eq $process } | select name, __SERVER,@{n="owner";e={$_.getowner().user}}} | Format-Table name, PSComputerName, owner -AutoSize

I haven't managed to send the $process parameter to the server that's invoking the command. How to I do that? Or is there an easier way to look for processes and return process, computername and username?

1
  • 'I can't get it to work'... have you some error? Commented Nov 9, 2012 at 12:55

1 Answer 1

1

You have param($process) in your where scriptblock, and you don't set it, so it will be empty.

Edit:

As requested, taking param out the following works on my machine:

[CmdletBinding()]
Param (
[parameter(mandatory=$true)][string]$process
)

Invoke-Command -ScriptBlock { get-wmiobject win32_process | where{ $_.name  -eq $process } | select name, __SERVER,@{n="owner";e={$_.getowner().user}}} | Format-Table name, PSComputerName, owner -AutoSize

It also works in Powershell version 1:

powershell -version 1.0 -noprofile -Command ".\test.ps1 -process 'chrome.exe'"

works just fine.

Sign up to request clarification or add additional context in comments.

5 Comments

I thought I'm setting it by adding it as a parameter when I run the script "LookForProcess.ps1 iexplore.exe"
Have you tried it without the param($process) in the where block? Still have issues? The parameter of the script is a different one from the one in your where block.
There's no difference in the results if I remove param($process) in the where block. Ok, it's my first attempt of writing a powershell script. Learning by doing :)
Not sure I can help then, I tested it locally and it works just fine for me.
Oh, that's strange. Could you paste your working script? Perhaps I've made some small mistake that you haven't done.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.