Q: Verifying an AD computer name

This topic contains 2 replies, has 2 voices, and was last updated by Profile photo of Jack Neff Jack Neff 1 year, 10 months ago.

  • Author
    Posts
  • #22279
    Profile photo of CTX_Christian
    CTX_Christian
    Participant

    Hi again NG,

    after I got very quick and good help regarding the iteration of the latest AD computer account I'm stuck again in my script.

    This time I want to verify my new AD computer name (no object yet) before I create it against 5 different sets of collections.

    This is the actual code I'm using currently:

    #########################

    write-output "++++++++++++++++++++++++++++++++++++++++"
    write-output "Verifying NEW AD Account $newADaccount ..."
    write-output "++++++++++++++++++++++++++++++++++++++++"

    #1. Verify against Provtask list VDIs
    $Result = $arrprovmachines | where {$_ -eq $newADaccount}
    write-host -ForegroundColor Yellow "Verify against Provtask list VDIs"
    check-result

    #2. Verify against the Active Directory
    $Result = get-adcomputer -filter {name -eq $newADaccount}
    write-host -ForegroundColor Yellow "Verify against the Active Directory"
    check-result

    #3. Verify against Identity Pools
    $Result = $arridmachines | where {$_ -eq $newADaccount}
    write-host -ForegroundColor Yellow "Verify against Identity Pools"
    check-result

    #4. Verify against XenServer VMs
    $Result = $XenVMs | where {$_ -eq $newADaccount}
    write-host -ForegroundColor Yellow "Verify against XenServer VMs"
    check-result

    #5. Verify against Brokermachines
    $Result = $brokermachines | where {$_ -eq $newADaccount}
    write-host -ForegroundColor Yellow "Verify against Brokermachines"
    check-result

    ###########################

    The "check-result" function used to be an if else to check wether $result contained any data. If it contained any data I wrote a warning and a break. Else would write that the AD computer name is valid.

    My problem is, that I'm stuck if one of the verifications is positive. It will report that the AD computer name is already in use and the script just stops. Well I guess that's the "break" in my IF ELSE.

    I'm not sure but I believe I have to put the tests into a loop....but I don't know in what kind of loop. It get even more tricky – to me at least – that the AD computer name must be iterated again if a verifications turnes out positive.

    I try to display it with text a bit:

    AD computer name –> TEST 1, TEST 2, TEST3, TEST4, TEST5 –> OK

    AD computer name –> TEST 1, TEST 2 –> TEST FAIL because NAME seems in use! –> INTERATE AD commputer name by 1 —> Repeat TESTs

    So you see these are actually the two scenarios I'm facing. The verification might fail at any test.

    My idea is to setup a loop – well this is what I hope to learn here – and put this code in there:

    Write-warning "NEW AD Account $newADaccount has already been used!"

    # split the number from the chars
    $strnumber = $newADaccount.Substring(9)

    # convert string to integer
    $intnumber = [int] $strnumber

    # interate by one
    $intnumber++

    # combine new AD computer account name
    $newADaccount = "USLAXDT" + $intnumber

    Regards

    Christian

  • #22297
    Profile photo of CTX_Christian
    CTX_Christian
    Participant

    Hi again,

    I did some further research.

    Is it possible to create a do/while loop and place multple conditions in there?

    My idea would be:

    # Functions
    function iterate-ADaccount
    {
    Write-warning "NEW AD Account $newADaccount has already been used!"

    # split the number from the chars
    $strnumber = $newADaccount.Substring(9)

    # convert string to integer
    $intnumber = [int] $strnumber

    # interate by one
    $intnumber++

    # combine new AD computer account name
    $newADaccount = "USLAXDT" + $intnumber

    }

    do
    {
    write-output "++++++++++++++++++++++++++++++++++++++++"
    write-output "Verifying NEW AD Account $newADaccount …"
    write-output "++++++++++++++++++++++++++++++++++++++++"

    #1. Verify against Provtask list VDIs
    $Result = $arrprovmachines | where {$_ -eq $newADaccount}
    write-host -ForegroundColor Yellow "Verify against Provtask list VDIs"

    #2. Verify against the Active Directory
    $Result = get-adcomputer -filter {name -eq $newADaccount}
    write-host -ForegroundColor Yellow "Verify against the Active Directory"

    #3. Verify against Identity Pools
    $Result = $arridmachines | where {$_ -eq $newADaccount}
    write-host -ForegroundColor Yellow "Verify against Identity Pools"
    check-result

    #4. Verify against XenServer VMs
    $Result = $XenVMs | where {$_ -eq $newADaccount}
    write-host -ForegroundColor Yellow "Verify against XenServer VMs"

    #5. Verify against Brokermachines
    $Result = $brokermachines | where {$_ -eq $newADaccount}
    write-host -ForegroundColor Yellow "Verify against Brokermachines"
    }
    while (Result -eq $TRUE; iterate-ADaccount)

    ############### END Of CODE #################

    My problem is that ISE tell me that the while loop is missing a closing and an unexpected token...

    What's the error here?

    Regards

    Christian

  • #22345
    Profile photo of Jack Neff
    Jack Neff
    Participant

    The error is in your while loop declaration. You attempt to call function Iterate-ADAccount but that must be done within the curly braces { }. A do/while loop isn't the best choice you just need a while loop. Here's a trimmed down version and I'm just assuming your method for finding the number inside the name string works. I have not tested it myself and since your original code doesn't show how you populate all the name arrays I didn't bother either.

    $NewADAccount = Read-Host "Type a name"
    $ADMachines = Get-ADComputer -Filter {Name -eq $NewADAccount} | Select-Object -Expand Name
    $NameArray = $ArrProvMachines,$ArrIDMachines,$XenVMs,$BrokerMachines,$ADMachines
    
    while ($NameArray -contains $NewADAccount){
        Write-Warning "$NewADAccount already in use"
        $Index = [int]$NewADAccount.Substring(9)
        $NewADAccount = "USLAXDT" + ($Index + 1).ToString()
    }
    
    Write-Output "$NewADAccount is an available name"
    

You must be logged in to reply to this topic.