Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I want to create a script that will be kicked off through an SSH server to start/stop a Windows service. The SSH user is part of the Administrator group. However, when I execute the script, I get an error stating that the service cannot be started.

How can I ensure that my script is kicked off with Admin privileges? I have done similar scripts in Linux using sudo service myservice start/stop. I am looking for an equivalent in Windows, if such thing exists.

My ps1 shell looks something like this:

function start() { Start-Service MyService // verify service started here }

function stop() { Stop-Service MyService // verify service stopped }

function status() { // Return Running/Stopped for MyService }
share|improve this question
    
not sure how you would do it from Linux and SSH... having the user be an admin is not enough, the environment needs to be elevated as well. For instance, just opening a command prompt and typing net start <service> wont work, as the command prompt needs to be run as administrator. –  Keltari Oct 9 '13 at 18:37
    
Thank you all for your replies! I looked into using the wmi API, but I put it on hold since that required getting new software into the environment, which is a cumbersome process... What I ended up using is creating a special user that had permissions to start/stop my targeted service. As for the SSH connectivity between Windows/Linux, I installed a Windows ssh server and used public key authentication to allow my user to log in through a linux script. –  Byob Oct 9 '13 at 22:37
    
Just to clarify: The service control was done through Windows Batch using ' sc <query | start | stop> MyServiceName ' –  Byob Oct 14 '13 at 12:38

2 Answers 2

up vote 3 down vote accepted

I would have the SSH script start a powershell script. Powershell is able to elevate itself and then start stop services, and check their status.

Some pseudo code:

ssh user@server powershell.bat

Where powershell.bat has all the necessary code to do what you want with the services.

share|improve this answer
    
This worked for me. Thanks! –  Byob Oct 9 '13 at 22:40
2  
Hi Byob, if this answer was correct, please accept it as the correct answer so it is marked as solved. –  tbenz9 Oct 9 '13 at 23:31

I know you want to use bash/ssh but the easiest way in my experience to administer Windows from a Linux box is with Python and WMI

http://timgolden.me.uk/python/wmi/index.html

http://timgolden.me.uk/python/wmi/cookbook.html

http://timgolden.me.uk/python/wmi/tutorial.html

import wmi

# Open a connection to a remote machine
c = wmi.WMI (
computer="other_machine",
user="you",
password="something"
)

# start the service
c.<service>.StartService ()

This will the simplest way I know of if you don't need BASH/SSH, if you need to use SSH it gets more complicated as now you will need Python and WMI installed on any machine you want to run this on in which case you may want to go with a language that is already installed on every machine (VBScript, Powershell, etc). This method also assumes you have the WMI service enabled on the Windows box which some admins are not fond of doing.

Linux boxes are great for administering mixed environments but make sure you use the right tool for the job, a pocketknife will cut down a tree but a chainsaw was built to cut down the tree.

share|improve this answer
    
"A pocketknife will cut down a tree but a chainsaw was built to cut down the tree" - Definitely agree - –  Byob Oct 9 '13 at 19:31

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.