0

Making a short script to keep a program that crashes every now and then running,

Here is what I am trying and been editing

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

What am I doing wrong? / New to scripting / programming

1 Answer 1

3

Writing a mash of code, or not copy and pasting properly?

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

It doesn't seem like you need a function at all, just a 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
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.