1

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

Start-Process ipconfig -Credential domain\user -NoNewWindow

这里的文档似乎没有指出这一点。

考虑到这种情况正在发生,并且我需要以提升的权限运行,我怎样才能将上述命令的输出返回到我的控制台?

4

1 回答 1

2

使用-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;
于 2014-01-03T13:55:16.750 回答