powershell - generate next available samaccountname in increments -
i trying generate next available ad account using incremental numbers. example, domain has accounts names"opr1000-opr1014", when run script, should expect opr1015, instead gets stuck in loop , never returns value. have running while loop , increasing numerical value in increments until finds unused value @ point while loop should no longer true , script should end. have ideas?
$account = "opr" $accountnum = "1000" $accountname = $account + $accountnum $accountint = $account + $int $int = [system.decimal]::parse($accountnum) do{ $query = "(&(objectclass=user)(samaccountname=$accountname))" $result = ([adsisearcher]$query).findone() if($result){$int++} }while($accountint) "$account$int"
there quite few mistakes, see this:
$account = "opr" $accountnum = 1000 { $accountname = $account + $accountnum; $query = "(&(objectclass=user)(samaccountname=$accountname))" $result = ([adsisearcher]$query).findone() if($result -eq $false) { break } $accountnum++ } while($true)
Comments
Post a Comment