创建一个简短的脚本来保持程序不时崩溃,然后运行,
以下是我正在尝试和编辑的内容
$date = Get-Date -Format G
Function Loop {
{ if(Get-Process -Name notepad -ErrorAction SilentlyContinue) -eq $null
write-host -ForegroundColor red "Server Is Not Running"
.\eldorado.exe -launcher -dedicated -headless -window -height 300 -width 300
echo "Guardian Started Headless Eldorito Server $($date)" | Add-Content .\dedicatedServer.log }
else {write-host -ForegroundColor green "Server Is Running"
sleep 10
Loop
}
Loop
}
我做错了什么? /脚本/编程新手
答案 0 :(得分:3)
编写代码混搭,还是不正确复制和粘贴?
$date = Get-Date -Format G # OK
Function Loop { # OK
# no, your function now starts with a scriptblock
# no, your if () {} pattern is broken.
{if(Get-Process -Name notepad -ErrorAction SilentlyContinue) -eq $null
# ok, but formatting makes it hard to read
write-host -ForegroundColor red "Server Is Not Running"
.\eldorado.exe -launcher -dedicated -headless -window -height 300 -width 300
echo "Guardian Started Headless Eldorito Server $($date)" | Add-Content .\dedicatedServer.log }
#ok
else {write-host -ForegroundColor green "Server Is Running"
sleep 10
# not ok, your loop is now a recursive function call which will consumer more resources forever until it crashes
Loop
}
# what's this doing?
Loop
}
看起来你根本不需要一个函数,只是一个循环..
- >
$date = Get-Date -Format G
while ($true)
{
if ((Get-Process -Name notepad -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
}
else
{
write-host -ForegroundColor Green "Server Is Running"
}
Start-Sleep -Seconds 10
}