1

I want to make a script that checks two time if a process is running. First I would like to check normally if a process is running and second time I want to wait 30 seconds until the next check.

Here I have something like this:

$check=Get-Process hl -ErrorAction SilentlyContinue
if ($check -eq $null) {
Start-Sleep -s 30
}

If in both cases the process is not running I want poweshell to send me a mail with this :

$EmailFrom = “[email protected]”
$EmailTo = “[email protected]”
$Subject = “Process not running”
$Body = “NOT RUNNING”
$SMTPServer = “smtp.gmail.com”
$SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 587)
$SMTPClient.EnableSsl = $true
$SMTPClient.Credentials = New-Object System.Net.NetworkCredential(“user”, “pass”);
$SMTPClient.Send($EmailFrom, $EmailTo, $Subject, $Body)

It's something with and but I can't figure out how exactly.

So it will be something like this: if process not running {sleep} and if second time checking not running {mail}

1 Answer 1

3

This should do it:

$check1 = -not ( Get-Process hl -ErrorAction SilentlyContinue )
Start-Sleep -Seconds 30
$check2 = -not ( Get-Process hl -ErrorAction SilentlyContinue )

if ($check1 -and $check2) {
    # email
}

Normally Get-Process returns either process objects, or nothing at all. In terms of truth testing, "one or more anythings" compares as $true and "no things" compares as $false.

  • -not ( get-process ) with a running process, would == $true, this makes it $false.
  • -not ( get-process ) with a stopped process, would == $false, this makes it $true.

So the if test is simple - if ($check1 -and $check2) "if this and this evaluate as $true"

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.