Forum Discussion
Need help generating a uidNumber using the EmployeeID Attribute
- Jul 26, 2022
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} } }
- Baron164Jul 25, 2022Brass 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 } }
- farismalaebJul 26, 2022Steel Contributor
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 } }
- Baron164Jul 26, 2022Brass Contributor
farismalaeb
Still not working, the script runs but it doesn't do anything and doesn't give me any errors.
In my testing environment the uidNumber attribute is "<not set>" so I don't think the $user.uidNumber -eq 0 line was working. Once I changed it, the script now appears to at least be trying to set the attribute. However I've been unable to figure out how to use set-aduser to set the uidNumber attribute.
This is what I change it to.$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 $null) { $uidnum = "1"+'{0:d5}' -f $empid Set-ADUser -Identity $user -Replace @{uidNumber=$uidnum} } }
This is the error I'm getting.