Using Powershell to Loop check usernames from csv, loop through AD and add a number if needed -
here scenario,
i wrote sql query extracts user accounts null username our student information system. lets assume these newly enrolled students. want take list, has column of suggested usernames, done simple concatenation in sql query. want loop through csv , check make sure username doesn't exist in ad , if does, append next available number username.
so in test environment have csv looks this. ( made testing)
studentid,first,last,suggestedusername
12345,tony,test,testto
54321,tolly,test,testto
i test ad environment have student named tommy test or testto, in case, powershell script should tell me tony test should testto1 , tolly test should testto2. making sense?
the meat of script works, read csv, loop through ad , return testto1 line 1 of csv, problem not read line 2, script ends
i have been playing around arrays in script here have far
import-module activedirectory add-pssnapin quest.activeroles.admanagement $useraccounts =@() $useraccounts = import-csv "path\test.csv" $userbase = get-qaduser -sizelimit 0 -searchroot 'mydomain.com/ou' foreach ($user in $useraccounts) { if ($userbase) { $userbase = $userbase | select samaccountname | %{($_ -split "@")[0]} $usernumbers = @() $userbase | % { if ($_ -match '\d+') { $usernumbers += $matches[0] } } $maxusernumber = ($usernumbers | measure-object -max).maximum $suggestedusername = $user+($maxusernumber+1) } else { $suggestedusername = $user } } write-host $suggestedusername
ok, loop doesn't appear cycling because aren't using array of strings output, using string, shows last loop. if solution that's neither here nor there, because think have better option you. sake of simplicity, csv , delete suggested name column , run against that.
$users = import-csv "path\test.csv" $users|%{$_|add-member userid ("$($_.last)$($_.first.substring(0,2))")} $nameconflicts = $users|group userid|?{$_.count -gt 1} foreach($name in $nameconflicts){ $x=0 if(dsquery user -samid "$($name.name)*"){ $x = get-aduser -filter {samaccountname -like "$($name.name)*"}|%{$_.samaccountname -replace "($($name.name))(\d*)","`$2"}|sort -descending |select -first 1 } for($i=if($x -gt 0){0}else{1};$i -lt ($name.count);$i++){ $name.group[$i].userid = "$($name.name)$($i+$x)" } } $users|group userid|?{$_.count -eq 1}|%{if(dsquery user -samid "$($_.name)"){$_.group[0].userid = "$($_.name)1"}} $users|ft -auto
that loads list, creates potential user names taking last name , first 2 letters of first name. groups that, , finds of them have more one. each of checks ad existing accounts names that, , takes number off end of name, , selects highest one. if found in existence renames of potential user names append next available number end (if none found in ad leaves first one, , renames rest of them). lastly checks of other names, there 1 user name, , if in ad adds 1 end of user id.
this should run faster script, because i'm searching names needed checked, , it's filtering @ provider level instead of taking names, , filtering through powershell.
Comments
Post a Comment