getting_domain_windows_server_s_licensing_status
Differences
This shows you the differences between two versions of the page.
| Next revision | Previous revision | ||
| getting_domain_windows_server_s_licensing_status [2024/09/02 14:36] – created oso | getting_domain_windows_server_s_licensing_status [2025/09/15 18:38] (current) – oso | ||
|---|---|---|---|
| Line 9: | Line 9: | ||
| * Exports the results to a CSV file located at **C: | * Exports the results to a CSV file located at **C: | ||
| - | < | + | < |
| # Import the Active Directory module | # Import the Active Directory module | ||
| Import-Module ActiveDirectory | Import-Module ActiveDirectory | ||
| Line 52: | Line 52: | ||
| </ | </ | ||
| + | {{: | ||
| + | |||
| + | ===== " | ||
| + | |||
| + | |||
| + | Here, the script modified so we get both the LicensingService countdown (from WMI) and the slmgr countdown (parsed from /xpr). It will export all results to CSV. | ||
| + | |||
| + | <code powershell> | ||
| + | # Import the Active Directory module | ||
| + | Import-Module ActiveDirectory | ||
| + | |||
| + | # Define a hashtable for license status descriptions | ||
| + | $licenseStatusDescriptions = @{ | ||
| + | 0 = ' | ||
| + | 1 = ' | ||
| + | 2 = ' | ||
| + | 3 = ' | ||
| + | 4 = ' | ||
| + | 5 = ' | ||
| + | 6 = ' | ||
| + | } | ||
| + | |||
| + | # Get all Windows Server machines in the domain | ||
| + | $servers = Get-ADComputer -Filter " | ||
| + | |||
| + | # Initialize an array to store the results | ||
| + | $results = @() | ||
| + | |||
| + | foreach ($server in $servers) { | ||
| + | try { | ||
| + | $licenseStatus = Get-CimInstance -ComputerName $server.Name -ClassName SoftwareLicensingProduct -ErrorAction Stop | | ||
| + | Where-Object { $_.PartialProductKey -and $_.Description -like ' | ||
| + | Select-Object Description, | ||
| + | } catch { | ||
| + | Write-Warning " | ||
| + | continue | ||
| + | } | ||
| + | |||
| + | foreach ($status in $licenseStatus) { | ||
| + | $statusDescription = $licenseStatusDescriptions[$status.LicenseStatus] | ||
| + | |||
| + | # LicensingService calculation (WMI) | ||
| + | $lsDays = $null | ||
| + | if ($status.GracePeriodRemaining) { | ||
| + | $lsDays = [math]:: | ||
| + | if ($lsDays -lt 0) { $lsDays = 0 } | ||
| + | } elseif ($status.EvaluationEndDate -and $status.EvaluationEndDate.Year -gt 1900) { | ||
| + | $lsDays = ($status.EvaluationEndDate - (Get-Date)).Days | ||
| + | if ($lsDays -lt 0) { $lsDays = 0 } | ||
| + | } | ||
| + | |||
| + | # slmgr /xpr parsing | ||
| + | $slmgrDays = $null | ||
| + | try { | ||
| + | $slmgrOutput = Invoke-Command -ComputerName $server.Name -ScriptBlock { | ||
| + | cscript.exe //Nologo " | ||
| + | } | ||
| + | |||
| + | if ($slmgrOutput -match ' | ||
| + | $expireDate = [datetime]:: | ||
| + | $slmgrDays = ($expireDate - (Get-Date)).Days | ||
| + | if ($slmgrDays -lt 0) { $slmgrDays = 0 } | ||
| + | } elseif ($slmgrOutput -match ' | ||
| + | $slmgrDays = ' | ||
| + | } | ||
| + | } catch { | ||
| + | Write-Warning " | ||
| + | } | ||
| + | |||
| + | $results += [PSCustomObject]@{ | ||
| + | ServerName | ||
| + | OperatingSystem | ||
| + | Description | ||
| + | LicenseStatus | ||
| + | LicensingServiceRemainingDays = $lsDays | ||
| + | SlmgrRemainingDays | ||
| + | } | ||
| + | } | ||
| + | } | ||
| + | |||
| + | # Export the results to a CSV file | ||
| + | $results | Export-Csv -Path " | ||
| + | </ | ||
getting_domain_windows_server_s_licensing_status.1725287816.txt.gz · Last modified: 2024/10/17 21:42 (external edit)
