Skip to content

Commit b3a82ac

Browse files
authored
Merge pull request #63 from bowenzhang85/master
WindowsActivationValidation V2 Update
2 parents dcd3ef1 + 5fdcf19 commit b3a82ac

File tree

2 files changed

+82
-12
lines changed

2 files changed

+82
-12
lines changed

Windows_scripts/WindowsActivationValidation/README.md

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,28 @@
22

33
This PowerShell script is designed to assist with diagnosing Windows licensing and connectivity issues on Azure virtual machines (VMs). It performs a series of checks to verify:
44

5-
- Whether the system is running an Azure-specific Windows edition
6-
- Connectivity to the Azure Instance Metadata Service (IMDS)
7-
- Presence and validity of attested certificates
8-
- The configured Key Management Service (KMS) endpoint
9-
- TCP connectivity to the KMS server
10-
- Windows activation status
5+
6+
## 🔧 What It Does
7+
8+
1. **WinRM Service Check**
9+
- Ensures the `WinRM` service is running.
10+
- Attempts to start the service if it's not already running.
11+
12+
2. **Azure Edition Detection**
13+
- Detects if the machine is using an **Azure edition** of Windows.
14+
- Verifies connectivity to the **Azure Instance Metadata Service (IMDS)**.
15+
- Checks for any missing root certificates in the attestation document.
16+
17+
3. **KMS Endpoint Validation**
18+
- Retrieves the configured KMS endpoint from the Windows registry.
19+
- Falls back to the default Azure KMS endpoint if none is found.
20+
- Tests TCP connectivity to the KMS endpoint on port `1688`.
21+
22+
4. **Windows Activation Check & Attempt**
23+
- Checks current activation status.
24+
- If not activated, attempts to activate using `slmgr.vbs`.
25+
- Parses and displays activation error codes with direct links to Microsoft troubleshooting documentation.
26+
1127

1228
# Prerequisites
1329

Windows_scripts/WindowsActivationValidation/WindowsActivationValidation.ps1

Lines changed: 60 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,15 @@
11
try {
2+
# Ensure WinRM service is running
3+
$winrmService = Get-Service -Name WinRM -ErrorAction SilentlyContinue
4+
if ($winrmService -and $winrmService.Status -ne 'Running') {
5+
Write-Host "WinRM service is not running. Attempting to start WinRM..." -ForegroundColor Yellow
6+
try {
7+
Start-Service -Name WinRM
8+
Write-Host "WinRM service started successfully." -ForegroundColor Green
9+
} catch {
10+
Write-Host "Failed to start WinRM service: $($_.Exception.Message)" -ForegroundColor Red
11+
}
12+
}
213
# Check if the VM is using an Azure edition image
314
$WindowsEdition = (Get-ComputerInfo).OsName
415
$isAzureEdition = $WindowsEdition -match "Azure"
@@ -41,18 +52,61 @@ try {
4152
}
4253

4354
# Check Windows Activation Status
44-
$licenseStatus = (Get-CimInstance SoftwareLicensingProduct -ComputerName $env:computername | Where-Object { $_.Name -like "*Windows*" }).LicenseStatus
55+
$licenseStatus = (Get-CimInstance SoftwareLicensingProduct -ComputerName $env:computername | Where-Object { $_.Name -like "*Windows*" -and $_.PartialProductKey })[0].LicenseStatus
4556

46-
if ($licenseStatus -eq 0) {
57+
if ($licenseStatus -eq 1) {
4758
Write-Host "Windows is Activated." -ForegroundColor Green
48-
} elseif ($licenseStatus -eq 1) {
49-
Write-Host "Windows is Not Activated." -ForegroundColor Red
5059
} else {
51-
Write-Host "Unable to determine Windows activation status." -ForegroundColor Yellow
60+
Write-Host "Windows is not activated or activation status is undetermined. Attempting activation..." -ForegroundColor Yellow
61+
$activationResult = cscript.exe C:\Windows\System32\slmgr.vbs /ato 2>&1
62+
if ($activationResult -match "Product activated successfully") {
63+
Write-Host "Product activated successfully." -ForegroundColor Green
64+
} elseif ($activationResult -match "Error:") {
65+
$errorLine = $activationResult | Select-String -Pattern "Error:" | Select-Object -First 1
66+
$errorCode = $errorLine -replace '.*Error:\s*', ''
67+
Write-Host ""
68+
Write-Host "Activation failed. Error code: $errorCode" -ForegroundColor Red
69+
Write-Host ""
70+
$errorCodeTrimmed = $errorCode.Trim()
71+
$knownError = $false
72+
if ($errorCodeTrimmed -like '*0xC004F074*') {
73+
Write-Host "Troubleshoot Link: https://learn.microsoft.com/en-us/troubleshoot/azure/virtual-machines/windows/windows-vm-activation-error-0xc004f074" -ForegroundColor Yellow
74+
$knownError = $true
75+
}
76+
if ($errorCodeTrimmed -like '*0xC004FD01*') {
77+
Write-Host "Troubleshoot Link: https://learn.microsoft.com/en-us/troubleshoot/azure/virtual-machines/windows/windows-vm-activation-error-0xc004fd01-0xc004fd02" -ForegroundColor Yellow
78+
$knownError = $true
79+
}
80+
if ($errorCodeTrimmed -like '*0xC004FD02*') {
81+
Write-Host "Troubleshoot Link: https://learn.microsoft.com/en-us/troubleshoot/azure/virtual-machines/windows/windows-vm-activation-error-0xc004fd01-0xc004fd02" -ForegroundColor Yellow
82+
$knownError = $true
83+
}
84+
if ($errorCodeTrimmed -like '*0xC004F06C*') {
85+
Write-Host "Troubleshoot Link: https://learn.microsoft.com/en-us/troubleshoot/windows-server/licensing-and-activation/error-0xc004f06c-activate-windows" -ForegroundColor Yellow
86+
$knownError = $true
87+
}
88+
if ($errorCodeTrimmed -like '*0xC004E015*') {
89+
Write-Host "Troubleshoot Link: https://learn.microsoft.com/en-us/troubleshoot/windows-server/installing-updates-features-roles/error-0xc004e015-sl-e-eul-consumption-failed-activate-windows" -ForegroundColor Yellow
90+
$knownError = $true
91+
}
92+
if ($errorCodeTrimmed -like '*0x800705B4*') {
93+
Write-Host "Troubleshoot Link: https://learn.microsoft.com/en-us/troubleshoot/azure/virtual-machines/windows/windows-vm-activation-error-0x800705b4" -ForegroundColor Yellow
94+
$knownError = $true
95+
}
96+
if ($errorCodeTrimmed -like '*0x80070005*') {
97+
Write-Host "Troubleshoot Link: https://learn.microsoft.com/en-us/troubleshoot/windows-server/installing-updates-features-roles/error-0x80070005-access-denied" -ForegroundColor Yellow
98+
$knownError = $true
99+
}
100+
if (-not $knownError) {
101+
Write-Host "Troubleshoot Link: https://learn.microsoft.com/en-us/troubleshoot/azure/virtual-machines/windows/troubleshoot-activation-problems" -ForegroundColor Yellow
102+
}
103+
} else {
104+
Write-Host "Activation command executed. Please verify activation status again." -ForegroundColor Yellow
105+
}
52106
}
53107

54108
} catch {
55109
Write-Host "Unable to connect to the metadata server: $($_.Exception.Message)" -ForegroundColor Red
56110
Write-Host "Please refer to the following link for details about IMDS endpoint connection:" -ForegroundColor Yellow
57111
Write-Host "https://learn.microsoft.com/azure/virtual-machines/instance-metadata-service" -ForegroundColor Yellow
58-
}
112+
}

0 commit comments

Comments
 (0)