Get Process ID after starting psexec

戏子无情 提交于 2020-07-09 05:25:34

问题


I have a script that calls notepad on a remote computer with psexec. Is there a way I can get the Process ID after it is started?

Here is what I have:

$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

After running I get this in the output window that shows the Process 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.

How can I grab the process ID?


回答1:


You can use the Select-String cmdlet to grab the process ID using a regex:

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



回答2:


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

$a.Id contains the wanted Id



来源:https://stackoverflow.com/questions/43519345/get-process-id-after-starting-psexec

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!