powershell - 在 powershell 中将 try/catch block 与 Lync 2010 cmdlet 结合使用

标签 powershell lync-2010

我编写了一个函数,用于在 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/

相关文章:

json - 如何在powershell中输出从REST api返回的json对象?

python - 在windows powershell中模拟python3 shell命令行为

rtp - 捕获 Lync 视频流

c# - 如何为 Lync 2010 和 Skype For Business 编码

lync - 是否可以强制 Lync 立即保存对话历史记录?

.net - Powershell - IO.Directory - 查找所有子目录中的文件类型

powershell - Powershell获取服务问题

powershell - 如何在新的隐藏窗口中启动此 PowerShell 脚本

sip - LYNC - SIP "413 - Request entity too large"订阅请求错误

c# - 使用 Lync Client API 2010 拒绝后如何接受传入的视频调用?