1

I would like to manage a service on a remote machine via cmd (or powershell) using sc, e.g.

sc \\hostname query service_name

I need to use a specific user account to make this connection, which I understand I can do by logging into my workstation as the user and running cmd from that context.

However, I want to specify the account to use in the command line, something like:

sc username:password \\hostname query service_name

Is this possible? sc appears to use the current windows-authentication implicitly, and I'm not sure how to switch this easily, or in fact what is doing this authentication (and therefore where to look to find out how to change current user for a single specific command).

Thanks for any help

ricky116
  • 744
  • 8
  • 21

3 Answers3

1

I'd recommend using the *-Service cmdlets over sc.exe.

PS C:\> Get-Command -Noun Service | select Name

Name
----
Get-Service
New-Service
Restart-Service
Resume-Service
Set-Service
Start-Service
Stop-Service
Suspend-Service

You can use Invoke-Command to run them on a remote host with different credentials:

$cred = Get-Credential

Invoke-Command -Computer server -Credential $cred -ScriptBlock {
  Get-Service
}
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
0

You can use PowerShell and WMI to do this remotely:

$service = gwmi win32_service -computername hostname | ? { $_.name -eq "service_name" }
$service.change($null,$null,$null,$null,$null,$false,"username","password",$null,$null,$null)

Check out the msdn reference

arco444
  • 22,002
  • 12
  • 63
  • 67
  • It would appear that `$service = gwmi win32_service -computername hostname | ? { $_.name -eq "service_name" }` also requires authentication of some description? From that line I am getting the response `NotSpecified: (:) [Get-WmiObject], UnauthorizedAccessException`, so it doesn't even get to the .change method with the username/password that is required. Thanks though – ricky116 Jan 13 '15 at 14:05
  • You'd need to be running the script in an admin elevated console with an account that has admin rights on the remote computer for this to work – arco444 Jan 13 '15 at 14:07
  • Ah, that's the crux of it - 'with an account that has admin rights on the remote computer' - by that you mean the account I am logged into my local windows with (and running powershell from) right? I am looking to say "for this command I want to be logged into local windows as X:Y" where X:Y has admin access to the remote machine but the local windows account I normally use does not.. – ricky116 Jan 13 '15 at 14:14
  • tbh you'd have the same issue using `sc` remotely, you still need to run it with the correct rights. If you wanted to logon as a different user that has rights to the remote machine whilst using a different one locally, you could try using `runas.exe` – arco444 Jan 13 '15 at 14:16
  • A combination of `runas.exe` and `sc` was what I was looking for, I am now able to manage a remote service as a different user on my domain - thanks! – ricky116 Jan 13 '15 at 15:00
0

Additional to the great answers already provided, another way I have uncovered to do this is to use the excellent tool CPAU (which likely does something akin to the other answers here behind the scenes).

It appears to run a .bat from a different security context, so in my case I execute:

.\CPAU\CPAU.exe -u DOMAIN\USERNAME-p PASSWORD -k -ex ".\restart_service.bat" > null

where restart_service.bat uses a regular sc command, but now from a different user account on the domain than the one I'm logged into my desktop with.

Thanks

ricky116
  • 744
  • 8
  • 21