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 long. Each uidNumber needs to start with "1" and then any additional digits would be "0".

For example:
EmployeeID 35 becomes uidNumber 100035
EmployeeID 1235 becomes uidNumber 101235
EmployeeID 12356 becomes uidNumber 112356

Running a foreach loop for each user with an EmployeeID is straight forward. But I'm lost as to how best to take the EmployeeID and convert it into a 6 digit long number for the uidNumber attribute.

Any help would be greatly appreciated.


  • 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}
            }
            
    }
    
    • Baron164's avatar
      Baron164
      Brass Contributor

      farismalaeb 

      Thanks, this is what I have so far but it's not working. 

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



       

      • farismalaeb's avatar
        farismalaeb
        Steel Contributor

        Baron164 

        Wont work like this.

        make it like this

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

Resources