Reputation: 115
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}
Upvotes: 1
Views: 121
Reputation: 28963
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
.
So the if test is simple - if ($check1 -and $check2)
"if this and this evaluate as $true"
Upvotes: 3