summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJordan Borean <jborean93@gmail.com>2018-04-12 20:01:16 +1000
committeransibot <ansibot@users.noreply.github.com>2018-04-12 06:01:16 -0400
commit2fc3ac351666e50bb4e9144fb2be3b280d498c5b (patch)
treef8d746ff74608d3cc8e57a1ab470714e4d4700ab
parentb9d97d85f6de742226df74c783986f2a9fa8a252 (diff)
downloadansible-2fc3ac351666e50bb4e9144fb2be3b280d498c5b.tar.gz
win_setup: fix for machine sid to work in domains with lots of users (#38646)
-rw-r--r--lib/ansible/modules/windows/setup.ps118
1 files changed, 13 insertions, 5 deletions
diff --git a/lib/ansible/modules/windows/setup.ps1 b/lib/ansible/modules/windows/setup.ps1
index fe67ce289f..6fc9f273ed 100644
--- a/lib/ansible/modules/windows/setup.ps1
+++ b/lib/ansible/modules/windows/setup.ps1
@@ -29,16 +29,24 @@ Function Get-MachineSid {
# only accessible by the Local System account. This method get's the local
# admin account (ends with -500) and lops it off to get the machine sid.
+ $admins_sid = "S-1-5-32-544"
+ $admin_group = ([Security.Principal.SecurityIdentifier]$admins_sid).Translate([Security.Principal.NTAccount]).Value
+
Add-Type -AssemblyName System.DirectoryServices.AccountManagement
$principal_context = New-Object -TypeName System.DirectoryServices.AccountManagement.PrincipalContext([System.DirectoryServices.AccountManagement.ContextType]::Machine)
- $user_principal = New-Object -TypeName System.DirectoryServices.AccountManagement.UserPrincipal($principal_context)
- $searcher = New-Object -TypeName System.DirectoryServices.AccountManagement.PrincipalSearcher($user_principal)
- $users = $searcher.FindAll() | Where-Object { $_.Sid -like "*-500" }
+ $group_principal = New-Object -TypeName System.DirectoryServices.AccountManagement.GroupPrincipal($principal_context, $admin_group)
+ $searcher = New-Object -TypeName System.DirectoryServices.AccountManagement.PrincipalSearcher($group_principal)
+ $groups = $searcher.FindOne()
$machine_sid = $null
- if ($users -ne $null) {
- $machine_sid = $users.Sid.AccountDomainSid.Value
+ foreach ($user in $groups.Members) {
+ $user_sid = $user.Sid
+ if ($user_sid.Value.EndsWith("-500")) {
+ $machine_sid = $user_sid.AccountDomainSid.Value
+ break
+ }
}
+
return $machine_sid
}