Changes for page Windows Server 2012 R2
Last modified by Sebastian Marsching on 2022/05/29 14:04
From version 1.1
edited by Sebastian Marsching
on 2022/05/29 12:36
on 2022/05/29 12:36
Change comment:
Uploaded new attachment "export_users.ps1", version {1}
To version 2.1
edited by Sebastian Marsching
on 2022/05/29 12:36
on 2022/05/29 12:36
Change comment:
Uploaded new attachment "update_users.ps1", version {1}
Summary
-
Attachments (0 modified, 1 added, 0 removed)
Details
- update_users.ps1
-
- Author
-
... ... @@ -1,0 +1,1 @@ 1 +XWiki.SebastianMarsching - Size
-
... ... @@ -1,0 +1,1 @@ 1 +1.8 KB - Content
-
... ... @@ -1,0 +1,42 @@ 1 +Function Extract-Rid([byte[]] $sid) { 2 + $sid_length = $sid.Count 3 + $sid[$sid_length - 4] + 256 * $sid[$sid_length - 3] + 65536 * $sid[$sid_length - 2] + 16777216 * [uint32]$sid[$sid_length - 1] 4 +} 5 + 6 +Function Is-SystemUserSid([byte[]] $sid) { 7 + if (($sid.Count -eq 28) -and ($sid[0] -eq 1) -and ($sid[1] -eq 5) -and ($sid[7] -eq 5) -and ($sid[8] -eq 21)) { 8 + $rid = Extract-Rid($sid) 9 + ($rid -lt 1000) 10 + } else { 11 + $FALSE 12 + } 13 +} 14 + 15 +Function Is-SystemGroupSid([byte[]] $sid) { 16 + ($sid.Count -eq 16) -and ($sid[0] -eq 1) -and ($sid[1] -eq 2) -and ($sid[7] -eq 5) -and ($sid[8] -eq 32) 17 +} 18 + 19 +$users_and_groups = Import-Csv -Path "users_and_groups.txt" -Delimiter "`t" -Encoding UTF8 20 +$users = $users_and_groups | Where-Object { $_.Type -eq "user" } | foreach { $result = @{} } { $result[$_.RID.ToString()] = $_ } { $result } 21 +$groups = $users_and_groups | Where-Object { $_.Type -eq "group" } | foreach { $result = @{} } { $result[$_.RID.ToString()] = $_ } { $result } 22 +$computer = [ADSI] "WinNT://." 23 +$computer.Children.SchemaFilter.Clear() 24 +$computer.Children.SchemaFilter.AddRange(@("user", "group")) 25 +$computer.Children | foreach { 26 + $sid = $_.objectSid[0]; 27 + $rid = (Extract-Rid($sid)).toString() 28 + if (Is-SystemUserSid($sid) -and $users.Contains($rid)) { 29 + $user = $users[$rid]; 30 + Write-Host ("Updating user `"" + $_.Name.Value + "`" with new name `"" + $user.Name + "`" and description.") 31 + $_.Rename($user.Name) 32 + $_.Description = $user.Description 33 + $_.CommitChanges() 34 + } elseif (Is-SystemGroupSid($sid) -and $groups.Contains($rid)) { 35 + $group = $groups[$rid]; 36 + Write-Host ("Updating group `"" + $_.Name.Value + "`" with new name `"" + $group.Name + "`" and description.") 37 + $_.Rename($group.Name) 38 + $_.Description = $group.Description 39 + $_.CommitChanges() 40 + } 41 + } 42 +