Reputation: 1
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?
Upvotes: 0
Views: 2498
Reputation: 6117
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.
Upvotes: 1