PowerShell远程开启服务


因为Start-Service没有-ComputerName参数,所以你不能非常方便的远程开启服务。所以可能得使用远程会话来Start-Service。另外还有一个命令Set-Service,它支持-ComputerName属性,你也可以通过它来开启远程服务。

Set-Service -Name Spooler -Status Running -ComputerName Server12

倒霉的是Set-Service没有-force选项,一旦尝试停止的服务有依赖服务,就会报错。因为我们不可能停止一个具有依赖的服务。

PS> Get-Service spooler | Set-Service -ComputerName . -Status Running
PS> Get-Service spooler | Set-Service -ComputerName . -Status stopped
Set-Service : 无法停止服务“Print Spooler (spooler)”,因为它具有依赖服务。
所在位置 行:1 字符: 23
+ Get-Service spooler | Set-Service -ComputerName . -Status stopped
+                       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (System.ServiceProcess.ServiceController:ServiceController) [Set-Servi
   ce],ServiceCommandException
    + FullyQualifiedErrorId : ServiceHasDependentServicesNoForce,Microsoft.PowerShell.Commands.SetServiceCommand

原文链接:Starting Services Remotely

本文链接: https://www.pstips.net/starting-services-remotely.html
请尊重原作者和编辑的辛勤劳动,欢迎转载,并注明出处!

关于 Mooser Lee

我是一个Powershell的爱好者,创建了PowerShell中文博客,热衷于Powershell技术的搜集和分享。本站部分内容来源于互联网,不足之处敬请谅解,并欢迎您批评指正。

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注