我正在使用PowerShell脚本更改本地帐户名称。当然,第一步是检查账号是否存在:使用ADSI处理错误
$user=[ADSI]"WinNT://$server/$oldName,user"
如果账号存在,那就没问题了。但是,如果没有,那么我得到这个错误:
format-default : The following exception occurred while retrieving member >"distinguishedName": "The user name could not be found." + CategoryInfo : NotSpecified: (:) [format-default], ExtendedTypeSystemException + FullyQualifiedErrorId : CatchFromBaseGetMember,Microsoft.PowerShell.Commands.FormatDefaultCommand
我无法弄清楚如何寻找这个错误,报告类似“$使用oldName未找到”,然后继续。从我所知道的情况来看,它并没有被引入错误变量,所以我无法搜索“无法找到用户名”字符串。 Try-Catch-Finally似乎忽略了这个错误。
我承认我在错误处理方面很弱。看起来有无数种方法可能导致失败,并且我的用户在使用我的脚本时总是会找到新的方法。
我添加了一个可能有帮助的链接问题的答案,在这里:https://stackoverflow.com/questions/43145567/powershell-directoryservice-object-error-not-catched-neither-trapped/43617003 – 2017-04-26 09:24:46