Receive Location Autohandling false email alerts

63 Views Asked by At
$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
}

Example:

Disabled_RLs

and

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)
}

Question: when all RLs have the status "disabled" and all of these RLs are included in the exception list the value of the variable $ReceiveLocations should be false and I need to stop further processing in my script. (do nothing if all RLs are found in exception list, just exit)

But I'm still getting false email alerts. How can we set logic for not getting email alerts if there were no extra RLs found in $ReceiveLocations?

1

There are 1 best solutions below

2
On BEST ANSWER

The value of the variable $ReceiveLocations is $null when your Get-WmiObject statement doesn't return results. $null doesn't have a property Count, hence the check $ReceiveLocations.Count -eq 0 fails and your script doesn't terminate before sending an e-mail.

You can avoid this issue in a number of ways, e.g. by putting $ReceiveLocations in the array subexpression operator:

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

or you could use the way PowerShell interprets values in boolean expressions (non-empty arrays become $true, $null becomes $false):

if (-not $ReceiveLocations) {
    exit
}