![](/img/trans.png)
[英]How can I output all the output of a called command in Powershell script
[英]How can I force powershell to output all errors normally in cmd?
我希望能夠使用Sysnative cmd執行Powershell腳本並將結果輸出到日志。
當我運行(System32)時:
C:\\Windows\\System32\\cmd.exe /C "powershell -file “C:\\…\\ScriptWithErrors.ps1" >> C:\\…\\Log.log”
我的日志將顯示Powershell文件中的錯誤(通常在終端中以紅色顯示)
但是,當我運行(系統)時:
C:\\Windows\\Sysnative\\cmd.exe /C "powershell -file “C:\\…\\ScriptWithErrors.ps1" >> C:\\…\\Log.log”
我的日志不會顯示任何我未明確使用Write-Host $_.Exception
_。Exception的powershell腳本錯誤。
有沒有一種方法可以執行Powershell腳本並強制輸出錯誤輸出?
另外,為什么使用cmd \\C
使echo無法正常工作?
>> C:\Windows\Sysnative\cmd.exe /C "echo time \t”
>> time /t (Output)
>> 11:20 AM (Expected output)
Powershell將在$ Error變量中保存會話的所有錯誤。 最新的錯誤將是數組中的數字0。
因此,基本上要獲得會話的所有錯誤,您可以最終將整個$ Error變量數組導出到日志中,或者遍歷整個日志並獲取您感興趣的部分。
要獲取錯誤的行號,可以在錯誤變量上使用屬性ScriptStackTrace。
例如,我運行:
Get-ChildItem -Path c:\cmder
Get-ChildItem -Path c:\nodirhere
第一行給我輸出,第二行給我一個錯誤。 現在,通過選擇數組中的位置0並詢問我得到的scriptstacktrace,檢查$ Error數組中的最新錯誤:
$Error[0].ScriptStackTrace
at <ScriptBlock>, <No file>: line 2
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.