[PowerShell] AD enable UNIX Attributes

When using Active Directory and UNIX Attributes (User/Group Unique Identifiers) the first thing you need is the Identity Management for UNIX Components. Now you are ready to start using UNIX Attributes.

Adding UNIX Attributes to every user and group in your domain, using PowerShell.

Get every user that already has UNIX Attributes enabled, only change the and the to you specifics:

Remove-Variable -Name * -Force -ErrorAction SilentlyContinue
Import-Module ActiveDirectory
$usuarios = Get-ADUser -Filter * -SearchBase "DC=;,DC=" -Properties:* | sort SamAccountName
foreach($usr in $usuarios){write-host $usr.SamAccountName,$usr.mssfu30nisdomain,$usr.uidnumber}

Setting the Unix IDs it a bit more difficult, you need to do 4 thing:
1. Get the last Unix User ID available
2. Enable NIS on the User account
3. Give the user a unique Unix User ID
4. Make the user member of a Unix Group

Set every user that didn’t receive a UNIX Attributes yet, only change the and the to you specifics:

Remove-Variable -Name * -Force -ErrorAction SilentlyContinue
Import-Module ActiveDirectory
$NIS = Get-ADObject "CN=NISdomain,CN=ypservers,CN=ypServ30,CN=RpcServices,CN=System,DC=myDomain,DC=local" -Properties:* #Get NIS server information
$maxUid = $NIS.msSFU30MaxUidNumber #Get the last used User ID

$usuarios = Get-ADUser -Filter * -SearchBase "DC=myDomain,DC=local" -Properties:* #Get all users
foreach($usr in $usuarios)
{
  if ($usr.mssfu30nisdomain -eq $null){
  Set-ADUser -Identity "$($usr.SamAccountName)" -Replace @{mssfu30nisdomain = "NISdomain"} #Enable NIS
  Set-ADUser -Identity "$($usr.SamAccountName)" -Replace @{gidnumber="10000"} #Set Group ID
  $maxUid++ #Raise the User ID number
  Set-ADUser -Identity "$($usr.SamAccountName)" -Replace @{uidnumber=$maxUid} #Set User ID
  Write-Host -Backgroundcolor Green -Foregroundcolor Black $usr.SamAccountName changed #Write Changed Username to console
  }
  else{Write-Host -Backgroundcolor Yellow -Foregroundcolor Black $usr.SamAccountName unchanged} #Write Unchanged Username to console with a yellow background
}