我编写了一个函数,用于在 sip 域已存在的情况下使用 try/catch block 包装 new-cssipdomain cmdlet。 代码是:
function LHP-AddSIPDomain
{
param ( [string] $SIPDomain)
try
{
New-cssipdomain -id $SIPDomain
}
catch
{
Write-host "Lync specific exception occured adding SIP domain"
Write-host "Exception String:"+$_.Exception.Message
exit
}
}
LHP-AddSIPDOmain -SipDomain "Test206.com"
域已存在时的输出为:
New-CsSipDomain : "SipDomain" with identity "Test206.com" already exists. To modify
the existing item, use the Set- cmdlet. To create a new item, use a different
identity. Parameter name: Identity
At S:\Scripts\LHP-AddSIPDomain.ps1:33 char:26
+ New-cssipdomain <<<< -id $SIPDomain
+ CategoryInfo : InvalidArgument: (Test206.com:String) [New-CsSipDomain],
ArgumentException
+ FullyQualifiedErrorId :
InvalidIdentity,Microsoft.Rtc.Management.Xds.NewOcsSipDomainCmdlet
这应该由 try/catch block 捕获。 我尝试将 [system.exception] 添加到 catch 语句中。我还尝试设置 $erroraction=”Stop”。两者都没有任何不同,try/catch 语句似乎被忽略了。我已经使用这种类型的代码结构来捕获来自 new-aduser cmdlet 的错误,这似乎工作正常。
我也考虑并尝试首先使用 hte get-cssipdomin cmdlet 来检查 sip 域是否已存在,但我有一个类似的问题,如果您使用不存在的域调用 get-cscsipdomain ,它会抛出我似乎无法捕获错误。
如有任何建议,我们将不胜感激。
最佳答案
尝试:
try
{
New-cssipdomain -id $SIPDomain -ERRORACTION SilentlyContinue
}
也许命令本身有一个 try/catch 错误。
关于powershell - 在 powershell 中将 try/catch block 与 Lync 2010 cmdlet 结合使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7147632/