我正在寫一個Powershell函數,用在我寫的.ps1腳本中。Powershell函數返回值併產生調試輸出
該函數返回由腳本內調用它的函數使用的單個數字。在開發它的時候,我想要輸出調試信息的函數(普通的舊字符串)。
有時候,我只是想調試輸出顯示在屏幕上,有時我想捕捉它在一個文件中(我以爲我會做任何
.\myscript.pl1 > file.txt
或
?.\myscript.pl1 2> file.txt
有沒有辦法做到這一點
邁克,我想嘗試寫日誌,但是我的系統上我有:
D:\library>gcm write* CommandType Name ----------- ---- Alias write Application write.exe Application write.exe Cmdlet Write-Debug Cmdlet Write-Error Cmdlet Write-EventLog Cmdlet Write-Host Cmdlet Write-Output Cmdlet Write-Progress Cmdlet Write-Verbose Cmdlet Write-Warning
思考更多,我敢打賭我沉沒了。我習慣於可以寫入STDOUT並返回值的函數,這兩件事情是不同的。它們在Powershell中總是一樣嗎? – JonathanZ
當然不是。例如,比較'Write-Output'和'Write-Host'。還有'Write-Debug','Write-Warning','Write-Error'和'Write-Verbose'。 PowerShell有大量的流,'Write-Output'是唯一一個進入流水線的流。 – Joey
Joey-有沒有這些默認的屏幕,可以重新定向到一個文件? – JonathanZ