Accessing output from Start-Process with -Credential parameter

拈花ヽ惹草 提交于 2019-12-24 13:43:19

问题


The following opens in a new window, I guess because the new window represents the process running under a different credential:

Start-Process ipconfig -Credential domain\user -NoNewWindow

The documentation here doesn't seem to point this out.

Considering this is occuring, and I need to run with with elevated privilages, how can I get the output of the above command back into my console?


回答1:


Use the -RedirectStandardOutput parameter to redirect the output to a file. Then, read the file contents back into your PowerShell sessions.

# 1. Get an alternate credential
$Cred = Get-Credential;

# 2. Start the process, redirecting the output to a file
Start-Process -Credential $Cred -FilePath ipconfig.exe -NoNewWindow -Wait -RedirectStandardOutput $env:windir\temp\ipconfig.log;

# 3. Retrieve the content from the log file
Get-Content -Path $env:windir\temp\ipconfig.log;


来源:https://stackoverflow.com/questions/20899007/accessing-output-from-start-process-with-credential-parameter

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