我正在尝试使用 powershell 创建一个脚本,游戏正在运行,并通过推送将信息发送到“Uptime Kuma”。
但是,代码中有问题,虽然它没有打开,但正在发送通知。
$satisfactory = Get-Process UnrealServer-Win64-Shipping
$url = "http://:3001/api/push/3xlVsB4AVr?status=up&msg=OK&ping="
while ($true) {
# Check if the service is running
if (!$satisfactory.started) {
# Send a push notification
$pingTime = (Get-Date).ToString("yyyy-MM-ddTHH:mm:ss.fffZ")
Invoke-WebRequest -Uri ($url + $pingTime)
}
# Wait for XX seconds before checking again
Start-Sleep -Seconds 60
}
Powershell AFAIK 没有 ToString 方法。查看 Get-Date 上的文档https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/get-date?view=powershell-7.4。尝试以下方法,看看是否适合您:
# Define the name of the process to check
$processName = "UnrealServer-Win64-Shipping"
# Define the API endpoint URL
$url = "http://localhost:3001/api/push/3xlVsB4AVr"
# Continuously check if the process is running and send a push notification if it is, otherwise log to the console
while ($true) {
# Check if the process is running
$process = Get-Process -Name $processName -ErrorAction SilentlyContinue
if ($process) {
# Send a push notification if the process is running
$pingTime = Get-Date -Format "yyyy-MM-ddTHH:mm:ss.fffZ"
$pingUrl = $url + "?status=up&msg=OK&ping=" + $pingTime
Invoke-WebRequest -Uri $pingUrl -Method Post
} else {
# Log to the console if the process is not running
Write-Host "The process $processName is not running."
}
# Wait for a few seconds before checking again
Start-Sleep -Seconds 60
}