Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Powershell 使用-Credential参数访问启动进程的输出_Powershell_Powershell 4.0 - Fatal编程技术网

Powershell 使用-Credential参数访问启动进程的输出

Powershell 使用-Credential参数访问启动进程的输出,powershell,powershell-4.0,Powershell,Powershell 4.0,以下内容将在新窗口中打开,我想这是因为新窗口表示在不同凭据下运行的进程: Start-Process ipconfig -Credential domain\user -NoNewWindow 文档似乎没有指出这一点 考虑到这种情况正在发生,并且我需要使用提升的权限运行,如何将上述命令的输出返回到控制台?使用-RedirectStandardOutput参数将输出重定向到文件。然后,将文件内容读回PowerShell会话 # 1. Get an alternate credential $Cr

以下内容将在新窗口中打开,我想这是因为新窗口表示在不同凭据下运行的进程:

Start-Process ipconfig -Credential domain\user -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:windir\temp\ipconfig.log;

# 3. Retrieve the content from the log file
Get-Content -Path $env:windir\temp\ipconfig.log;

我在这里的回答应该对您有所帮助,它是关于用事件重定向标准输出的-