diff --git a/lib/ansible/modules/windows/setup.ps1 b/lib/ansible/modules/windows/setup.ps1 index 8be498b3b5c..156c7dda5da 100644 --- a/lib/ansible/modules/windows/setup.ps1 +++ b/lib/ansible/modules/windows/setup.ps1 @@ -54,6 +54,35 @@ Function Get-MachineSid { return $machine_sid } +Function Get-ProductKey { + # First try to find the product key from ACPI + $product_key = (Get-CimInstance -Class SoftwareLicensingService).OA3xOriginalProductKey + + if (-not $product_key) { + # Else try to get it from the registry instead + $data = Get-ItemPropertyValue -Path "HKLM:\Software\Microsoft\Windows NT\CurrentVersion" -Name DigitalProductId + $hexdata = $data[52..66] + + $chardata = "B","C","D","F","G","H","J","K","M","P","Q","R","T","V","W","X","Y","2","3","4","6","7","8","9" + + # Decode base24 binary data + $product_key = $null + for ($i = 24; $i -ge 0; $i--) { + $k = 0 + for ($j = 14; $j -ge 0; $j--) { + $k = $k * 256 -bxor $hexdata[$j] + $hexdata[$j] = [math]::truncate($k / 24) + $k = $k % 24 + } + $product_key = $chardata[$k] + $product_key + if (($i % 5 -eq 0) -and ($i -ne 0)) { + $product_key = "-" + $product_key + } + } + } + return $product_key +} + $result = @{ ansible_facts = @{ } changed = $false @@ -180,6 +209,8 @@ $ansible_facts = @{ ansible_nodename = ($ip_props.HostName + "." + $ip_props.DomainName) ansible_os_family = "Windows" ansible_os_name = ($win32_os.Name.Split('|')[0]).Trim() + ansible_os_product_id = $win32_os.SerialNumber + ansible_os_product_key = Get-ProductKey ansible_owner_contact = ([string] $win32_cs.PrimaryOwnerContact) ansible_owner_name = ([string] $win32_cs.PrimaryOwnerName) ansible_powershell_version = ($PSVersionTable.PSVersion.Major)