-->

访问来自与-Credential参数启动过程输出(Accessing output from Sta

2019-10-19 03:58发布

下面在新窗口中打开,我猜是因为新的窗口代表的过程中根据不同的凭据运行:

Start-Process ipconfig -Credential domain\user -NoNewWindow

该文件在这里似乎并没有指出这一点。

考虑到这是存在的,我需要用较高的privilages跑,我怎么能得到上述命令的输出回到我的控制台?

Answer 1:

使用-RedirectStandardOutput参数输出重定向到一个文件中。 然后,读取文件内容返回到您的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;


文章来源: Accessing output from Start-Process with -Credential parameter