Softomotive Software Solutions

The power of Windows Scripting without the hassle!

Start Services Remotely

Download the ScriptAhead File: StartServicesRemotely.saf

The Problem:
You want a script that starts a specific service on specific computers of your network. The list of computers is stored in a text file. Finally you want the script to report on which computers the service failed to start.

The Solution:
Step 1: You load the list of computers that is stored in a text file. To do so you will use the task "Read Text File Contents"

In the dialog box you enter the path of the text file you want to read. The contents of the file will be stored in a variable as a collection of text values (each line of the file will be an item of the collection)

You may change the proposed name of the variable ($FileContents) to a more comprehensive name, in our case $ComputerNames

 

Step 2: To select a specific service you will use the "Get Service" task

In the dialog box you must choose to get services from remote computers. In the "Computer(s)" field you enter the $ComputerNames variable. Remember that this is the variable defined in the previous task that contains the contents of the text file, i.e. the names of the computers

To retrieve only selected services instead of all from the computers you click on the "Filters" tab and you specify the appropriate criteria. Let's suppose that you want to start the MSSQLSERVER service on the remote computers

Now you have a new variable called $Services that contains a collection of service objects (Win32_Service objects in WMI). This variable contains an instance of the MSSQLSERVER service from each computer. Therefore if the text file contained 10 computer names the $Services variable will be a collection of 10 service objects.

 

Step 3: To start the services you will use the "Start Service" task

In the properties dialog box, in the "Service(s) to Start" field you enter $Services which is the name of the variable defined in the previous task and contains the retrieved services

Since you want to know whether all services started or one or more failed to start click on the "Task Results" tab

Uncheck the "Start Service Succeeded" (you will not need this variable) and check the "Services Failed to Start". This will generate a variable that will contain a collection of the Services that failed to start (if any).

 

Step 4: To check whether any of the services failed to start you need to check the length of the collection "$ServicesFailedToStart". To retrieve the length you will use the "Count Collection" task

In the Properties Dialog Box, in the "Collection" field enter the variable $ServicesFailedToStart

So now in the $CollectionLength variable you have the length of the collection that holds the services that failed to start. If the collection length is 0 then all services started correctly. If not, some services failed to start

 

Step 5: Check the collection length using the "If" task

Fist you will check whether all services started correctly

If so, you can use the "Display Message" to display an info message to the user

In the properties you enter the message to be displayed if the service started correctly on all computers

You can use the "Else" task to execute some task if the condition of the "If" task is false (in this case if the length of the collection $ServicesFailedToStart is more than 0)

After the "Else" task you can insert the following "Display Message" task to inform the user that some tasks failed to start

Using the following task the script will display the list of computers on which the MSSQLSERVER service failed to start. Since the $Services variable contains a collection of Win32_Service objects you may choose to display the SystemName property of each object, which is the computer where the service is retrieved from. Therefore using the following task the script will display the names of the computers for the services stored in $ServicesFailedToStart

 

Finally the completed script is:

 

You may either run the script from within ScriptAhead or just generate it in a wsf file and use it independently.