How can I start/stop a Windows Service through a Linux bash script?

9,342

Solution 1

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.

Solution 2

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.

Solution 3

start = net rpc service start SERVICENAME -I IPADDRESS -U USERNAME%PASSWORD

stop = net rpc service stop SERVICENAME -I IPADDRESS -U USERNAME%PASSWORD

status = net rpc service status SERVICENAME -I IPADDRESS -U USERNAME%PASSWORD

If your password contains specials escape them with a backslash \.

Share:
9,342

Related videos on Youtube

Byob
Author by

Byob

Updated on September 18, 2022

Comments

  • Byob
    Byob over 1 year

    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 }
    
    • Keltari
      Keltari over 10 years
      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.
    • Byob
      Byob over 10 years
      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
      Byob over 10 years
      Just to clarify: The service control was done through Windows Batch using ' sc <query | start | stop> MyServiceName '
  • Byob
    Byob over 10 years
    "A pocketknife will cut down a tree but a chainsaw was built to cut down the tree" - Definitely agree -
  • tbenz9
    tbenz9 over 10 years
    Hi Byob, if this answer was correct, please accept it as the correct answer so it is marked as solved.
  • Hastur
    Hastur over 3 years
    "If the password blank, what should i do to write ?" Comment by an anonymous user.