通过 Service-now 执行脚本时无法索引到空数组

时间:2021-03-24 09:32:33

标签: arrays powershell null

我在通过 Service-now 执行 powershell 脚本时遇到以下错误。该脚本连接到 EXO 并检查电子邮件 ID 是否存在,如果存在,则在电子邮件地址中添加一个数字。该脚本在我系统上的 Powershell 上运行良好。

错误:

Failer description: [object Object] *** Cannot index into a null array.
HRESULT: [-2146233087]
Stack Trace:    
at System.Management.Automation.ExceptionHandlingOps.CheckActionPreference(FunctionContext funcContext, Exception exception)
at System.Management.Automation.Interpreter.ActionCallInstruction`2.Run(InterpretedFrame frame)
at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)
at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)

脚本失败的部分如下:

function checkemailid {
    param($smtppAddr, [int]$times)
    
    <#Check if emailid exist, if not then the output is the smtpaddress#>
    if(!(Get-Recipient $tempAddr)) {
        return $smtpAddr
    } else {
        <#If the email id exist, add a suffix number to make it unique#>
        [int]$count = $times + 1
        [string]$tempAddr = $IniAddr.split("@")[0] + "$count" + "@" + $IniAddr.split("@")[1]
        checkemailid $tempAddr $count
    }
}
$tempAddr = "abc@xyz.com"
$IniAddr = $tempAddr  
$UserPrimarySMTP = checkemailid $tempAddr 0
$UserPrimarySMTP

0 个答案:

没有答案