myhelpermonkey
asked on
AD Join - Powershell
Is there a way to hide any errors in the following portion of my script? It basically checks to make sure the computer object does not exist in AD prior to joining, however, often times it is a new object and will give an error stating that it could not find it in AD. If there was a way to tweak it so it will skip the removal attempt if it does not detect it that would be great.
#Make sure there is not an AD Object with the new computer name
$dn = $null
$dn = (Get-ADComputer "$NewVUTag" -Server $Domain -Credential $domaincreds).Distinguishe dName
if ($dn){
Add-Content -PassThru -Path $Filename "$(Get-Date -Format s) - Removing existing AD Object with the new computer name: $dn..."
Remove-ADComputer -Identity $dn -Server $Domain -Credential $domaincreds -Confirm:$false -WarningAction SilentlyContinue
$dn = $null
}
#Make sure there is not an AD Object with the new computer name
$dn = $null
$dn = (Get-ADComputer "$NewVUTag" -Server $Domain -Credential $domaincreds).Distinguishe
if ($dn){
Add-Content -PassThru -Path $Filename "$(Get-Date -Format s) - Removing existing AD Object with the new computer name: $dn..."
Remove-ADComputer -Identity $dn -Server $Domain -Credential $domaincreds -Confirm:$false -WarningAction SilentlyContinue
$dn = $null
}
If you just want to suppress outputting the errors to screen, simply add this to the top of your script
$ErrorActionPreference = "SilentlyContinue"
$ErrorActionPreference = "SilentlyContinue"
ASKER
It still is giving me the error when I enter in an object that is not in AD. stating that it cannot find the object with identify....
Is there any easy way to completely hide that error? I even added the $ErrorActionPreference = "SilentlyContinue"
Is there any easy way to completely hide that error? I even added the $ErrorActionPreference = "SilentlyContinue"
If you are using the code I gave you, it can never get to the REMOVE-ADComputer line IF the computer does not exist.
So how exactly are you getting the error and on EXACTLY what line. Can you paste the error please.
So how exactly are you getting the error and on EXACTLY what line. Can you paste the error please.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Would the next line just be Remove-ADComputer -Identity "$dn" -Server $Domain -Credential $domaincreds -Confirm:$false?
My reason for asking is that I am getting an error stating that it cannot bind the parameter to the target because the argument is null or empty.
My reason for asking is that I am getting an error stating that it cannot bind the parameter to the target because the argument is null or empty.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
In fact, using what I posted, the existing check should work just fine.
if ($dn)
if ($dn)
Open in new window