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
}
myhelpermonkeyAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Neil RussellTechnical Development LeadCommented:
#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

0
becraigCommented:
If you just want to suppress outputting the errors to screen, simply add this to the top of your script

$ErrorActionPreference = "SilentlyContinue"
0
myhelpermonkeyAuthor Commented:
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"
0
Creating Active Directory Users from a Text File

If your organization has a need to mass-create AD user accounts, watch this video to see how its done without the need for scripting or other unnecessary complexities.

Neil RussellTechnical Development LeadCommented:
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.
0
footechCommented:
Since the error from Get-ADComputer is a non-terminating error, setting the -errorAction to SilentlyContinue doesn't have any effect.  However, when using the -filter parameter if it doesn't find a match, no error is generated.
$dn = (Get-ADComputer -filter {Name -eq "$NewVUTag"} -Server $Domain -Credential $domaincreds).DistinguishedName

Open in new window

0
myhelpermonkeyAuthor Commented:
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.
0
becraigCommented:
using foo's line you could just evaluate for one of 2 things
if $true
if -ne $null

$dn = (Get-ADComputer -filter {Name -eq "$NewVUTag"} -Server $Domain -Credential $domaincreds).DistinguishedName
if ($dn -ne $null) {Remove-ADComputer -Identity "$dn" -Server $Domain -Credential $domaincreds -Confirm:$false?}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
footechCommented:
In fact, using what I posted, the existing check should work just fine.
if ($dn)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.