启动psexec后获取进程ID

时间:2017-04-20 12:19:27

标签: powershell pid

我有一个用psexec在远程计算机上调用记事本的脚本。有没有办法在启动后获取进程ID?

这就是我所拥有的:

$PCname = "MyPC"
$SessionID = "2"
$Program = "Notepad.exe"
$FilePath = "C:\temp\"
$FileName =  "Test.txt"

set-alias psexec "C:\PsExec\psexec.exe"
   &psexec -s -d -i $SessionID \\$PCname $Program $FilePath\$FileName

运行后,我在输出窗口中显示进程ID:

Connecting to MyPC...Starting PSEXESVC service on MyPC...Connecting 
with PsExec service on MyPC...Starting Notepad.exe on MyPC...
Notepad.exe started on MyPC with process ID 8352.

如何获取进程ID?

2 个答案:

答案 0 :(得分:2)

您可以使用Select-String cmdlet使用 regex 获取进程ID:

&psexec -s -d -i $SessionID \\$PCname $Program $FilePath\$FileName |
   Select-String 'process ID (\d+)' | 
   ForEach-Object {$_.Matches.Groups[1].Value}

答案 1 :(得分:0)

$a = (gps -ComputerName PcName| where{ $_.ProcessName -eq "Notepad.exe"} | select Id)

$a.Id包含想要的ID