|
| 1 | +$script:localizedDataSession = Get-LocalizedData -DefaultUICulture en-US -FileName 'JeaSessionConfiguration.strings.psd1' |
| 2 | + |
| 3 | +class SessionConfigurationUtility |
| 4 | +{ |
| 5 | + hidden [bool] TestParameters() |
| 6 | + { |
| 7 | + if (-not $this.SessionType) |
| 8 | + { |
| 9 | + $this.SessionType = 'RestrictedRemoteServer' |
| 10 | + } |
| 11 | + |
| 12 | + if ($this.RunAsVirtualAccountGroups -and $this.GroupManagedServiceAccount) |
| 13 | + { |
| 14 | + throw $script:localizedDataSession.ConflictRunAsVirtualAccountGroupsAndGroupManagedServiceAccount |
| 15 | + } |
| 16 | + |
| 17 | + if ($this.GroupManagedServiceAccount -and $this.RunAsVirtualAccount) |
| 18 | + { |
| 19 | + throw $script:localizedDataSession.ConflictRunAsVirtualAccountAndGroupManagedServiceAccount |
| 20 | + } |
| 21 | + |
| 22 | + if (-not $this.GroupManagedServiceAccount) |
| 23 | + { |
| 24 | + $this.RunAsVirtualAccount = $true |
| 25 | + Write-Warning -Message $script:localizedDataSession.NotDefinedGMSaAndVirtualAccount |
| 26 | + } |
| 27 | + |
| 28 | + return $true |
| 29 | + } |
| 30 | + |
| 31 | + ## Get a PS Session Configuration based on its name |
| 32 | + hidden [object] GetPSSessionConfiguration($Name) |
| 33 | + { |
| 34 | + $winRMService = Get-Service -Name 'WinRM' |
| 35 | + if ($winRMService -and $winRMService.Status -eq 'Running') |
| 36 | + { |
| 37 | + # Temporary disabling Verbose as xxx-PSSessionConfiguration methods verbose messages are useless for DSC debugging |
| 38 | + $verbosePreferenceBackup = $Global:VerbosePreference |
| 39 | + $Global:VerbosePreference = 'SilentlyContinue' |
| 40 | + $psSessionConfiguration = Get-PSSessionConfiguration -Name $Name -ErrorAction SilentlyContinue |
| 41 | + $Global:VerbosePreference = $verbosePreferenceBackup |
| 42 | + |
| 43 | + if ($psSessionConfiguration) |
| 44 | + { |
| 45 | + return $psSessionConfiguration |
| 46 | + } |
| 47 | + else |
| 48 | + { |
| 49 | + return $null |
| 50 | + } |
| 51 | + } |
| 52 | + else |
| 53 | + { |
| 54 | + Write-Verbose -Message $script:localizedDataSession.WinRMNotRunningGetPsSession |
| 55 | + return $null |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + ## Unregister a PS Session Configuration based on its name |
| 60 | + hidden [void] UnregisterPSSessionConfiguration($Name) |
| 61 | + { |
| 62 | + $winRMService = Get-Service -Name 'WinRM' |
| 63 | + if ($winRMService -and $winRMService.Status -eq 'Running') |
| 64 | + { |
| 65 | + # Temporary disabling Verbose as xxx-PSSessionConfiguration methods verbose messages are useless for DSC debugging |
| 66 | + $verbosePreferenceBackup = $Global:VerbosePreference |
| 67 | + $Global:VerbosePreference = 'SilentlyContinue' |
| 68 | + $null = Unregister-PSSessionConfiguration -Name $Name -Force -WarningAction 'SilentlyContinue' |
| 69 | + $Global:VerbosePreference = $verbosePreferenceBackup |
| 70 | + } |
| 71 | + else |
| 72 | + { |
| 73 | + throw ($script:localizedDataSession.WinRMNotRunningUnRegisterPsSession -f $Name) |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + ## Register a PS Session Configuration and handle a WinRM hanging situation |
| 78 | + hidden [Void] RegisterPSSessionConfiguration($Name, $Path, $Timeout) |
| 79 | + { |
| 80 | + $winRMService = Get-Service -Name 'WinRM' |
| 81 | + if ($winRMService -and $winRMService.Status -eq 'Running') |
| 82 | + { |
| 83 | + Write-Verbose -Message ($script:localizedDataSession.RegisterPSSessionConfiguration -f $Name,$Path,$Timeout) |
| 84 | + # Register-PSSessionConfiguration has been hanging because the WinRM service is stuck in Stopping state |
| 85 | + # therefore we need to run Register-PSSessionConfiguration within a job to allow us to handle a hanging WinRM service |
| 86 | + |
| 87 | + # Save the list of services sharing the same process as WinRM in case we have to restart them |
| 88 | + $processId = Get-CimInstance -ClassName 'Win32_Service' -Filter "Name LIKE 'WinRM'" | Select-Object -ExpandProperty ProcessId |
| 89 | + $serviceList = Get-CimInstance -ClassName 'Win32_Service' -Filter "ProcessId=$processId" | Select-Object -ExpandProperty Name |
| 90 | + foreach ($service in $serviceList.clone()) |
| 91 | + { |
| 92 | + $dependentServiceList = Get-Service -Name $service | ForEach-Object { $_.DependentServices } |
| 93 | + foreach ($dependentService in $dependentServiceList) |
| 94 | + { |
| 95 | + if ($dependentService.Status -eq 'Running' -and $serviceList -notcontains $dependentService.Name) |
| 96 | + { |
| 97 | + $serviceList += $dependentService.Name |
| 98 | + } |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + if ($Path) |
| 103 | + { |
| 104 | + $registerString = "`$null = Register-PSSessionConfiguration -Name '$Name' -Path '$Path' -NoServiceRestart -Force -ErrorAction 'Stop' -WarningAction 'SilentlyContinue'" |
| 105 | + } |
| 106 | + else |
| 107 | + { |
| 108 | + $registerString = "`$null = Register-PSSessionConfiguration -Name '$Name' -NoServiceRestart -Force -ErrorAction 'Stop' -WarningAction 'SilentlyContinue'" |
| 109 | + } |
| 110 | + |
| 111 | + $registerScriptBlock = [scriptblock]::Create($registerString) |
| 112 | + |
| 113 | + if ($Timeout -gt 0) |
| 114 | + { |
| 115 | + $job = Start-Job -ScriptBlock $registerScriptBlock |
| 116 | + Wait-Job -Job $job -Timeout $Timeout |
| 117 | + Receive-Job -Job $job |
| 118 | + Remove-Job -Job $job -Force -ErrorAction 'SilentlyContinue' |
| 119 | + |
| 120 | + # If WinRM is still Stopping after the job has completed / exceeded $Timeout, force kill the underlying WinRM process |
| 121 | + $winRMService = Get-Service -Name 'WinRM' |
| 122 | + if ($winRMService -and $winRMService.Status -eq 'StopPending') |
| 123 | + { |
| 124 | + $processId = Get-CimInstance -ClassName 'Win32_Service' -Filter "Name LIKE 'WinRM'" | Select-Object -ExpandProperty ProcessId |
| 125 | + Write-Verbose -Message ($script:localizedDataSession.ForcingProcessToStop -f $processId) |
| 126 | + $failureList = @() |
| 127 | + try |
| 128 | + { |
| 129 | + # Kill the process hosting WinRM service |
| 130 | + Stop-Process -Id $processId -Force |
| 131 | + Start-Sleep -Seconds 5 |
| 132 | + Write-Verbose -Message ($script:localizedDataSession.RegisterPSSessionConfiguration -f $($serviceList -join ', ')) |
| 133 | + # Then restart all services previously identified |
| 134 | + foreach ($service in $serviceList) |
| 135 | + { |
| 136 | + try |
| 137 | + { |
| 138 | + Start-Service -Name $service |
| 139 | + } |
| 140 | + catch |
| 141 | + { |
| 142 | + $failureList += $script:localizedDataSession.FailureListStartService -f $service |
| 143 | + } |
| 144 | + } |
| 145 | + } |
| 146 | + catch |
| 147 | + { |
| 148 | + $failureList += $script:localizedDataSession.FailureListKillWinRMProcess |
| 149 | + } |
| 150 | + |
| 151 | + if ($failureList) |
| 152 | + { |
| 153 | + Write-Verbose -Message ($script:localizedDataSession.FailureListKillWinRMProcess -f $($failureList -join ', ')) |
| 154 | + } |
| 155 | + } |
| 156 | + elseif ($winRMService -and $winRMService.Status -eq 'Stopped') |
| 157 | + { |
| 158 | + Write-Verbose -Message $script:localizedDataSession.RestartWinRM |
| 159 | + Start-Service -Name 'WinRM' |
| 160 | + } |
| 161 | + } |
| 162 | + else |
| 163 | + { |
| 164 | + Invoke-Command -ScriptBlock $registerScriptBlock |
| 165 | + } |
| 166 | + } |
| 167 | + else |
| 168 | + { |
| 169 | + throw ($script:localizedDataSession.WinRMNotRunningRegisterPsSession -f $Name) |
| 170 | + } |
| 171 | + } |
| 172 | + |
| 173 | +} |
0 commit comments