【发布时间】: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