powershell - 获取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
解决方案
基于此处的出色答案: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
推荐阅读
- c# - 与 Server.MapPath 等效的功能齐全的 ASP.Net Core
- python - 在 python 中绘制 4D 绘图
- c# - 实体框架,易于迭代的设置和多级层次结构的通用引用
- scala - 如何找出谁以及从哪里调用了一个方法?
- eclipse - IEventBroker 订阅多次处理同一个事件并且处理不正确
- c# - 更改 ListView 颜色会在 Xamarin 列表顶部提供奇怪的文本
- cassandra - 我们可以拆分 Cassandra 数据文件吗?
- c# - 包含两个字符串的Something.Text
- kotlin - 使用通用接口列表中的类
- ios - 如何使用swift 3在视图中显示网格