2016-12-18 26 views
0
$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 
} 

例子:接收位置Autohandling虚假的电子邮件警报

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包含在例外列表t中他变量$ReceiveLocations应该是错误的,我需要停止在我的脚本中进一步处理。 (如果在例外列表中找到所有RL,请不要执行任何操作)

但是我仍然收到错误的电子邮件警报。如果在$ReceiveLocations中没有发现额外的RL,我们如何设置逻辑以避免收到电子邮件警报?

回答

1

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

您可以通过多种方式避免此问题,例如,通过将$ReceiveLocationsarray subexpression operator

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

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

if (-not $ReceiveLocations) { 
    exit 
} 
+0

谢谢你!!!!!! ! –

+1

++,但值得注意的是,在PSv3中引入的标量和集合的统一处理(methinks)_does_允许在空值表达式上使用'.Count'并合理地返回'0':'$ null.Count -eq 0 '在PSv3 +中返回'$ true',而在PSv2-中返回'$ false'。 – mklement0

+1

我以为它没有,但我刚刚证实,你是对的。不过,如果仅出于兼容性原因,我可能仍然不会依赖它。 –