首页 > 解决方案 > 获取windows服务失败动作值

问题描述

需要获取为服务设置的失败操作。下面的 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

标签: powershell

解决方案


基于此处的出色答案: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         

推荐阅读