LoginSignup
4
0

More than 1 year has passed since last update.

この記事は

  • 毎日18時になったら、YahooにPingを打つ
  • Ping応答の結果はメールでお知らせしてもらう
    • メールは自宅のぷららメールを使う

前提

OSはWindows11 Homeをつかう

Cドライブ直下にExPingフォルダーを作る

C:\ExPing
│  create-pass-text.ps1 <--- メールアカウントのパスワードを平文からSecure Stringに変換する
│  ExPing.def
│  ExPing.def.#CONFIG#
│  ExPing.exeExPing.ini
│  ExPing.txt
│  main.ps1 <--- PowerShellpass.txt <--- メールアカウントのパスワード(Secure String)task.bat <--- タスクスケジューラーから呼び出されるバッチファイル
│
└─log
        20221221-064725.csv
        20221221-064920.csv
        20221221-064939.csv
        ...

タスクスケジューラーを設定する

  1. Windows + R
  2. taskschd.msc + Enter

18時になったらtask.batを起動する

image.png

「開始のオプション」には task.bat が入っているフォルダを指定する

プログラムを作成する

18時に起動するtask.batをつくる

task.bat

と言っても、task.batはPowerShellを呼び出すだけのバッチ
メールを送信したりするのは、PowerShellのほうが簡単にできるから!

main.ps1を起動するバッチ
powershell -NoProfile -ExecutionPolicy Unrestricted .\main.ps1
exit

# ほかの書き方
%SYSTEMROOT%\System32\WindowsPowerShell\v1.0\powershell.exe -NoProfile -ExecutionPolicy Unrestricted .\main.ps1

task.batはタスクスケジューラーに登録して、PowerShellを呼び出すだけ

main.ps1

こちらがメインの処理
task.batが18時に起動して、直後にこのmain.ps1(PowerShellのスクリプト)が起動する

main.ps1
#Requires -Version 5.0

# ExPing を使って Ping を打つ
$DateTime = Get-Date -Format "yyyyMMdd-HHmmss"
$LogFile = ".\log\$Datetime.csv"
C:\ExPing\ExPing.exe -R -C -O $LogFile | Out-Null
# 注意: Out-Null をつけて同期しないと
# ログファイルが生成される前に次の処理に行ってしまう

# Pingのログファイルから、NGの行を拾う
$NGList = Get-Content -Encoding oem $LogFile | Select-String -Pattern "NG"

if($null -eq $NGList) {
    $Result = "OK"
} else {
    $Result = ($NGList -join "`r`n")
}

Write-Host $Result

$UserName = "{メールアカウント}"
$SecurePassword = Get-Content ".\pass.txt" | ConvertTo-SecureString

$Credential = New-Object System.Management.Automation.PSCredential ($UserName, $SecurePassword)

$MailParam = @{
    SmtpServer = "purple.plala.or.jp";
    From       = "{メールアカウント}@purple.plala.or.jp";
    To         = "{宛先}@yahoo.co.jp";
    Subject    = "test pwsh"
    Body       = $Result
    Encoding   = [System.Text.Encoding]::UTF8
    Port       = 587
    Credential = $Credential
}

Send-MailMessage @MailParam

ExPingの説明

オプション 説明
-R 起動時にPingをうつ
-C Pingを打ったあとにExPingを閉じる
-O hoge.log ExPingの結果を hoge.log に出力する
ExPing.exe -R -C -O hoge.log

メールと連携せず、単純に毎日Pingを打つだけなら、このままスケジューラーに登録すればよい

メール送信コマンドの説明

Send-MailMessageはObsoleteになっているので、クリティカルな場面では使わないほうがよさそう
MailKitを使えとのことだけど
GitHubにビルド済のdllが転がっておらず、NuGetパッケージを取得しなくてはいけないので
PowerShellから使うのは手間がかかるため、今回は使わなかった

C#で書くときは、従順にNuGetからMailKitをつかえばいいと思う

WARNING: The command 'Send-MailMessage' is obsolete. This cmdlet does not guarantee secure connections to SMTP servers. While there is no immediate replacement available in PowerShell, we recommend you do not use Send-MailMessage at this time. See https://aka.ms/SendMailMessage for more information.

セキュアパスワード生成用スクリプト

メールアカウントのパスワードを平文からSecure Stringに変換し
pass.txtに保存するだけのスクリプト

基本的に1回しか使わない

create-pass-text.ps1
$UserName = "{メールアカウント}"
$Credential = Get-Credential -UserName $UserName
$Credential.Password | ConvertFrom-SecureString | Set-Content ".\pass.txt"

余談:パスワードだけセキュアストリングで保存する方法

create-secure-pass
$pass = Read-Host -AsSecureString -Prompt "Enter your password"
ConvertFrom-SecureString -SecureString $pass -Key (1..16) | Set-Content ".\pass.txt"
# ConvertTo-SecureString するときも、同様に -Key (1..16) をつける

メールを送った結果

NG行だけをメールで受け取ることができた!

image.png

糸冬了!!

4
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
0