Link to home
Start Free TrialLog in
Avatar of myhelpermonkey
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).DistinguishedName
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
}
Avatar of Neil Russell
Neil Russell
Flag of United Kingdom of Great Britain and Northern Ireland image

#Make sure there is not an AD Object with the new computer name

$Computer = Get-ADComputer "$NewVUTag" -Server $Domain -Credential $domaincreds -erroraction SilentlyContinue
if ($Computer -ne $null )
{
    $dn = $computer.DistinguishedName
    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

}

Open in new window

Avatar of becraig
If you just want to suppress outputting the errors to screen, simply add this to the top of your script

$ErrorActionPreference = "SilentlyContinue"
Avatar of myhelpermonkey
myhelpermonkey

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"
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.
SOLUTION
Avatar of footech
footech
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
In fact, using what I posted, the existing check should work just fine.
if ($dn)