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?