简体   繁体   English

Powershell脚本if和

[英]Powershell script if and

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. 首先,我想正常检查进程是否正在运行,第二次我要等待30秒直到下一次检查。

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 : 如果在这两种情况下进程都没有运行我想要poweshell给我发一封邮件:

$EmailFrom = “mail@gmail.com”
$EmailTo = “othermail@yahoo.com”
$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} 所以它会是这样的:如果进程没有运行{sleep}并且第二次检查没有运行{mail}

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. 通常, Get-Process返回流程对象,或者根本不返回任何流程对象。 In terms of truth testing, "one or more anythings" compares as $true and "no things" compares as $false . 在真值测试方面,“一个或多个任何事物”比较为$true和“no things”比较为$false

  • -not ( get-process ) with a running process, would == $true, this makes it $false. -not(get-process)有一个正在运行的进程,将== $ true,这使得它为$ false。
  • -not ( get-process ) with a stopped process, would == $false, this makes it $true. -not(get-process)使用已停止的进程,将== $ false,这使得它为$ true。

So the if test is simple - if ($check1 -and $check2) "if this and this evaluate as $true" 所以if测试很简单 - if ($check1 -and $check2) “如果这个并且这个评估为$ true”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM