以下内容
在新窗口中打开,我猜是因为新窗口表示在不同凭据下运行的进程:
Start-Process ipconfig -Credential domainuser -NoNewWindow
这里的文档似乎没有指出这一点。
考虑到这种情况正在发生,并且我需要使用提升的权限运行,如何将上述命令的输出恢复到我的控制台中?
使用 -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:windirtempipconfig.log;
# 3. Retrieve the content from the log file
Get-Content -Path $env:windirtempipconfig.log;