Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
980 views
in Technique[技术] by (71.8m points)

powershell - start remote process within the context

I am wondering how to start process remotely within the users context like he started it. Let me explain. I know how to start process remotely, so for example I want to start notepad:

Invoke-WmiMethod win32_process -name create -ComputerName $remoteMachine -ArgumentList "notepad" -credential (Get-Credential)

the problem is that it starts notepad "in the backround" (not in the users context), so in this case he won't see opened "notepad" dialog/process (he will see notepad just in the list of processes in task manager). I want him to see notepad window dialog.

Does anybody know how to achieve that?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Use built-in SchTasks.exe for a supported way to create processes on a remote system. This interfaces with the built-in Task Scheduler service and does not require PsExec.exe.

To create a task on a remote machine (in this example running as SYSTEM):

schtasks.exe /create /F /S COMPUTERNAME /RU "NT AUTHORITYSYSTEM" /RL HIGHEST /SC ONSTART /TN "RemoteProcess" /TR "program.exe "argument 1" "argument 2""

schtasks.exe /Run /S COMPUTERNAME /I /TN "RemoteProcess"

schtasks.exe /Delete /S COMPUTERNAME /TN "RemoteProcess"

Notes:

  • We use ONSTART as the schedule, but then we start the process manually and delete it before the schedule is fired. This effectively means "just do it now". You could also specify /SC ONCE /SD "01/01/1980" /ST "00:00:00" which would have the same effect.
  • This example is running as System. To run as the logged-in user, provided you know who that is use /RU "DOMAINUSER". This will work without a password (/RP option) if the user is logged in .
  • You can use /Query /S COMPUTERNAME /TN "RemoteProcess" /V to find the current status e.g. to wait for exit and then read the exit code.

You can also do all the above with script using the Task Scheduler Scripting Objects:


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...