Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 75d424a

Browse files
authoredJan 22, 2025
migrate module (ansible-collections#747)
1 parent 6b89e3a commit 75d424a

File tree

4 files changed

+180
-0
lines changed

4 files changed

+180
-0
lines changed
 
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
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()
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
#!/usr/bin/python
2+
# -*- coding: utf-8 -*-
3+
4+
# Copyright: (c) 2017, Dag Wieers (@dagwieers) <dag@wieers.com>
5+
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
6+
7+
DOCUMENTATION = r'''
8+
---
9+
module: win_product_facts
10+
short_description: Provides Windows product and license information
11+
description:
12+
- Provides Windows product and license information.
13+
version_added: 2.7.0
14+
author:
15+
- Dag Wieers (@dagwieers)
16+
'''
17+
18+
EXAMPLES = r'''
19+
- name: Get product id and product key
20+
ansible.windows.win_product_facts:
21+
22+
- name: Display Windows edition
23+
debug:
24+
var: ansible_os_license_edition
25+
26+
- name: Display Windows license status
27+
debug:
28+
var: ansible_os_license_status
29+
'''
30+
31+
RETURN = r'''
32+
ansible_facts:
33+
description: Dictionary containing all the detailed information about the Windows product and license.
34+
returned: always
35+
type: complex
36+
contains:
37+
ansible_os_license_channel:
38+
description: The Windows license channel.
39+
returned: always
40+
type: str
41+
sample: Volume:MAK
42+
ansible_os_license_edition:
43+
description: The Windows license edition.
44+
returned: always
45+
type: str
46+
sample: Windows(R) ServerStandard edition
47+
ansible_os_license_status:
48+
description: The Windows license status.
49+
returned: always
50+
type: str
51+
sample: Licensed
52+
ansible_os_product_id:
53+
description: The Windows product ID.
54+
returned: always
55+
type: str
56+
sample: 00326-10000-00000-AA698
57+
ansible_os_product_key:
58+
description: The Windows product key.
59+
returned: always
60+
type: str
61+
sample: T49TD-6VFBW-VV7HY-B2PXY-MY47H
62+
'''
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
shippable/windows/group1
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# This file is part of Ansible
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+
- win_product_facts:
7+
8+
- assert:
9+
that:
10+
- ansible_os_product_id is defined
11+
- ansible_os_product_key is defined

0 commit comments

Comments
 (0)
Please sign in to comment.