|
257 | 257 | } |
258 | 258 | } |
259 | 259 |
|
| 260 | + function Get-MockFailureActionsData |
| 261 | + { |
| 262 | + [CmdletBinding()] |
| 263 | + param ( |
| 264 | + [Parameter()] |
| 265 | + [System.String] |
| 266 | + $DataSetName |
| 267 | + ) |
| 268 | + |
| 269 | + switch ($DataSetName) { |
| 270 | + '1ActionNoCommandOrMessage' { |
| 271 | + $data = @{ |
| 272 | + resetPeriodSeconds = 86400 |
| 273 | + hasRebootMessage = 0 |
| 274 | + hasFailureCommand = 0 |
| 275 | + failureActionCount = 1 |
| 276 | + failureCommand = $null |
| 277 | + rebootMessage = $null |
| 278 | + actionsCollection = @([PSCustomObject]@{ |
| 279 | + type = 'RESTART' |
| 280 | + delaySeconds = 30000 |
| 281 | + }) |
| 282 | + failureActionsOnNonCrashFailures = $true |
| 283 | + } |
| 284 | + } |
| 285 | + Default {} |
| 286 | + } |
| 287 | + |
| 288 | + $keyData = New-Object 'System.Collections.Generic.List[int]' |
| 289 | + |
| 290 | + $keyData.add($data.resetPeriodSeconds) | Out-Null |
| 291 | + $keyData.add($data.hasRebootMessage) | Out-Null |
| 292 | + $keyData.add($data.hasFailureCommand) | Out-Null |
| 293 | + $keyData.add($data.failureActionCount) | Out-Null |
| 294 | + $keyData.add(20) | Out-Null |
| 295 | + |
| 296 | + foreach ($action in $data.ActionsCollection) { |
| 297 | + $keyData.add([ACTION_TYPE]$action.type) | Out-Null |
| 298 | + $keyData.add($action.delaySeconds) | Out-Null |
| 299 | + } |
| 300 | + |
| 301 | + $key = @{ |
| 302 | + FailureActions = $keyData | Format-Hex -raw | Select-Object -ExpandProperty bytes |
| 303 | + } |
| 304 | + |
| 305 | + if ($data.failureActionsOnNonCrashFailures) |
| 306 | + { |
| 307 | + $key.failureActionsOnNonCrashFailures = 1 |
| 308 | + } |
| 309 | + |
| 310 | + if ($data.hasRebootMessage) |
| 311 | + { |
| 312 | + $key.RebootMessage = 'Mocked Reboot Message' |
| 313 | + } |
| 314 | + |
| 315 | + if ($data.hasFailureCommand) |
| 316 | + { |
| 317 | + $key.FailureCommand = 'C:\new\command.exe' |
| 318 | + } |
| 319 | + |
| 320 | + $GetValueNames = {$this.keys} |
| 321 | + $GetValue = {param($key) $this[$key]} |
| 322 | + |
| 323 | + Add-Member -InputObject $key -MemberType ScriptMethod -Name GetValueNames -Value $GetValueNames |
| 324 | + Add-Member -InputObject $key -MemberType ScriptMethod -Name GetValue -Value $GetValue |
| 325 | + |
| 326 | + @([PSCustomObject]$data, $key) |
| 327 | + } |
| 328 | + |
| 329 | + |
260 | 330 | Mock -CommandName 'Get-Service' -MockWith { } |
261 | 331 | Mock -CommandName 'Get-ServiceCimInstance' -MockWith { } |
262 | 332 | Mock -CommandName 'ConvertTo-StartupTypeString' -MockWith { } |
@@ -303,24 +373,12 @@ try |
303 | 373 | DesktopInteract = $true |
304 | 374 | } |
305 | 375 |
|
306 | | - $testServiceFailureActions = @{ |
307 | | - resetPeriodSeconds = 86400 |
308 | | - hasRebootMessage = 0 |
309 | | - hasFailureCommand = 0 |
310 | | - failureActionCount = 1 |
311 | | - failureCommand = $null |
312 | | - rebootMessage = $null |
313 | | - actionsCollection = @([PSCustomObject]@{ |
314 | | - type = 'RESTART' |
315 | | - delaySeconds = 30000 |
316 | | - }) |
317 | | - failureActionsOnNonCrashFailures = $true |
318 | | - } |
| 376 | + $testServiceFailureActions, $failureRegistryKey = Get-MockFailureActionsData -DataSetName '1ActionNoCommandOrMessage' |
319 | 377 |
|
320 | 378 | Mock -CommandName 'Get-Service' -MockWith { return $testService } |
321 | 379 | Mock -CommandName 'Get-ServiceCimInstance' -MockWith { return $testServiceCimInstance } |
322 | 380 | Mock -CommandName 'ConvertTo-StartupTypeString' -MockWith { return $convertToStartupTypeStringResult } |
323 | | - Mock -CommandName 'Get-ServiceFailureActions' -MockWith { return $testServiceFailureActions } |
| 381 | + Mock -CommandName 'Get-Item' -ParameterFilter { $Path -eq "HKLM:\SYSTEM\CurrentControlSet\Services\$($testService.name)" } -MockWith { return $failureRegistryKey} |
324 | 382 |
|
325 | 383 | Test-GetTargetResourceDoesntThrow -GetTargetResourceParameters $getTargetResourceParameters -TestServiceCimInstance $testServiceCimInstance |
326 | 384 |
|
|
0 commit comments