简体   繁体   English

使用-Credential参数访问Start-Process的输出

[英]Accessing output from Start-Process with -Credential parameter

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? 考虑到这种情况的发生,并且我需要使用特权提升运行,如何才能将上述命令的输出返回到控制台?

Use the -RedirectStandardOutput parameter to redirect the output to a file. 使用-RedirectStandardOutput参数可将输出重定向到文件。 Then, read the file contents back into your PowerShell sessions. 然后,将文件内容读回到PowerShell会话中。

# 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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM