2

I'm working on a script to deploy vendor software to a large environment. The first step is to stop the services in question. The script executes fine in our test environment, but I'm not an admin in the production environment so I'm convinced it's a permissions issue. I can't get admin rights to the prod environment so I need to try to find out anything that I may need to set to grant permissions to stop services remotely. I'm issuing the following command to stop services:

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

When I run the script I get:

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

The service definitely exists and if I rdp into the target box and issue the stop-service command locally it will execute. So there is something preventing me from stopping the service remotely. Any ideas?

Edit: A coworker suggested using WMI so tried replacing the Stop-Service line with:

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

and I get:

Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))
+ CategoryInfo          : NotSpecified: (:) [Get-WmiObject], UnauthorizedAccessException
+ FullyQualifiedErrorId : System.UnauthorizedAccessException,Microsoft.PowerShell.Commands.GetWmiObjectCommand
JDH
  • 2,618
  • 5
  • 38
  • 48
  • What is the output when you issue the command Get-Service -ComputerName YourRemoteComputerName? – dugas Oct 16 '12 at 15:15
  • If i issue it remotely I get: Get-Service : Cannot find any service with service name 'moca.wmsprd'. If I rdp to the box and issue the command it works. – JDH Oct 16 '12 at 15:32
  • 1
    What I mean is to issue only the Get-Service command remotely and see the entire list of services that are returned. – dugas Oct 16 '12 at 16:15
  • I get: Get-Service : Cannot open Service Control Manager on computer 'XXXX'. This operation might require other privileges. I noticed on some machines this command succeeds. Permissions are probably not applied uniformly. I will find out if my script works anywhere and provide feedback. – JDH Oct 16 '12 at 16:42
  • HOw does this place get the exact scrubbed code from Stack? http://anycodings.com/questions/powershell-stop-service-error-cannot-find-any-service-with-service-name. affiliated? or just a ripoff? Note you willl need to copy the ip into incognito, or they will block your ip if you click the link from stack site here! – blamb Sep 22 '22 at 15:46

2 Answers2

0

If you know the exact service name you can try this

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

Here im assuming that the service name is moca

PowerShell
  • 1,991
  • 8
  • 35
  • 57
  • The filter can be like sql where `%` is the wildcard: `"name like 'moca%'"` – js2010 Jun 14 '20 at 17:21
  • im not clear how to use this, is $_.name supposed to be $_., e.g. we fill in our pc name? i cant get this code to work, but im trying to log results from a botched service start – blamb Sep 22 '22 at 15:49
0

Is DCOM working on the remote computer? I know how to do it with remote powershell, which uses wsman:

 invoke-command comp001 { stop-service adobearmservice }
js2010
  • 23,033
  • 6
  • 64
  • 66