少し涼しいのではいだろうか?デスクトップ上に保存したps1ファイルをダブルクリックすると見事に青色表示の26.9℃だ。遠く英国のサーバーにあるであろうデータから文京区内の温度を知るというのもよく考えると不思議なものである。


前回、生成AIに気温をPowewreShellで簡単に表示できないか?と何度かの壁打ち(プロンプトによる問答)の末に気温APIを提供するひな形を生成し、動作だけを追えるようにエラー処理や余分なデコレートを削いだ。

$apiKey = $env:OPENWEATHER_API_KEY

# 文京区(おおよその緯度経度)
$lat = 35.717
$lon = 139.753

$uri = "https://api.openweathermap.org/data/2.5/weather?lat=$lat&lon=$lon&appid=$apiKey&units=metric&lang=ja"

$res = Invoke-RestMethod -Uri $uri -Method Get -ErrorAction Stop

$temp = [double]$res.main.temp
$color = if ($temp -ge 38) { 'Red' }
elseif ($temp -ge 33) { 'Yellow' }
elseif ($temp -le 32) { 'Blue' }
else { 'White' } # 32 < temp < 33 の場合

$msg = ('東京都文京区の現在の気温: {0:N1}°C' -f $temp)
Write-Host $msg -ForegroundColor $color

Read-Host "Enterキーを押すとウィンドウが閉じます"

$apiKey = $env:OPENWEATHER_API_KEYでWindowsに設定した環境変数でOPENWEATHER_APIのKeyを取得、経度・緯度を変数に入れてコールし、レスポンスをInvoke-RestMethod -Uriで取得。気温をダブル型で$tempに入れて、if-else文で温度によって赤、黄、青で分けた色を使って表示させている。

{0:N1}はフォーマット文字列で0は最初の引数$temp、小数点以下1桁までを:N1で指定。次のWrite-Hostでコンソールに書き出し、ダブルクリックで表示したままにするためにRead-Host "Enterキーを押すとウィンドウが閉じます"を末尾に追加している。簡単なカスタマイズをしてみよう。
○気温APIの情報を自動更新

せっかくなので、表示されている情報を更新させたい。数分に1回くらいの更新なら無料枠で十分対応できるのでここではStart-Sleep -Seconds 300で5分に1回更新させてみる。最後の入力待機Read-Hostを削って全体をループで囲むだけだ。

while ($true) {
$apiKey = $env:OPENWEATHER_API_KEY

# 文京区(おおよその緯度経度)
$lat = 35.717
$lon = 139.753

$uri = "https://api.openweathermap.org/data/2.5/weather?lat=$lat&lon=$lon&appid=$apiKey&units=metric&lang=ja"

$res = Invoke-RestMethod -Uri $uri -Method Get -ErrorAction Stop

$temp = [double]$res.main.temp
$color = if ($temp -ge 38) { 'Red' }
elseif ($temp -ge 33) { 'Yellow' }
elseif ($temp -le 32) { 'Blue' }
else { 'White' }

$msg = ('東京都文京区の現在の気温: {0:N1}°C' -f $temp)
Write-Host $msg -ForegroundColor $color

Start-Sleep -Seconds 300
}

こうしてみると何かが足りない。同じ温度が続いていると果たしてサーバー側が動作しているのか不安になったりする。そうだ時刻を入れてみよう。
時刻表示を追加するには、$date = Get-Dateで取得して、書き出しのWrite-Hostに$dateを加えるだけだ。

while ($true) {
$apiKey = $env:OPENWEATHER_API_KEY

# 文京区(おおよその緯度経度)
$lat = 35.717
$lon = 139.753

$uri = "https://api.openweathermap.org/data/2.5/weather?lat=$lat&lon=$lon&appid=$apiKey&units=metric&lang=ja"

$res = Invoke-RestMethod -Uri $uri -Method Get -ErrorAction Stop
$date = Get-Date
$temp = [double]$res.main.temp
$color = if ($temp -ge 38) { 'Red' }
elseif ($temp -ge 33) { 'Yellow' }
elseif ($temp -le 32) { 'Blue' }
else { 'White' }

$msg = ('東京都文京区の現在の気温: {0:N1}°C' -f $temp)
Write-Host $date $msg -ForegroundColor $color

Start-Sleep -Seconds 300
}

○ログ機能を追加してみる

やはり時刻表示があると引き締まるものである。時間があると記録性が生まれる。こうなるととりあえず記録しておきたいと思うわけであるが、色は必要ないので手軽に変数($log)に時間($date)と気温($temp)をOut-Fileでテキスト保存する。

while ($true) {
$apiKey = $env:OPENWEATHER_API_KEY

# 文京区(おおよその緯度経度)
$lat = 35.717
$lon = 139.753

$uri = "https://api.openweathermap.org/data/2.5/weather?lat=$lat&lon=$lon&appid=$apiKey&units=metric&lang=ja"

$res = Invoke-RestMethod -Uri $uri -Method Get -ErrorAction Stop
$date = Get-Date
$temp = [double]$res.main.temp
$color = if ($temp -ge 38) { 'Red' }
elseif ($temp -ge 33) { 'Yellow' }
elseif ($temp -le 32) { 'Blue' }
else { 'White' }

$msg = ('東京都文京区の現在の気温: {0:N1}°C' -f $temp)
Write-Host $date $msg -ForegroundColor $color
$log = "$msg $date"
Out-File -FilePath "log.txt" -Append -InputObject $log

Start-Sleep -Seconds 300
}

ログ用に$log = "$msg $date"と、Out-File -FilePath "log.txt" -Append -InputObject $logの2行を追加。Out-Fileはファイルを書き出すコマンドレットで変数の内容を読み取りファイルに書き込む際に必要な-InputObject、上書き指定の-Appendを用いている。

スクリプトを実行すると、ソースコード(ps1ファイル)と同じ場所にlog.txtが作成されるので、開いてみると無事に記録されていた。

.
編集部おすすめ