使用 Powershell 设置远程服务的恢复选项? [英] Set Remote Service's Recovery Options using Powershell?
问题描述
我真的很难让它发挥作用.希望有人能帮助我!
I am have a really hard time getting this to work. Hopefully someone can help me out!
我目前正在为一项服务编写 Powershell 部署脚本.安装服务后,我想将服务恢复选项设置为每次服务在 0 分钟后崩溃时重新启动服务".
I am currently working on a Powershell deployment script for a service. After installing the service, I'd like to set the Service Recovery options to "Restart the Service" every time the service crashes after 0 minutes.
有谁知道如何使用 Powershell 为远程机器设置这些选项?
Does anyone know how to do this using Powershell to set these options for remote machine?
推荐答案
如果是本地服务,您可以使用 sc.exe
但是您想更改远程服务的设置.一种方法是直接使用远程注册表设置注册表项:
If it were a local service you could use sc.exe
however you want to change settings for remote service. One way to do this is to set the registry keys directly using remote registry:
以下是您需要的设置:
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\<ServiceShortName>
Value Name Data Type Description
FailureActions REG_BINARY Configuration information for 1st, 2nd, and subsequent failures.
我要做的是按照您希望的方式设置服务恢复选项,然后读取注册表值 FailureActions
What I would do is setup the service recovery options the way you want them and then read the registry value FailureActions
$actions = get-itemproperty hklm:\system\currentcontrolset\services\<ServiceShortName> | select -Expand FailureActions
然后将其序列化到磁盘以备后用:
Then serialize this to disk for use later:
$actions | Export-Clixml C:\actions.xml
当您准备好远程配置服务时,重新读取FailureActions数据,连接到远程注册表并设置注册表项:
When your ready to remotely configure the service, re-read the FailureActions data, connect to the remote registry and set the registry key:
$actions2 | Import-Clixml C:\actions.xml
$key = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey([Microsoft.Win32.RegistryHive]::LocalMachine, "<RemoteComputerName>")
$key2 = $key.OpenSubKey('SYSTEM\CurrentControlSet\Services\<ServiceShortName>', $true)
$key2.SetValue('FailureActions', ([byte[]] $actions))
这篇关于使用 Powershell 设置远程服务的恢复选项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!