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