Guest User

powershell scripts for adding rfc2307 attributes

a guest
Jan 29th, 2015
308
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. $id_offset = 300000
  2. $homedir_root = "/home/example.com"
  3. $login_shell = "/bin/bash"
  4. $search_bases = @("OU=Organisation,DC=example,DC=com", "CN=Users,DC=example,DC=com")
  5.  
  6. Write-Host "Using id offset ${id_offset}"
  7.  
  8. foreach ($search_base in $search_bases) {
  9. Write-Host "`nGenerating Unix attributes for user/group objects in ${search_base}"
  10.  
  11. foreach ($ad_user in Get-ADUser -Filter * -SearchBase $search_base -Properties PrimaryGroupID) {
  12. $sid_uid = [int]"$($ad_user.SID)".split('-')[7]
  13. $username = $ad_user.SamAccountName
  14. $uid = $id_offset + $sid_uid
  15. $primary_group_gid = $id_offset + [int]$ad_user.PrimaryGroupID
  16.  
  17. if ($uid -eq $id_offset -Or $primary_group_gid -eq $id_offset) {
  18. continue
  19. }
  20.  
  21. $homedir = "${homedir_root}/${username}"
  22.  
  23. Write-Host "${username}:*:${uid}:${primary_group_gid}:$($ad_user.name):${homedir}:${login_shell}"
  24. Set-ADUser -Identity $username -Replace @{uidNumber=$uid;gidNumber=$primary_group_gid;unixHomeDirectory=$homedir;loginShell=$login_shell}
  25. }
  26.  
  27. foreach ($ad_group in Get-ADGroup -Filter * -SearchBase $search_base) {
  28. $sid_gid = [int]"$($ad_group.SID)".split('-')[7]
  29. $groupname = $ad_group.SamAccountName
  30. $gid = $id_offset + $sid_gid
  31.  
  32. if ($gid -eq $id_offset) {
  33. continue
  34. }
  35.  
  36. Write-Host "GID for group ${groupname}: ${gid}"
  37. Set-ADGroup -Identity $groupname -Replace @{gidNumber=$gid}
  38. }
  39. }
RAW Paste Data