Forum Discussion

Baron164's avatar
Baron164
Brass Contributor
Jul 25, 2022

Need help generating a uidNumber using the EmployeeID Attribute

Existing Employee's all have an EmployeeID Attribute which is anywhere from 2 to 5 numbers long. I need to take a user's existing EmployeeID number and turn it into a uidNumber that is 6 characters l...
  • farismalaeb's avatar
    farismalaeb
    Jul 26, 2022

    Baron164 

    Yes, You have some things to consider in your script.

    First $uidnum is invalid as it return the user DN "CN=User,DC=MyDC ...." not the employeeID

    Second , you need to cast the $empid to be Int not String otherwise the int conversion will fail.

    Here is an update

    Please Check and let me know

     

    $allusers = Get-ADUser -Filter "employeeID -ge 0" -Properties Name,employeeID,uidNumber
    
    foreach ($user in $allusers)
    {
        [int]$empid = (Get-ADUser $user -Property EmployeeID).EmployeeID
    
        if ($user.uidNumber -eq $null)
            {
                $uidnum = "1"+'{0:d5}' -f $empid
                Set-ADUser -Identity $user.SamAccountName -Replace @{uidNumber=$uidnum}
            }
            
    }
    

Resources