我一直在编写ps菜单脚本来帮助我使用游戏服务器。
其中一个函数是循环,检查进程是否正在运行,并从文件中获取一些数据。不过我一直试图打破循环回到主菜单,此刻我们必须关闭脚本并重新打开它。
以下是我在网上找到的一些例子中所尝试的内容
# Guardian Mode, keeps halo online running
Function guardian {
while ($true) {
cls
if ((Get-Process -Name eldorado -ErrorAction SilentlyContinue) -eq $null)
{
Write-Host -ForegroundColor Red "Server Is Not Running" ;
.\eldorado.exe -launcher -dedicated -headless -window -height 300 -width 300 ;
"Guardian Started Headless Eldorito Server $($date)" | Add-Content .\dedicatedServer.log
}
if ([console]::KeyAvailable)
{
echo "Press space to go back to mainmenu";
$x = [System.Console]::ReadKey()
switch ( $x.key)
{
F12 { $continue = mainmenu }
}
}
else {write-host -ForegroundColor Green "Guardian Mode is Activated, Server Is Running (Checking 15s)"
Write-Host -ForegroundColor cyan "Name" ; (Invoke-WebRequest -Uri 127.0.0.1:11775 | ConvertFrom-Json).Name
Write-Host -ForegroundColor cyan "Player Count:" ; (Invoke-WebRequest -Uri 127.0.0.1:11775 | ConvertFrom-Json).numPlayers
Write-Host -ForegroundColor cyan "Variant" ; (Invoke-WebRequest -Uri 127.0.0.1:11775 | ConvertFrom-Json).variant
Write-Host -ForegroundColor cyan "Map" ; (Invoke-WebRequest -Uri 127.0.0.1:11775 | ConvertFrom-Json).map
Write-Host -ForegroundColor cyan "GameType" ; (Invoke-WebRequest -Uri 127.0.0.1:11775 | ConvertFrom-Json).variantType
Write-Host -ForegroundColor cyan "Status" ; (Invoke-WebRequest -Uri 127.0.0.1:11775 | ConvertFrom-Json).status
Get-Content .\chat.log -Tail 5
Write-Host -ForegroundColor cyan "Running Since" ; Get-Content .\dedicatedServer.log -Tail 1
}
Start-Sleep -Seconds 15
}
}