I'm trying to pickup the error generated by add-computer on my powershell script. What I'm trying to achieve here is if I try to add a computer to a domain and it already exist I would like to ask the user if he wants to remove and and retry.
$result = 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))) -ErrorAction SilentlyContinue -ErrorVariable computerError
Write-Host $computerError[0]So that comes back with my error "Can't add the computer to the domain as it already exist" What I would like to trap is the error code so I could switch on that for different scenarios.
Thank you for the help