[英]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.