【问题标题】:Powershell stop-service error: cannot find any service with service namePowershell停止服务错误:找不到具有服务名称的任何服务
【发布时间】:2012-10-16 15:07:12
【问题描述】:

我正在编写将供应商软件部署到大型环境的脚本。第一步是停止相关服务。该脚本在我们的测试环境中执行良好,但我不是生产环境中的管理员,所以我确信这是权限问题。我无法获得 prod 环境的管理员权限,因此我需要尝试找出我可能需要设置的任何内容,以授予远程停止服务的权限。我发出以下命令来停止服务:

Stop-Service -InputObject $(Get-Service -Computer $destination.Server -Name ("moca."+$destEnv))

当我运行脚本时,我得到:

Cannot find any service with service name 'moca.WMSPRD'.
+ CategoryInfo          : ObjectNotFound: (moca.WMSPRD:String) [Get-Service], ServiceCommandException
+ FullyQualifiedErrorId : NoServiceFoundForGivenName,Microsoft.PowerShell.Commands.GetServiceCommand

Cannot validate argument on parameter 'InputObject'. The argument is null or empty. Supply an argument that is not null or empty and then try the command again.
+ CategoryInfo          : InvalidData: (:) [Stop-Service], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.PowerShell.Commands.StopServiceCommand

该服务肯定存在,如果我 rdp 进入目标框并在本地发出 stop-service 命令,它将执行。所以有些东西阻止我远程停止服务。有什么想法吗?

编辑: 一位同事建议使用 WMI,因此尝试将 Stop-Service 行替换为:

(Get-WmiObject -computer $destination.Server Win32_Service -Filter ("Name='moca."+$destEnv+"'")).InvokeMethod("StopService",$null)

我得到:

Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))
+ CategoryInfo          : NotSpecified: (:) [Get-WmiObject], UnauthorizedAccessException
+ FullyQualifiedErrorId : System.UnauthorizedAccessException,Microsoft.PowerShell.Commands.GetWmiObjectCommand

【问题讨论】:

  • 发出命令 Get-Service -ComputerName YourRemoteComputerName 时的输出是什么?
  • 如果我远程发出它,我会得到:Get-Service:找不到任何服务名称为“moca.wmsprd”的服务。如果我 rdp 到盒子并发出命令,它就可以工作。
  • 我的意思是只远程发出 Get-Service 命令并查看返回的整个服务列表。
  • 我得到:Get-Service:无法在计算机“XXXX”上打开服务控制管理器。此操作可能需要其他权限。我注意到在某些机器上这个命令成功了。权限可能没有统一应用。我会查明我的脚本是否适用于任何地方并提供反馈。

标签: powershell


【解决方案1】:

如果你知道确切的服务名称,你可以试试这个

(Get-WmiObject -computerName $_.name Win32_Service -Filter "Name='moca'").StopService()

这里我假设服务名称是 moca

【讨论】:

  • 过滤器可以像 sql 一样,% 是通配符:"name like 'moca%'"
【解决方案2】:

DCOM 是否在远程计算机上工作?我知道如何使用使用 wsman 的远程 powershell 来做到这一点:

 invoke-command comp001 { stop-service adobearmservice }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-10-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多