2017-04-20 41 views
1

我有一个脚本,用psexec在远程计算机上调用记事本。有没有一种方法可以在启动后获得进程ID?启动后获取进程ID psexec

以下是我有:

$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

可以使用Select-String cmdlet的使用正则表达式抢进程ID:

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

我有点新手的。我如何从中获得我的进程ID? – Eric

+0

它已经返回进程ID ;-) –

+0

???我搞不清楚了。看不到它在哪里返回进程ID – Eric

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

$a.Id包含通缉编号

+0

如果机器上运行的唯一记事本是我开始使用的记事本,那么这很有效。 – Eric