Public/AzStackHci.CrashDumpSettings.ps1

# ///////////////////////////////////////////////////////////////////
# Set-AzStackHciUserModeCrashDumpSettings function
# Used to set the registry settings for user mode crash dumps
# ///////////////////////////////////////////////////////////////////
Function Set-AzStackHciUserModeCrashDumpSettings {
    <#
    .SYNOPSIS
 
    Sets the registry settings for user mode crash dumps
 
    .DESCRIPTION
 
    Sets the registry settings for user mode crash dumps, enabling Windows Error Reporting to create user mode dumps on the host.
 
    #>


    [CmdletBinding(SupportsShouldProcess, ConfirmImpact = 'Medium')]
    [OutputType([void])]
    param (
        [switch]$NoOutput
    )

    begin {
        # Requires administrator permissions to set user mode crash dump settings
        if (-not (Test-Elevation)) { throw "This script must be run as an Administrator." }
    }

    process {
        # Handle -NoOutput: suppress all console output
        if ($NoOutput.IsPresent) {
            $script:SilentMode = $true
            $VerbosePreference = 'SilentlyContinue'
            $DebugPreference = 'SilentlyContinue'
        }

        # Enabling Windows Error Reporting to create user mode dumps on Host
        $HKLMWERLocalDumps = "HKLM:\SOFTWARE\Microsoft\Windows\Windows Error Reporting\LocalDumps"

        # ShouldProcess gate: prompt user before modifying WER registry settings
        if (-not $PSCmdlet.ShouldProcess("WER LocalDumps registry ($HKLMWERLocalDumps)", "Set user mode crash dump registry values")) {
            return
        }

        New-Item $HKLMWERLocalDumps -ErrorAction SilentlyContinue | Out-Null
        
        # Host_Application_LocalDump_DumpType
        Set-ItemProperty -Path $HKLMWERLocalDumps -Name DumpType -Type DWord -Value 1 -ErrorAction Stop

        # Use path "C:\Windows\CrashDumps" and compress the folder
        if(-not(test-path "C:\Windows\CrashDumps")) { New-Item "C:\Windows\CrashDumps" -ItemType Directory -ErrorAction SilentlyContinue | Out-Null }
        Write-Verbose "Compressing the 'C:\Windows\CrashDumps' folder..." -Verbose
        Compact /i /c /a /s:C:\Windows\CrashDumps

        # Host_Application_LocalDump_DumpFolder
        Set-ItemProperty -Path $HKLMWERLocalDumps -Name DumpFolder -Type ExpandString -Value "C:\Windows\CrashDumps" -ErrorAction Stop

        # Host_Application_LocalDump_DumpCount, only keep one dump per process to limit disk space usage
        Set-ItemProperty -Path $HKLMWERLocalDumps -Name DumpCount -Type DWord -Value 1 -ErrorAction Stop

    } # End of process block

    end {
        if ($NoOutput.IsPresent) { $script:SilentMode = $false }
        Write-Debug "Completed Set-AzStackHciUserModeCrashDumpSettings function"
    }

} # End of Set-AzStackHciUserModeCrashDumpSettings function