forked from joerod/powershell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathad_set_uid.ps1
62 lines (52 loc) · 1.64 KB
/
ad_set_uid.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# Sets group IDs for Users in Active Directory
Param(
[Parameter(Position=0,mandatory=$true)]
[string]$userName
)
Function checkAccount{
Get-ADUser -Identity $userName
}
Function checkPrimaryGroupGid {
Get-ADUser $userName -Properties PrimaryGroup | select -ExpandProperty PrimaryGroup |
Get-ADGroup -Properties gidNumber | select -ExpandProperty gidNumber
}
Function nextUid {
#gets the next avalible uidNumber to assign to the object
$uidNum = Get-ADObject -filter { uidNumber -like "*" } -Properties uidNumber |
select -ExpandProperty uidNumber | sort uidNumber
$next = $uidNum | Measure -Maximum | select -ExpandProperty maximum
#skips UID 110000
if ( $next -eq 110000 -or $next -eq $null ) {
$next = 110001
$next
}
else {
$next++
$next
}
}
Function checkUser {
$uid = Get-ADUser -Identity $userName -Properties * | select -ExpandProperty uidNumber
$primaryGid = checkPrimaryGroupGid
if ( $uid -ne $null ){
Write-Output "$userName already has UID #$uid"
}
elseif ( $primaryGid -eq $null ) {
Write-Output "Primary group does not have GID, please check to ensure that GIDs are being set."
}
else {
$uidNumber = nextUid
Set-ADUser -Identity $userName -replace @{loginShell = "/bin/bash"}
Set-ADUser -Identity $userName -replace @{homeDirectory = "/home/AMDX/$userName"}
Set-ADUser -Identity $userName -replace @{gidNumber = "$primaryGid"}
Set-ADUser -Identity $userName -replace @{uidNumber = "$uidNumber"}
Write-Output "Unix attributes added to $userName. uidNumber is $uidNumber"
}
}
$checkAccount = checkAccount
if ( $checkAccount -ne $null ) {
checkUser
}
else {
Write-Output "User not found in Active Directory"
}