尝试捕获对于Get-CsUser命令不起作用

问题描述 投票:0回答:1

我已经设置了一个Try Catch块来检查Skype用户是否存在,并且该命令引发错误,但是Catch块无法捕获它并显示正确的错误消息。

这里是代码,后面是在Windows PowerShell ISE中引发的错误消息

try
{
    $Output = Get-CsUser -Identity "Chrlis.domain\$username"

} Catch
{
    If($error[0] -like "*Management object not found for identity*") {
        Write-Host "SKYPE LYNC ERROR3: $username does not have a Skype account" -ForegroundColor Red
    } Else {
        Write-Host "SKYPE LYNC ERROR4:" $Error[0].Exception -ForegroundColor Red
    }
}

Management object not found for identity "Chrlis.domain\Jacob.Hal".
    + CategoryInfo          : InvalidData: (Chrlis.domain\Jacob.Hal:UserIdParameter) [Get-CsUser], ManagementException
    + FullyQualifiedErrorId : Identity,Microsoft.Rtc.Management.AD.Cmdlets.GetOcsUserCmdlet
    + PSComputerName        : pigeon1.Chrlis.domain

任何帮助将不胜感激。

powershell powershell-4.0 skype skype-for-business
1个回答
0
投票

您可以将-erroractionpreference参数修改为静默继续,然后执行.count()或.length()检查变量和操作中是否确实存在任何内容。

我会提供样板代码,但在移动设备上有点困难。

请仔细阅读Ed的帖子-https://devblogs.microsoft.com/scripting/hey-scripting-guy-how-can-i-use-erroractionpreference-to-control-cmdlet-handling-of-errors/

© www.soinside.com 2019 - 2024. All rights reserved.