Solved

AD Join - Powershell

Posted on 2014-10-29
8
182 Views
Last Modified: 2014-10-29
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
}
0
Comment
Question by:myhelpermonkey
  • 2
  • 2
  • 2
  • +1
8 Comments
 
LVL 37

Expert Comment

by:Neil Russell
ID: 40411613
#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
 
LVL 29

Expert Comment

by:becraig
ID: 40411627
If you just want to suppress outputting the errors to screen, simply add this to the top of your script

$ErrorActionPreference = "SilentlyContinue"
0
 

Author Comment

by:myhelpermonkey
ID: 40411676
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
 
LVL 37

Expert Comment

by:Neil Russell
ID: 40411701
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
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
LVL 39

Assisted Solution

by:footech
footech earned 250 total points
ID: 40411703
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
 

Author Comment

by:myhelpermonkey
ID: 40411786
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
 
LVL 29

Accepted Solution

by:
becraig earned 250 total points
ID: 40411791
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
 
LVL 39

Expert Comment

by:footech
ID: 40411864
In fact, using what I posted, the existing check should work just fine.
if ($dn)
0

Featured Post

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Are you one of those front-line IT Service Desk staff fielding calls, replying to emails, all-the-while working to resolve end-user technological nightmares? I am! That's why I have put together this brief overview of tools and techniques I use in o…
Synchronize a new Active Directory domain with an existing Office 365 tenant
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now