Session 远程AD会话错误捕获问题

Session 远程AD会话错误捕获问题,session,powershell,Session,Powershell,我试图在我的远程PoSH脚本中捕获异常,根据它是本地还是远程PoSH会话,我得到了不同的行为。第一个代码按预期工作,用户无法启用,因为它没有密码,因此被ADPasswordComplexityException捕获 导入模块activedirectory try { Set-ADUser user123 -ErrorAction "stop" -Enabled $true } catch [Microsoft.ActiveDirectory.Management.ADPasswordComp

我试图在我的远程PoSH脚本中捕获异常,根据它是本地还是远程PoSH会话,我得到了不同的行为。第一个代码按预期工作,用户无法启用,因为它没有密码,因此被ADPasswordComplexityException捕获

导入模块activedirectory

try { Set-ADUser user123 -ErrorAction "stop" -Enabled $true  } 
catch [Microsoft.ActiveDirectory.Management.ADPasswordComplexityException] { write-host "gotya" } 
catch { write-host "err" }
…导致gotya被写下来。无论我是在DC或其他PC上本地运行代码,都会发生这种情况。所以在那里一切都很好

但是,通过远程会话执行此操作时,不仅初始捕获不起作用,第二个捕获也不起作用

$ADS = New-PSSession -computername dc.domain -Authentication default -Credential $(get-credential)
Import-Module ActiveDirectory -PSSession $ADS   

try { Set-ADUser user123 -ErrorAction "stop" -Enabled $true  } 
catch [Microsoft.ActiveDirectory.Management.ADPasswordComplexityException] { write-host "gotya" } 
catch { write-host "err" }
远程运行时,可能会跳过所有捕获:

The password does not meet the length, complexity, or history requirement of the domain.
    + CategoryInfo          : InvalidData: (user123:ADUser) [Set-ADUser], ADPasswordComplexityException
    + FullyQualifiedErrorId : The password does not meet the length, complexity, or history requirement of the etc..
另外,当我在DC上本地运行远程脚本时,奇怪的是,我得到了一个不同的错误:

Unable to find type [Microsoft.ActiveDirectory.Management.ADPasswordComplexityException]: make sure that the assembly
containing this type is loaded.
At line:1 char:7
+ try { Set-ADUser -ErrorAction "stop" user123 -Enabled $true  }
+       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (Microsoft.Activ...lexityException:TypeName) [], RuntimeException
    + FullyQualifiedErrorId : TypeNotFound
知道波什为什么恨我吗?提前谢谢