Solved

AD Join - Powershell

Posted on 2014-10-29
8
178 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 28

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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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 28

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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

How to sign a powershell script so you can prevent tampering, and only allow users to run authorised Powershell scripts
Synchronize a new Active Directory domain with an existing Office 365 tenant
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

760 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

22 Experts available now in Live!

Get 1:1 Help Now