2

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 check 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.

Arcass
  • 932
  • 10
  • 19

3 Answers3

2

TRY:

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

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

CB.
  • 58,865
  • 9
  • 159
  • 159
  • Excellent, problem solved! Turns out the error was non-terminating so the $ErrorAction variable didn't affect it. I recevied another suggestion of setting the $ErrorActionPreference = "Stop". Thanks – Arcass Aug 22 '11 at 13:06
1

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 ?

Community
  • 1
  • 1
JPBlanc
  • 70,406
  • 17
  • 130
  • 175
1

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.

Shay Levy
  • 121,444
  • 32
  • 184
  • 206