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