开发者

Using try/catch blocks with Lync 2010 cmdlets in powershell

I've written a function to wrap the the new-cssipdomain cmdlet with a try/catch block incase the sip domain already exists. Code is:

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"

The output when the domain already exists is:

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

This should be caught by the try/catch block. I've tried adding the [system.exception] to the catch statement. I'ev also tried setting $erroraction=”Stop”. Neither made any different, the try/catch statement seems to be being ignored. I've used this type of code structure to capture errors from the new-aduser cmdlet and this seemed to work ok.

I have also considerd and tried using hte get-cssipdomin cmdlet first to che开发者_高级运维ck if the sip domain already exists, but I have a similar problem in that if you call get-cscsipdomain with a domain that doesn't exist it throws an error which I don't seem to be able to catch.

Any suggestions would be greatly appreciated.


TRY:

try
   {
      New-cssipdomain -id $SIPDomain -ERRORACTION SilentlyContinue
   }

Maybe the command it self got a try/catch for errors.


You can perhaps have a look to this answer. It explains why try/catch is sometime not working.

Can't you just write :

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

And test the value of $Res ?


I guess the error you get is not a terminating error and that's why you can't catch it. Try to set the ErrorAction value to 'stop', that will make the error a terminating error and you'll be able to catch it in the catch block.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜