使用 Powershell 设置远程服务的恢复选项?
我真的很难让它发挥作用。希望有人可以帮助我!
我目前正在为一项服务编写 Powershell 部署脚本。安装服务后,我想将服务恢复选项设置为每次服务在 0 分钟后崩溃时“重新启动服务”。
有谁知道如何使用 Powershell 为远程计算机设置这些选项?
I am have a really hard time getting this to work. Hopefully someone can help me out!
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.
Does anyone know how to do this using Powershell to set these options for remote machine?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您可以使用 sc.exe 编写 powershell 函数,如此处所述。该函数将类似于:
您可以像这样调用该函数:
注意:运行脚本的帐户必须在远程服务器上具有管理员权限。
You can write a powershell function using sc.exe as explained here. The function will look something like:
And you can call the function like:
Note: The account you are running the script must have admin rights on the remote server.
请简化...
在powershell代码中使用最古老的sc.exe。更加简单且功能齐全。
重新启动(以毫秒为单位)。最后一次重置,以秒为单位。
(每次重启3分钟,重置1天)
再见!
Please, simplify..
Use the oldiest sc.exe in powershell code. Is more easy and fully functional.
The restarts, in milliseconds. And the last reset, in seconds.
(Each restart in 3 min, and the reset in 1 day)
Bye!
我采纳了 @Mohammad Nadeem 的想法,并通过对所有行动(而不仅仅是主要行动)的全力支持来扩展它。我还使用了服务的显示名称而不是服务名称,因此提供参数更容易一些。
我创建了一篇关于它的博客文章: https://evotec.xyz/set-服务恢复选项-powershell/。除了重新启动服务之外,我还没有在其他场景中对此进行过测试。可能需要一些工作来支持所有场景。
I've taken @Mohammad Nadeem idea and expanded it with full support of all actions instead of just primary one. I've also used Display Name for service rather than service name so it's a bit easier to provide parameter.
I've created a blog post about it: https://evotec.xyz/set-service-recovery-options-powershell/. I've not tested this in other scenarios than restart of service. Probably would need some work to support all scenarios.
如果它是本地服务,您可以使用
sc.exe
,但是您想更改远程服务的设置。一种方法是使用远程注册表直接设置注册表项:以下是您需要的设置:
我要做的是以您想要的方式设置服务恢复选项,然后读取注册表值
FailureActions
然后将其序列化到磁盘以供以后使用:
当您准备好远程配置服务时,重新读取 FailureActions 数据,连接到远程注册表并设置注册表项:
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:Here are the settings you'll need:
What I would do is setup the service recovery options the way you want them and then read the registry value
FailureActions
Then serialize this to disk for use later:
When your ready to remotely configure the service, re-read the FailureActions data, connect to the remote registry and set the registry key:
Carbon 库有一个非常全面的
Install-Service
cmdlet,可让您指定恢复操作,例如(改编自 Install-Service 文档页面):这将安装
DeathStar
服务并重新启动第一次失败后延迟 10 秒。The Carbon library has a pretty comprehensive
Install-Service
cmdlet which lets you specify recovery actions, e.g. (adapted from the Install-Service doc page):This will install the
DeathStar
service and restart with a 10 second delay after the first failure.jborean93 创建了一个自定义类型,将本机 C# 服务对象和方法公开给 PowerShell。附带的 Get-ServiceRecovery 和 Set-ServiceRecovery 函数使您可以轻松地在 PowerShell 中查看和更改服务恢复设置。
https://gist.github.com/jborean93/889288b56087a2c5def7fa49b6a8a0ad
对于 DSC 粉丝来说,看起来像这样未来某个时候,该功能也会被纳入 xPSDesiredStateConfiguration (xService) 中。
https://github.com/dsccommunity/xPSDesiredStateConfiguration/pull/679
jborean93 has created a custom type that exposes the native C# service objects and methods to PowerShell. The included Get-ServiceRecovery and Set-ServiceRecovery functions make it easy to view and change service recovery settings within PowerShell.
https://gist.github.com/jborean93/889288b56087a2c5def7fa49b6a8a0ad
For the DSC fans out there, looks like this functionality is also being worked into xPSDesiredStateConfiguration (xService) at some point in the future.
https://github.com/dsccommunity/xPSDesiredStateConfiguration/pull/679