PowerShellスクリプトの例: 実行パスを指定する場合

Some of the links in this article are "affiliate links", a link with a special tracking code. This means if you click on an affiliate link and purchase the item, we will receive an affiliate commission.

The price of the item is the same whether it is an affiliate link or not. Regardless, we only recommend products or services we believe will add value to our readers.

By using the affiliate links, you are helping support our Website, and we genuinely appreciate your support.

緊急地震速報アラーム+で実行ファイルのパスを指定する場合のPowerShellスクリプトの例を示します。
なお、下記のスクリプトの著作権はapitが保有します。
また、お使いになりたい方は私の銀行口座にPC1台につき月額2200円をお振込みください。振込手数料はご負担ください。
下記のスクリプトにおけるコマンド実行機能に設定する文字列の例: 「powershell “(PowerShellスクリプトファイルの絶対パス)” “<ソース>” “<推定震度>” “<マグニチュード>” “<最大震度>“」
(2026/3/11 更新済み)

# PowerShell Code
# Copyright(C) 2026 apit.

$CurrentDir = Split-Path $MyInvocation.MyCommand.Path

$SOURCE=$args[0]
$SOURCE_MATCH = $FALSE
$ESI=$args[1]
$ESI_OVER = $FALSE
$MAG=$args[2]
$MAG_OVER = $FALSE
$MAX_ESI=$args[3]
$MAX_ESI_OVER = $FALSE

if ($SOURCE -eq "SNP") { $SOURCE_MATCH = $TRUE }
elseif ($SOURCE -eq "L10S") { $SOURCE_MATCH = $TRUE }

if ($ESI -eq "不明") { $ESI_OVER = $FALSE }
if ($ESI -eq "1未満") { $ESI_OVER = $FALSE }
if ($ESI -eq "1") { $ESI_OVER = $TRUE }
if ($ESI -eq "2") { $ESI_OVER = $TRUE }
if ($ESI -eq "3") { $ESI_OVER = $TRUE }
if ($ESI -eq "4") { $ESI_OVER = $TRUE }
if ($ESI -eq "5弱") { $ESI_OVER = $TRUE }
if ($ESI -eq "5強") { $ESI_OVER = $TRUE }
if ($ESI -eq "6弱") { $ESI_OVER = $TRUE }
if ($ESI -eq "6強") { $ESI_OVER = $TRUE }
if ($ESI -eq "7") { $ESI_OVER = $TRUE }

if ($MAG -eq "不明") { $MAG_OVER = $FALSE }
elseif ($MAG -ge 6.0) { $MAG_OVER = $TRUE }

if ($MAX_ESI -eq "不明") { $MAX_ESI_OVER = $FALSE }
if ($MAX_ESI -eq "1未満") { $MAX_ESI_OVER = $FALSE }
if ($MAX_ESI -eq "1") { $MAX_ESI_OVER = $FALSE }
if ($MAX_ESI -eq "2") { $MAX_ESI_OVER = $FALSE }
if ($MAX_ESI -eq "3") { $MAX_ESI_OVER = $FALSE }
if ($MAX_ESI -eq "4") { $MAX_ESI_OVER = $FALSE }
if ($MAX_ESI -eq "5弱") { $MAX_ESI_OVER = $TRUE }
if ($MAX_ESI -eq "5強") { $MAX_ESI_OVER = $TRUE }
if ($MAX_ESI -eq "6弱") { $MAX_ESI_OVER = $TRUE }
if ($MAX_ESI -eq "6強") { $MAX_ESI_OVER = $TRUE }
if ($MAX_ESI -eq "7") { $MAX_ESI_OVER = $TRUE }

if ($SOURCE_MATCH)
{
if ((($SOURCE -eq "SNP") -and ($ESI_OVER -or $MAG_OVER)) -or (($SOURCE -eq "L10S") -and ($MAX_ESI_OVER)))
{
try
{
if (Test-Path -Path "${CurrentDir}\\WaitLoop")
{
Remove-Item -Path "${CurrentDir}\\WaitLoop"
}

$mutexObject = New-Object System.Threading.Mutex($false, "Global\MYCMDS_MUTEX")

if (-not $mutexObject.WaitOne(0, $false))
{
while($true)
{
$mutexObject = New-Object System.Threading.Mutex($false, "Global\MYCMDS_MUTEX")
if (-not $mutexObject.WaitOne(0, $false))
{
if (Test-Path -Path "${CurrentDir}\\WaitLoop")
{
try
{
Remove-Item -Path "${CurrentDir}\\WaitLoop"
}
catch
{
}
}
Start-Sleep -Milliseconds 20;
}
else
{
break;
}
}
}

# 監視対象のプロセスを取得
$process = Get-Process -Name "MyProcess" -ErrorAction SilentlyContinue;

# 監視対象のプロセスを監視して実行されていないときはプロセスを立ち上げる
# プロセス待機する場合はifを外す
if (!$process) {
Out-File -FilePath "${CurrentDir}\\WaitLoop"

if (!$process) {
(実行ファイルのパス)
}

if (-not (Test-Path -Path "${CurrentDir}\\KeepRunning"))
{
$DATE_BEGIN = Get-Date;
# 実行ファイルを閉じるまでの時間を指定(秒: AddSecondsで指定)
# 注: 延長式
$DATE_END = $DATE_BEGIN.AddSeconds(300);
while($TRUE)
{
$DATE_NOW = Get-Date;
$TIME_SPAN = $DATE_END - $DATE_NOW;
if ($TIME_SPAN -lt 0)
{
break;
}
if (-not (Test-Path -Path "${CurrentDir}\\WaitLoop"))
{
if (Test-Path -Path "${CurrentDir}\\WaitLoop")
{
try
{
Remove-Item -Path "${CurrentDir}\\WaitLoop"
}
catch
{
}
}
# ミューテックスを解放します。
if ($mutexObject -ne $null)
{
$mutexObject.ReleaseMutex();
}
# ミューテックスのリソースを解放します。
if ($mutexObject -ne $null)
{
$mutexObject.Close();
}
exit;
}
if (Test-Path -Path "${CurrentDir}\\WaitLoop - コピー")
{
Out-File -FilePath "${CurrentDir}\\KeepRunning"
if (Test-Path -Path "${CurrentDir}\\WaitLoop")
{
Remove-Item -Path "${CurrentDir}\\WaitLoop"
}
# Change the " - コピー" part to the expression for the post-copy string in Windows in your language.
if (Test-Path -Path "${CurrentDir}\\WaitLoop - コピー")
{
Remove-Item -Path "${CurrentDir}\\WaitLoop - コピー"
}
# ミューテックスを解放します。
if ($mutexObject -ne $null)
{
$mutexObject.ReleaseMutex();
}
# ミューテックスのリソースを解放します。
if ($mutexObject -ne $null)
{
$mutexObject.Close();
}
exit;
}
if (Test-Path -Path "${CurrentDir}\\KeepRunning")
{
if (Test-Path -Path "${CurrentDir}\\WaitLoop")
{
Remove-Item -Path "${CurrentDir}\\WaitLoop"
}
# Change the " - コピー" part to the expression for the post-copy string in Windows in your language.
if (Test-Path -Path "${CurrentDir}\\WaitLoop - コピー")
{
Remove-Item -Path "${CurrentDir}\\WaitLoop - コピー"
}
# ミューテックスを解放します。
if ($mutexObject -ne $null)
{
$mutexObject.ReleaseMutex();
}
# ミューテックスのリソースを解放します。
if ($mutexObject -ne $null)
{
$mutexObject.Close();
}
exit;
}
$process = Get-Process -Name "MyProcess" -ErrorAction SilentlyContinue;
if (!$process)
{
if (Test-Path -Path "${CurrentDir}\\WaitLoop")
{
Remove-Item -Path "${CurrentDir}\\WaitLoop"
}
# ミューテックスを解放します。
if ($mutexObject -ne $null)
{
$mutexObject.ReleaseMutex();
}
# ミューテックスのリソースを解放します。
if ($mutexObject -ne $null)
{
$mutexObject.Close();
}
exit;
}
Start-Sleep -Milliseconds 20;
}
if (Test-Path -Path "${CurrentDir}\\WaitLoop")
{
Remove-Item -Path "${CurrentDir}\\WaitLoop"
}

# 監視対象のプロセスを取得
$process2 = Get-Process -Name "MyProcess" -ErrorAction SilentlyContinue;

# 監視対象のプロセスを監視して実行されていないときはプロセスを立ち上げる
if ($process2) {
Stop-Process -Name "MyProcess"
}
}
else
{
$process = Get-Process -Name "MyProcess" -ErrorAction SilentlyContinue;
while($process)
{
if (-not (Test-Path -Path "${CurrentDir}\\WaitLoop"))
{
if (Test-Path -Path "${CurrentDir}\\WaitLoop")
{
try
{
Remove-Item -Path "${CurrentDir}\\WaitLoop"
}
catch
{
}
}
# ミューテックスを解放します。
if ($mutexObject -ne $null)
{
$mutexObject.ReleaseMutex();
}
# ミューテックスのリソースを解放します。
if ($mutexObject -ne $null)
{
$mutexObject.Close();
}
exit;
}
if (Test-Path -Path "${CurrentDir}\\WaitLoop - コピー")
{
Out-File -FilePath "${CurrentDir}\\KeepRunning"
if (Test-Path -Path "${CurrentDir}\\WaitLoop")
{
Remove-Item -Path "${CurrentDir}\\WaitLoop"
}
if (Test-Path -Path "${CurrentDir}\\WaitLoop - コピー")
{
Remove-Item -Path "${CurrentDir}\\WaitLoop - コピー"
}
# ミューテックスを解放します。
if ($mutexObject -ne $null)
{
$mutexObject.ReleaseMutex();
}
# ミューテックスのリソースを解放します。
if ($mutexObject -ne $null)
{
$mutexObject.Close();
}
exit;
}
$process = Get-Process -Name "MyProcess" -ErrorAction SilentlyContinue;
if (!$process)
{
Remove-Item -Path "${CurrentDir}\\WaitLoop"
# KeepRunningを自分で消す場合は次の行をコメントアウト
Remove-Item -Path "${CurrentDir}\\KeepRunning"
# ミューテックスを解放します。
if ($mutexObject -ne $null)
{
$mutexObject.ReleaseMutex();
}
# ミューテックスのリソースを解放します。
if ($mutexObject -ne $null)
{
$mutexObject.Close();
}
exit;
}
Start-Sleep -Milliseconds 20;
}
}
#}
}
finally
{
try
{
if (Test-Path -Path "${CurrentDir}\\WaitLoop")
{
try
{
Remove-Item -Path "${CurrentDir}\\WaitLoop"
}
catch
{
}
}
# ミューテックスを解放します。
if ($mutexObject -ne $null)
{
$mutexObject.ReleaseMutex();
}
# ミューテックスのリソースを解放します。
if ($mutexObject -ne $null)
{
$mutexObject.Close();
}
}
catch
{
}
}
}
}