接收位置自动处理虚假电子邮件警报

时间:2016-12-18 20:13:07

标签: powershell biztalk

$exceptionList = Get-Content C:\Users\Dipen\Desktop\Exception_List.txt

$ReceiveLocations = Get-WmiObject MSBTS_ReceiveLocation -Namespace 'root\MicrosoftBizTalkServer' -Filter '(IsDisabled = True)' |
                    Where-Object { $exceptionList -notcontains $_.Name }

# Exit the script if there are no disabled receive locations
if ($ReceiveLocations.Count -eq 0)
{
    exit
}

示例:

Disabled_RLs

Exception_List

$mailBodyPT = ""

$mailTextReportPT = "There are: "
[STRING]$Subject = $SubjectPrefix + $BizTalkGroup
$mailTextReportPT += "in the BizTalk group: " + $BizTalkGroup + "." 

#Send mail
foreach ($to in $EmailTo) 
{
    $Body = $HTMLmessage
    #$SMTPClient = New-Object Net.Mail.SmtpClient($PSEmailServer) 
    $message = New-Object Net.Mail.MailMessage($from, $to, $Subject, $Body)
    $message.IsBodyHtml = $true;
    $SMTPClient.Send($message)
}

问题:当所有RL的状态为“已禁用”且所有这些RL都包含在异常列表中时,变量$ReceiveLocations的值应为false,我需要在脚本中停止进一步处理。 (如果在例外列表中找到所有RL,则不执行任何操作,只需退出)

但我仍然收到虚假的电子邮件提醒。如果在$ReceiveLocations中找不到额外的RL,我们如何设置不获取电子邮件警报的逻辑?

1 个答案:

答案 0 :(得分:1)

$ReceiveLocations语句未返回结果时,变量$null的值为Get-WmiObject$null没有属性Count,因此检查$ReceiveLocations.Count -eq 0失败,您的脚本在发送电子邮件之前不会终止。

您可以通过多种方式避免此问题,例如:将$ReceiveLocations放入array subexpression operator

if (@($ReceiveLocations).Count -eq 0) {
    exit
}

或者您可以使用PowerShell解释values in boolean expressions的方式(非空数组变为$true$null变为$false):

if (-not $ReceiveLocations) {
    exit
}