获取Windows服务失败操作值

时间:2019-06-13 06:03:24

标签: powershell

需要为服务设置失败操作。下面的PS查询给出了模糊的值

get-itemproperty hklm:\system\currentcontrolset\services\<ServiceName> | select -Expand FailureActions

我需要获取“首次失败”,“第二次失败”和“后续失败”字段的值。

上述PS查询的结果类似于

0
0
0
0
0
0
0
0
0
0
0
0
3
0
0
0
20
0
0
0
1
0
0
0
96
234
0
0
0
0
0
0
96
234
0
0
0
0
0
0
96
234
0
0

1 个答案:

答案 0 :(得分:1)

基于此处的出色答案:What REG-BINARY to set for FailureAction for service,这是一个选择:

function Get-ServiceRecovery {

    Param($ServiceName)

    $failureActions = (Get-ItemProperty hklm:\system\currentcontrolset\services\$ServiceName).FailureActions

    $possibleActions = 'NoAction', 'RestartService','RestartComputer','RunProgram'

    [PsCustomObject]@{
        Service           = $ServiceName
        FirstFailure      = $possibleActions[$failureActions[20]]
        SecondFailure     = $possibleActions[$failureActions[28]]
        SubsequentFailure = $possibleActions[$failureActions[36]]
    }

}

因此,像这样调用:Get-ServiceRecovery -ServiceName 'W32Time'会得到这样的输出:

Service FirstFailure   SecondFailure  SubsequentFailure
------- ------------   -------------  -----------------
W32Time RestartService RestartService NoAction