繁体   English   中英

在Powershell中将try / catch块与Lync 2010 cmdlet一起使用

[英]Using try/catch blocks with Lync 2010 cmdlets in powershell

我编写了一个函数,在sip域已经存在的情况下,用try / catch块包装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块捕获。 我尝试将[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有时不起作用。

你不能只写:

$Res = New-cssipdomain -id $SIPDomain -ERRORACTION SilentlyContinue

并测试$ Res的值?

我猜您得到的错误不是终结错误,这就是为什么您无法捕获它。 尝试将ErrorAction值设置为“ stop”,这将使该错误成为终止错误,您可以在catch块中捕获该错误。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM