簡體   English   中英

使用-Credential參數訪問Start-Process的輸出

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

以下是在新窗口中打開的,我想是因為新窗口代表在不同憑據下運行的進程:

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;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM