我剛開始使用 Powershell 并且已經遇到了問題。我正在使用來自 OpenWeathermap ( https://openweathermap.org/ )的 API來創建類似天氣機器人的東西。
我正在使用 API 中的這個函式:
Write-WeatherCurrent -City $place -ApiKey $ApiKey -Units metric
像這樣的輸出(如果我填寫變數):倫敦 10.2°C(?? 少云)
所以我希望這個輸出保存在一個檔案中。我已經嘗試過命令輸出檔案和 >>。但它只在終端中輸出,檔案為空。我不確定,但是否因為“寫入”-WeatherCurrent?
如果有人可以幫助我,我會很高興:D
謝謝
uj5u.com熱心網友回復:
Write-WeatherCurrent用于Write-Host將輸出直接寫入主機控制臺緩沖區。
如果您使用的是 PowerShell 5.0 或更高版本,您可以Write-Host使用InformationVariable通用引數將輸出捕獲到變數中:
Write-WeatherCurrent -City $place -ApiKey $ApiKey -Units metric -InformationVariable weatherInfo
$weatherInfo 現在包含字串輸出,您可以將其寫入檔案:
$weatherInfo |Out-File path\to\file.txt
如果目標命令不公開公共引數,另一種選擇是將Information流合并到標準輸出流中:
$weatherInfo = Write-WeatherCurrent -City $place -ApiKey $ApiKey -Units metric 6>&1 # "stream 6" is the Information stream
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/331893.html
