Stumped while trying to validate AD Objects

Greetings Folks,

I was hoping the community could help out this.  I created a script that runs in a do loop with try and catch.  You input the server name, it checks to see if the object exists and if it doesn't, it will prompt you to enter the object name again.  My issue is that the script keeps running in a loop even when the server name is correct.  I'm not sure how to fix this.  I suspect that the line that is bolded below is the problem.

do{

$servername

=read-host"Please enter Linux Server name"

try{

           

$remove=Get-ADObject"CN=syssec-$servername,OU=servers,OU=NISNetgroups,OU=Applications,DC=edj,DC=devjones,DC=com"


        }

       

catch{Write-Host"Syssec Object not found"}


}

until($remove-eq"$remove")

Remove-ADObject$remove

Thanks for the help in advance.

Mik

July 28th, 2015 1:20pm

Your code is hard to read. Please try to reformat it.

Pay careful attention to the exit condition for the loop.

What is the condition $remove -eq "remove" supposed to do?

Free Windows Admin Tool Kit Click here and download it now
July 28th, 2015 1:24pm

Hi Mike,

Here's a tweak:

do {

    $serverName = Read-Host 'Enter servername'

    try {

        $machine = Get-ADObject "CN=$serverName,OU=servers,OU=NISNetgroups,OU=Applications,DC=edj,DC=devjones,DC=com" -ErrorAction Stop

    } catch {

        Write-Host "$serverName not found" -ForegroundColor Red

    }

} Until ($machine)

July 28th, 2015 1:28pm

Thanks Mike,

That tweak works great.  I understand the code you wrote now. 

Until ($machine) simply carries the output from $machine = Get-ADObject "CN=$serverName,OU=servers,OU=NISNetgroups,OU=Applications,DC=edj,DC=devjones,DC=com" -ErrorAction Stop when a value is present.  I didn't know you could do that without having to act upon that value. 

That's invaluable information.

Thanks again.  You guys are always the best.

Mike B.

Free Windows Admin Tool Kit Click here and download it now
July 28th, 2015 2:15pm

Cheers, you're very welcome. Glad it worked out.
July 28th, 2015 2:18pm

Hey Bill,  The other Mike helped to get me straightened.  It was my exit condition that was not correct.  In the statement $remove -eq "$remove", I was trying to say that if $remove has a value, then set itself to be equal to itself, but that was not the correct formatting.

I love coming to you guys because you keep encouraging me to learn and to get my syntax correct.

Mike B.

Free Windows Admin Tool Kit Click here and download it now
July 28th, 2015 2:18pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics