Skip to content

updated LKGC script v1.3 - #127

Open
Tony Mocanu (anmocanu) wants to merge 7 commits into
Azure:mainfrom
anmocanu:patch-10
Open

updated LKGC script v1.3#127
Tony Mocanu (anmocanu) wants to merge 7 commits into
Azure:mainfrom
anmocanu:patch-10

Conversation

@anmocanu

Copy link
Copy Markdown
Contributor

This script enables Last Known Good Configuration (LKGC) by incrementing registry values for recovery from boot issues. It includes enhancements for logging and error handling, ensuring proper execution and tracking of applied changes. This update enhances the win-LKGC.ps1 script to increment Last Known Good Configuration (LKGC) registry values, log changes, and ensure proper handling of nested VMs. It includes improved error handling, logging, and a new mechanism to track whether LKGC was applied on any disk. .VERSION
v1.3: [May 2026] - Updated the script (current)
- Added LKGC_APPLIED log flag (per disk + overall) and corrected final summary message.
- Fixed Get-VM crash when Hyper-V module is not installed on host (guarded Get-VM).
- Fixed false "already set" detection by requiring ALL thresholds (AND instead of OR).
v1.1: Previous version
v0.1: Initial commit

This script enables Last Known Good Configuration (LKGC) by incrementing registry values for recovery from boot issues. It includes enhancements for logging and error handling, ensuring proper execution and tracking of applied changes. 
This update enhances the win-LKGC.ps1 script to increment Last Known Good Configuration (LKGC) registry values, log changes, and ensure proper handling of nested VMs. It includes improved error handling, logging, and a new mechanism to track whether LKGC was applied on any disk.
.VERSION
   v1.3: [May 2026] - Updated the script (current)
                       - Added LKGC_APPLIED log flag (per disk + overall) and corrected final summary message.
                       - Fixed Get-VM crash when Hyper-V module is not installed on host (guarded Get-VM).
                       - Fixed false "already set" detection by requiring ALL thresholds (AND instead of OR).
    v1.1: Previous version
    v0.1: Initial commit
@EdwinBernal1
EdwinBernal1 self-requested a review June 16, 2026 13:58
@EdwinBernal1

EdwinBernal1 commented Jun 16, 2026

Copy link
Copy Markdown
Member

Issues Found

🟢 Minor: OS version parsing is heuristic-based

The script derives $winosver from first numeric token in ProductName. This is pragmatic but brittle if naming format changes.

Recommendation: consider reading a stable version key (e.g., CurrentMajorVersionNumber / CurrentBuild) when available.

This update includes a production hardening update with several enhancements such as path validation, logging improvements, and backup functionality. It also introduces summary counters for processed, skipped, failed, and changed operations.
@EdwinBernal1

Copy link
Copy Markdown
Member

Tony Mocanu (@anmocanu) could you help us with the review of these findings

Findings

Critical

File Context Issue Recommendation
None

Warning

File Context Issue Recommendation
win-LKGC.ps1 version number PR title says v1.3, but the top .VERSION entry in the file is v1.4 ("Production hardening"). Reconcile the version in the title/history so it is unambiguous.
win-LKGC.ps1 Log-* wrapper Logging-wrapper pattern duplicated across the batch. Extract shared helper.

Info

File Context Suggestion
win-LKGC.ps1 backup + rollback SYSTEM hive backup with restore-on-failure is the strongest safety pattern in the batch. Good.
win-LKGC.ps1 AND-logic fix The OR→AND change is a genuine bug fix; verify the version-specific thresholds (Win10/2016+ vs 2012) match intended LKGC semantics.
win-LKGC.ps1 unload retry Pre-load defensive reg unload, plus 3× unload retry with GC — reduces stuck-hive risk on detach.

Operational Risk Assessment

Factor Rating Notes
Scope Low-Medium Single script; registry hive load/modify/unload.
Destructive ops Low-Medium Registry Select edits — but backed up and rolled back on failure.
Rollback possible Yes Per-disk SYSTEM hive backup + automatic restore on failure.
Testing documented Unknown Confirm test evidence in the PR.
Gen compatibility Gen1+Gen2 Operates on the lettered Windows volume; generation-agnostic.

Refactored logging mechanism to use Log-* functions for better consistency and clarity. Updated comments and improved error handling for registry operations.
@glimoli

Copy link
Copy Markdown
Contributor

VMRepair Script Update: win-LKGC.ps1

🎯 Summary

Comprehensive testing and validation of win-LKGC.ps1 (v1.3) with fault injection verification across 6 VM configurations.

✅ Test Results

Overall Score: 78.4/100 (Grade: B)

Metric Score Status
Test Success Rate 6/6 passed (100%)
Code Quality 96/100
Header Validation 37/100
Telemetry Coverage 0%

🔬 Testing Performed

Strategy: Representative (6 configurations)
Fault Injection: ✅ Enabled — SYSTEM\Select values set below LKGC threshold, ControlSet002 baseline created
Region: westus2
Test Date: 2026-07-30

Test Configurations

  1. Modern Standard (Gen2) — Win2022Datacenter, Standard_D2s_v3, Premium_LRS
  2. Legacy Support (Gen1) — Win2016Datacenter, Standard_D2as_v4, StandardSSD_LRS
  3. Encrypted Modern — Win2019Datacenter, Standard_D2s_v3, Premium_LRS + ADE
  4. Cost-Optimized — Win2022Datacenter, Standard_D2s_v3, Standard_LRS
  5. Latest Gen2 + Premium — Win2022Datacenter, Standard_D2s_v3, Premium_LRS
  6. Mid-Tier 2016 — Win2016Datacenter, Standard_D2s_v3, StandardSSD_LRS

Fault Injection Validation:

  • Breaker Script: break-win-LKGC-v2.ps1
  • Pre-repair state: ✅ Corruption verified (SYSTEM\Select below threshold)
  • Post-repair state: ✅ Repair verified (Select values incremented above threshold)
  • All 6 configurations passed fault injection testing

📊 Detailed Test Artifacts

Complete test artifacts available in SME work item:

  • Interactive HTML Report: Full test execution details with fault injection validation
  • Improvement Recommendations: Actionable enhancement suggestions
  • Test Data Package: Complete logs, breaker scripts, validation outputs
  • Reproducibility Guide: JSON manifest for test replication

🔗 SME Work Item: Tooling 59898 win-LKGC

🚀 Improvements Recommended

1. ❌ Header Documentation (37/100) — BLOCKING

Current Issues:

  • Missing required field: .DESCRIPTION
  • Missing required field: .NOTES

Recommendation: Add both fields to the PowerShell comment block. .DESCRIPTION should explain what LKGC restoration does and when this script should be used. .NOTES should include requirements, side-effects, and version history. These are required by the VMRepair script standard and needed for automated documentation generation.

2. ❌ Telemetry Coverage (0%) — High Priority

Current Issues:

  • No Log-Start event at script entry
  • No Log-Success event on successful LKGC application
  • No Log-Error event in the catch block

Recommendation: Add telemetry at minimum for script start, success (with SYSTEM\Select before/after values as custom dimensions), and error. Without this, production failures on this script are invisible in Application Insights dashboards.

# Example additions:
Log-Start -Message "Starting LKGC restoration" -Properties @{
    OSVersion = $productName
    SelectBefore = "$($before.Current),$($before.Default),$($before.Failed),$($before.LastKnownGood)"
}
# ... repair logic ...
Log-Output "LKGC APPLIED" -Properties @{
    SelectAfter = "$($after.Current),$($after.Default),$($after.Failed),$($after.LastKnownGood)"
}

3. ℹ️ Minor — Trailing Whitespace (PSScriptAnalyzer)

Lines 6, 8, 10, 11 have trailing whitespace. Not blocking but clean to fix.


Generated by VMRepairMint Script Testing Framework

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants