用于检查并保持程序运行的Powershell脚本

用于检查并保持程序运行的Powershell脚本,powershell,Powershell,制作一个简短的脚本,让一个时不时崩溃的程序运行 以下是我正在尝试和编辑的内容 $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

制作一个简短的脚本,让一个时不时崩溃的程序运行

以下是我正在尝试和编辑的内容

$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
}

我做错了什么编写脚本/编程新手

编写混搭代码,还是不正确复制和粘贴

$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   # 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
}
看起来你根本不需要函数,只需要一个循环

->