问题
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