locked
Stumped while trying to validate AD Objects RRS feed

  • Question

  • 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.

    Mike Bell


    Mike B.

    Tuesday, July 28, 2015 5:16 PM

Answers

  • 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)


    • Proposed as answer by jrv Tuesday, July 28, 2015 5:39 PM
    • Marked as answer by Bellm79 Tuesday, July 28, 2015 6:11 PM
    Tuesday, July 28, 2015 5:24 PM

All replies

  • 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?


    -- Bill Stewart [Bill_Stewart]

    Tuesday, July 28, 2015 5:21 PM
  • 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)


    • Proposed as answer by jrv Tuesday, July 28, 2015 5:39 PM
    • Marked as answer by Bellm79 Tuesday, July 28, 2015 6:11 PM
    Tuesday, July 28, 2015 5:24 PM
  • 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.


    Mike B.

    • Marked as answer by Bellm79 Tuesday, July 28, 2015 6:11 PM
    • Unmarked as answer by Bellm79 Tuesday, July 28, 2015 6:11 PM
    Tuesday, July 28, 2015 6:11 PM
  • Cheers, you're very welcome. Glad it worked out.

    Tuesday, July 28, 2015 6:14 PM
  • 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.


    Mike B.

    Tuesday, July 28, 2015 6:14 PM