Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

windows facts: better way to get machine SID #29821

Merged
merged 2 commits into from
Sep 12, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 19 additions & 1 deletion lib/ansible/modules/windows/setup.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,24 @@ Function Get-CustomFacts {
}
}

Function Get-MachineSid {
# The Machine SID is stored in HKLM:\SECURITY\SAM\Domains\Account and is
# 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.

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" }

$machine_sid = $null
if ($users -ne $null) {
$machine_sid = $users.Sid.AccountDomainSid.Value
}
return $machine_sid
}

$result = @{
ansible_facts = @{ }
changed = $false
Expand Down Expand Up @@ -147,7 +165,7 @@ $ansible_facts = @{
ansible_ip_addresses = $ips
ansible_kernel = $osversion.Version.ToString()
ansible_lastboot = $win32_os.lastbootuptime.ToString("u")
ansible_machine_id = $user.User.AccountDomainSid.Value
ansible_machine_id = Get-MachineSid
ansible_nodename = ($ip_props.HostName + "." + $ip_props.DomainName)
ansible_os_family = "Windows"
ansible_os_name = ($win32_os.Name.Split('|')[0]).Trim()
Expand Down