|
| 1 | +#!powershell |
| 2 | + |
| 3 | +# Copyright: (c) 2017, Dag Wieers (@dagwieers) <dag@wieers.com> |
| 4 | +# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 5 | + |
| 6 | +#AnsibleRequires -CSharpUtil Ansible.Basic |
| 7 | + |
| 8 | +# This modules does not accept any options |
| 9 | +$spec = @{ |
| 10 | + supports_check_mode = $true |
| 11 | +} |
| 12 | + |
| 13 | +$module = [Ansible.Basic.AnsibleModule]::Create($args, $spec) |
| 14 | + |
| 15 | +# First try to find the product key from ACPI |
| 16 | +try { |
| 17 | + $product_key = (Get-CimInstance -Class SoftwareLicensingService).OA3xOriginalProductKey |
| 18 | +} |
| 19 | +catch { |
| 20 | + $product_key = $null |
| 21 | +} |
| 22 | + |
| 23 | +if (-not $product_key) { |
| 24 | + # Else try to get it from the registry instead |
| 25 | + try { |
| 26 | + $data = Get-ItemPropertyValue -LiteralPath "HKLM:\Software\Microsoft\Windows NT\CurrentVersion" -Name DigitalProductId |
| 27 | + } |
| 28 | + catch { |
| 29 | + $data = $null |
| 30 | + } |
| 31 | + |
| 32 | + # And for Windows 2008 R2 |
| 33 | + if (-not $data) { |
| 34 | + try { |
| 35 | + $data = Get-ItemPropertyValue -LiteralPath "HKLM:\Software\Microsoft\Windows NT\CurrentVersion" -Name DigitalProductId4 |
| 36 | + } |
| 37 | + catch { |
| 38 | + $data = $null |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + if ($data) { |
| 43 | + $product_key = $null |
| 44 | + $isWin8 = [int]($data[66] / 6) -band 1 |
| 45 | + $HF7 = 0xF7 |
| 46 | + $data[66] = ($data[66] -band $HF7) -bOr (($isWin8 -band 2) * 4) |
| 47 | + $hexdata = $data[52..66] |
| 48 | + $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" |
| 49 | + |
| 50 | + # Decode base24 binary data |
| 51 | + for ($i = 24; $i -ge 0; $i--) { |
| 52 | + $k = 0 |
| 53 | + for ($j = 14; $j -ge 0; $j--) { |
| 54 | + $k = $k * 256 -bxor $hexdata[$j] |
| 55 | + $hexdata[$j] = [math]::truncate($k / 24) |
| 56 | + $k = $k % 24 |
| 57 | + } |
| 58 | + $product_key_output = $chardata[$k] + $product_key_output |
| 59 | + $last = $k |
| 60 | + } |
| 61 | + |
| 62 | + $product_key_tmp1 = $product_key_output.SubString(1, $last) |
| 63 | + $product_key_tmp2 = $product_key_output.SubString(1, $product_key_output.Length - 1) |
| 64 | + if ($last -eq 0) { |
| 65 | + $product_key_output = "N" + $product_key_tmp2 |
| 66 | + } |
| 67 | + else { |
| 68 | + $product_key_output = $product_key_tmp2.Insert($product_key_tmp2.IndexOf($product_key_tmp1) + $product_key_tmp1.Length, "N") |
| 69 | + } |
| 70 | + $num = 0 |
| 71 | + $product_key_split = @() |
| 72 | + for ($i = 0; $i -le 4; $i++) { |
| 73 | + $product_key_split += $product_key_output.SubString($num, 5) |
| 74 | + $num += 5 |
| 75 | + } |
| 76 | + $product_key = $product_key_split -join "-" |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +# Retrieve license information |
| 81 | +$license_info = Get-CimInstance SoftwareLicensingProduct | Where-Object { |
| 82 | + $product_key -match $_.PartialProductKey -and $_.Name -match "Windows" |
| 83 | +} |
| 84 | +$winlicense_status = switch ($license_info.LicenseStatus) { |
| 85 | + 0 { "Unlicensed" } |
| 86 | + 1 { "Licensed" } |
| 87 | + 2 { "OOBGrace" } |
| 88 | + 3 { "OOTGrace" } |
| 89 | + 4 { "NonGenuineGrace" } |
| 90 | + 5 { "Notification" } |
| 91 | + 6 { "ExtendedGrace" } |
| 92 | + default { $null } |
| 93 | +} |
| 94 | + |
| 95 | +$winlicense_edition = $license_info.Name |
| 96 | +$winlicense_channel = $license_info.ProductKeyChannel |
| 97 | + |
| 98 | +$module.Result.ansible_facts = @{ |
| 99 | + ansible_os_product_id = (Get-CimInstance Win32_OperatingSystem).SerialNumber |
| 100 | + ansible_os_product_key = $product_key |
| 101 | + ansible_os_license_edition = $winlicense_edition |
| 102 | + ansible_os_license_channel = $winlicense_channel |
| 103 | + ansible_os_license_status = $winlicense_status |
| 104 | +} |
| 105 | + |
| 106 | +$module.ExitJson() |
0 commit comments