|
| 1 | +#Requires -Version 5.1 |
| 2 | +#Requires -RunAsAdministrator |
| 3 | + |
| 4 | +<# |
| 5 | +.SYNOPSIS |
| 6 | + Increases the WinRM maximum SOAP envelope size. |
| 7 | +
|
| 8 | +.DESCRIPTION |
| 9 | + Resolves "The estimated response packet size exceeded the maximum allowed |
| 10 | + envelope size" by changing MaxEnvelopeSizekb, restarting WinRM, and |
| 11 | + confirming the applied value. |
| 12 | +
|
| 13 | +.EXAMPLE |
| 14 | + .\Set-WinRMMaxEnvelopeSize.ps1 |
| 15 | +
|
| 16 | +.EXAMPLE |
| 17 | + .\Set-WinRMMaxEnvelopeSize.ps1 -MaxEnvelopeSizeKB 16384 |
| 18 | +#> |
| 19 | + |
| 20 | +[CmdletBinding()] |
| 21 | +param ( |
| 22 | + [Parameter()] |
| 23 | + [ValidateRange(512, 1048576)] |
| 24 | + [int]$MaxEnvelopeSizeKB = 8192 |
| 25 | +) |
| 26 | + |
| 27 | +Set-StrictMode -Version Latest |
| 28 | +$ErrorActionPreference = 'Stop' |
| 29 | + |
| 30 | +# ----------------------------------------------------------------------------- |
| 31 | +# Display helpers |
| 32 | +# ----------------------------------------------------------------------------- |
| 33 | +function Write-Section { |
| 34 | + param ([Parameter(Mandatory)][string]$Title) |
| 35 | + |
| 36 | + Write-Host '' |
| 37 | + Write-Host $Title -ForegroundColor Cyan |
| 38 | + Write-Host ('=' * 79) -ForegroundColor DarkGray |
| 39 | +} |
| 40 | + |
| 41 | +function Write-Info { |
| 42 | + param ([Parameter(Mandatory)][string]$Message) |
| 43 | + |
| 44 | + Write-Host '[INFO] ' -ForegroundColor Cyan -NoNewline |
| 45 | + Write-Host $Message |
| 46 | +} |
| 47 | + |
| 48 | +function Write-Ok { |
| 49 | + param ([Parameter(Mandatory)][string]$Message) |
| 50 | + |
| 51 | + Write-Host '[ OK ] ' -ForegroundColor Green -NoNewline |
| 52 | + Write-Host $Message |
| 53 | +} |
| 54 | + |
| 55 | +function Write-Fail { |
| 56 | + param ([Parameter(Mandatory)][string]$Message) |
| 57 | + |
| 58 | + Write-Host '[FAIL] ' -ForegroundColor Red -NoNewline |
| 59 | + Write-Host $Message |
| 60 | +} |
| 61 | + |
| 62 | +function Format-Size { |
| 63 | + param ([Parameter(Mandatory)][long]$SizeKB) |
| 64 | + |
| 65 | + $sizeMB = $SizeKB / 1024 |
| 66 | + return ('{0:N0} KB ({1:N2} MB / {2:N0} bytes)' -f $SizeKB, $sizeMB, ($SizeKB * 1KB)) |
| 67 | +} |
| 68 | + |
| 69 | +# ----------------------------------------------------------------------------- |
| 70 | +# WinRM configuration helpers |
| 71 | +# ----------------------------------------------------------------------------- |
| 72 | +function Get-MaxEnvelopeSize { |
| 73 | + $item = Get-Item -Path 'WSMan:\localhost\MaxEnvelopeSizekb' |
| 74 | + return [int]$item.Value |
| 75 | +} |
| 76 | + |
| 77 | +function Set-MaxEnvelopeSize { |
| 78 | + param ([Parameter(Mandatory)][int]$SizeKB) |
| 79 | + |
| 80 | + Set-Item -Path 'WSMan:\localhost\MaxEnvelopeSizekb' -Value $SizeKB -Force |
| 81 | +} |
| 82 | + |
| 83 | +function Restart-WinRMService { |
| 84 | + Write-Info 'Restarting the Windows Remote Management service...' |
| 85 | + Restart-Service -Name 'WinRM' -Force |
| 86 | + |
| 87 | + $service = Get-Service -Name 'WinRM' |
| 88 | + $service.WaitForStatus([System.ServiceProcess.ServiceControllerStatus]::Running, |
| 89 | + [TimeSpan]::FromSeconds(30)) |
| 90 | + |
| 91 | + if ($service.Status -ne 'Running') { |
| 92 | + throw "The WinRM service did not return to the Running state." |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +# ----------------------------------------------------------------------------- |
| 97 | +# Main |
| 98 | +# ----------------------------------------------------------------------------- |
| 99 | +try { |
| 100 | + Write-Section 'WinRM maximum envelope size configuration' |
| 101 | + |
| 102 | + $service = Get-Service -Name 'WinRM' -ErrorAction Stop |
| 103 | + Write-Ok "WinRM service found. Current status: $($service.Status)." |
| 104 | + |
| 105 | + $currentSizeKB = Get-MaxEnvelopeSize |
| 106 | + Write-Info "Current size : $(Format-Size -SizeKB $currentSizeKB)" |
| 107 | + Write-Info "Requested size: $(Format-Size -SizeKB $MaxEnvelopeSizeKB)" |
| 108 | + |
| 109 | + if ($currentSizeKB -eq $MaxEnvelopeSizeKB) { |
| 110 | + Write-Ok 'The requested value is already configured; no change is required.' |
| 111 | + |
| 112 | + if ($service.Status -ne 'Running') { |
| 113 | + Write-Info 'WinRM is not running. Starting the service...' |
| 114 | + Start-Service -Name 'WinRM' |
| 115 | + } |
| 116 | + } |
| 117 | + else { |
| 118 | + Write-Info 'Applying the new MaxEnvelopeSizekb value...' |
| 119 | + Set-MaxEnvelopeSize -SizeKB $MaxEnvelopeSizeKB |
| 120 | + Write-Ok 'The WinRM configuration has been updated.' |
| 121 | + |
| 122 | + Restart-WinRMService |
| 123 | + Write-Ok 'The WinRM service restarted successfully.' |
| 124 | + } |
| 125 | + |
| 126 | + $appliedSizeKB = Get-MaxEnvelopeSize |
| 127 | + |
| 128 | + Write-Section 'Verification' |
| 129 | + Write-Ok "Applied size: $(Format-Size -SizeKB $appliedSizeKB)" |
| 130 | + Write-Ok "WinRM status: $((Get-Service -Name 'WinRM').Status)" |
| 131 | + |
| 132 | + if ($appliedSizeKB -ne $MaxEnvelopeSizeKB) { |
| 133 | + throw "Verification failed. Requested $MaxEnvelopeSizeKB KB, but WinRM reports $appliedSizeKB KB." |
| 134 | + } |
| 135 | + |
| 136 | + Write-Host '' |
| 137 | + Write-Host 'Configuration completed successfully.' -ForegroundColor Green |
| 138 | + exit 0 |
| 139 | +} |
| 140 | +catch { |
| 141 | + Write-Host '' |
| 142 | + Write-Fail $_.Exception.Message |
| 143 | + Write-Host 'Run this script from an elevated Windows PowerShell console.' -ForegroundColor Yellow |
| 144 | + exit 1 |
| 145 | +} |
0 commit comments