我试图找出如何检索从Active Directory中已经有计算机添加错误。 这是我用来在我的域中添加我的机器的脚本。
Add-Computer -DomainName "myDomain" -OUPath "ou=Postes de travail,ou=Ordinateurs,ou=TRB,DC=,dc=,dc=,dc=" -credential (New-Object System.Management.Automation.PSCredential ("myUser", (ConvertTo-SecureString "myPassword" -AsPlainText -Force))) -PassThru -ErrorVariable $test -OutVariable $test1
在我的Powershell ISE中,我看到错误,说我的机器已经存在,但我试图陷阱,所以我可以问用户是否要删除它,然后尝试重新添加计算机。
谢谢
将其与-ErrorAction参数一起使用:
Add-Computer ... -ErrorAction SilentlyContinue -ErrorVariable computerError
ErrorVariable是一个数组,所以产生的错误将被存储在:
$computerError[0]
要一次又一次地使用同一个variables,在var名称前面使用+ :
Add-Computer -ErrorVariable +manyErrors
而最后的错误将永远是:
$manyErrors[$manyerrors.count - 1]