PowerShell:按可执行文件名称重新启动服务
我实现
了我的第一个 PowerShell 脚本,它执行一些设置、设置注册表项,最后需要重新启动服务。问题是我只有可执行文件的名称,但没有服务名称。 Restart-Service 只能与服务名称一起使用。谷歌搜索(还有必应)并没有给我带来太多结果。
我想知道是否有办法通过可执行文件名称重新启动服务?
我知道我可以通过可执行文件名称获取进程,但仅仅终止进程并重新启动它不是一个好的选择,因为服务启动/Stop 函数未被调用,并且可能无法正常工作。
谢谢。
all
I implemented my first PowerShell script, that does some setup, sets registry keys and at then end needs to restart services. The problem is that I have only have name of the executable, but not service name. Restart-Service can work only with name of the service. Googling (well Binging also) around didn't give me much result.
I was wondering whether there is a way to restart service by executable name?
I know that I can get process by executable name, but just killing the process and starting it again is NOT good choice, since service Start/Stop functions are not called and it may not work properly.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以尝试使用 wmi 并执行以下操作:
You can try using wmi and do something like this:
您可以使用 WMI 执行此操作:
编辑:更改脚本以重新启动服务,而不仅仅是停止它。
You can do this using WMI:
Edit: Changed script to restart service, not just stop it.