?
Solved

AD Join - Powershell

Posted on 2014-10-29
8
Medium Priority
?
188 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Does Powershell have you tied up in knots?

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 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
 
LVL 40

Assisted Solution

by:footech
footech earned 1000 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 1000 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 40

Expert Comment

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

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

The following article is intended as a guide to using PowerShell as a more versatile and reliable form of application detection in SCCM.
Previously, on our Nano Server Deployment series, we've created a new nano server image and deployed it on a physical server in part 2. Now we will go through configuration.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

765 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