Powershell布尔运算符

Powershell布尔运算符,powershell,if-statement,boolean,Powershell,If Statement,Boolean,我希望创建一个布尔脚本,当某些证书设置为过期时发出警报 我在查看90天以上、89天到61天、60天到31天以及不到30天的警报。警语现在不是一个大问题 我知道我还需要更多,但希望能在这方面有所帮助。我感谢大家的帮助 $Alert = 'TakeAction' If ($Alert -gt '90') { Write-Host "Licenses or Certificate are fine" } ElseIf ($Alert -lt '89' -AND -g

我希望创建一个布尔脚本,当某些证书设置为过期时发出警报

我在查看90天以上、89天到61天、60天到31天以及不到30天的警报。警语现在不是一个大问题

我知道我还需要更多,但希望能在这方面有所帮助。我感谢大家的帮助

$Alert = 'TakeAction'

  If ($Alert  -gt '90')  {

       Write-Host "Licenses or Certificate are fine"

  }  ElseIf ($Alert  -lt '89' -AND -gt'61')  {

       Write-Host "Please contact Vendor for updated Licenses or Certificate information"

  }  ElseIf ($Alert  -lt '60' -AND -gt'31')  {

       Write-Host "Action must be taken to update Licenses or Certificate from Vendor"

  }  

}  ElseIf ($Alert  -lt '30' -AND -gt'1')  {

       Write-Host "Immediate action must be taken for renewal of Licenses or Certificate!"

  }  

Else {

       Write-Host "Certificate Information N/A"

} 

这是
开关
命令亮起的一种情况。它针对一个案例进行测试,如果为true,将执行关联的scriptblock。当在该脚本块中使用
Continue
命令时,它将跳过其余案例以再次测试

Switch ($Alert){
  { $_ -gt 90}  {

       Write-Host "Licenses or Certificate are fine"
       Continue

  }
  { $_ -gt 61}  {

       Write-Host "Please contact Vendor for updated Licenses or Certificate information"
       Continue

  }  
  { $_ -gt 31 }  {

       Write-Host "Action must be taken to update Licenses or Certificate from Vendor"
       Continue

  }  
  { $_ -gt 1 }  {

       Write-Host "Immediate action must be taken for renewal of Licenses or Certificate!"
       Continue
  }  

    Default {

       Write-Host "Certificate Information N/A"
  }
} 

在多种情况下,开关是一种选择。如果您想要警报,我通常使用
发送邮件消息
来接收警报。这将设置
[boolean]$sendMail=$true
,以评估是否应发送电子邮件(通知)

$certificateAge # = however you are getting your cert age
[boolean]$sendMail = $false
$message = @()
switch($certificateAge){
    {$_ -gt 90} 
    {
        $message = "Licenses or Certificate are fine"        
    }
    {$_ -lt 89 -and $_ -gt 61 } 
    {
        $message = "Please contact Vendor for updated Licenses or Certificate information"
        $sendMail = $true
    }
    {$_ -lt 60 -and $_ -gt 31 } 
    {
        $message = "Action must be taken to update Licenses or Certificate from Vendor"
        $sendMail = $true
    }
    {$_ -lt 30 -and $_ -gt 1 } 
    {
        $message = "Immediate action must be taken for renewal of Licenses or Certificate!"
        $sendMail = $true
    }    
    default{$message = "Certificate Information N/A"}
}


if($sendMail)
{
    Send-MailMessage -From CertAge@mail.com -To Admin@mail.com -SmtpServer mail.mail.com -Subject "Certificate information" -Body $message
}
你不能这样组合运算符。表达式的两边都需要一个操作数

试试这个:

If ($Alert -gt '90') {
    Write-Host "Licenses or Certificate are fine"
}
ElseIf ($Alert -lt '89' -AND $Alert -gt '61') {
    Write-Host "Please contact Vendor for updated Licenses or Certificate information"
}
ElseIf ($Alert -lt '60' -AND $Alert -gt '31') {
    Write-Host "Action must be taken to update Licenses or Certificate from Vendor"
}
ElseIf ($Alert -lt '30' -AND $Alert -gt '1') {
    Write-Host "Immediate action must be taken for renewal of Licenses or Certificate!"
Else {
    Write-Host "Certificate Information N/A"
} 

请注意,您使用的是大于(
-gt
)和小于(
-lt
),且不大于或等于(
-ge
)或小于或等于(
-le
)。这似乎是一个可能的错误。

类似于MadTechnician,但更接近于定义范围

0..95|%{
    $Alert = $_
    switch ($Alert) {
        {1..30  -contains $_}   {"$_ Immediate action must be taken for renewal of Licenses or Certificate!" ;break}
        {31..60 -contains $_}   {"$_ Action must be taken to update Licenses or Certificate from Vendor" ;break}
        {61..90 -contains $_}   {"$_ Please contact Vendor for updated Licenses or Certificate information" ;break}
        {$_ -gt 90}             {"$_ Licenses or Certificate are fine" ;break}
        Default                 {"$_ Certificate Information N/A"}
    }
}

1) 什么是“布尔脚本”?你是说“使用布尔运算符的脚本”吗?2) 你为什么要引用你的数字?这样做将导致PowerShell将它们作为字符串而不是数字进行比较。这是你的意图吗(如果是,为什么)?@Bill_Stewart PowerShell根据左手对象的类型与右手进行比较?所以如果
$Alert
[int32]
它会尝试将它们转换成数字,不是吗?是的,但是1)
$Alert
在OP的代码中包含一个字符串,2)为什么要添加不必要的引号?我不推荐这种方法,b/c
-contains
运算符比简单的
-gt
比较要慢得多。@AnsgarWiechers感谢您的提示,这是否也适用于
-in
中的
-是的。两者都在列表上进行线性搜索。
0..95|%{
    $Alert = $_
    switch ($Alert) {
        {1..30  -contains $_}   {"$_ Immediate action must be taken for renewal of Licenses or Certificate!" ;break}
        {31..60 -contains $_}   {"$_ Action must be taken to update Licenses or Certificate from Vendor" ;break}
        {61..90 -contains $_}   {"$_ Please contact Vendor for updated Licenses or Certificate information" ;break}
        {$_ -gt 90}             {"$_ Licenses or Certificate are fine" ;break}
        Default                 {"$_ Certificate Information N/A"}
    }
}