From cf1f7b53dfb88f996593ec37fe455c0a2e272758 Mon Sep 17 00:00:00 2001 From: Dag Wieers Date: Mon, 22 Jan 2018 22:00:45 +0100 Subject: [PATCH] win_setup: Add Product ID and Product Key in facts (#34097) * win_setup: Add Product ID and Product Key in facts So this is actually a very nice way to get product key information from systems collected centrally. Especially with systems that have been upgraded from Windows 7 or Windows 8 to Windows 10 may not have a valid Windows 10 product license key printed anywhere, it was a digital license. If you ever have to reinstall the system, you may recover the system from the recovery partition, or the original media, but cannot upgrade to Windows 10 for free. By collecting the product key, one can always reinstall your free Windows upgrade. My only question is, do we want this to be part of the default facts, as it may be considered important information. Or should we make a special **win_product_key_facts** ? * Add ACPI product key support --- lib/ansible/modules/windows/setup.ps1 | 31 +++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/lib/ansible/modules/windows/setup.ps1 b/lib/ansible/modules/windows/setup.ps1 index 8be498b3b5..156c7dda5d 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)