How do you start a service on a remote computer with Autoit?

- Advertisement -

How do you start a service on a remote computer with Autoit? Autoit can’t directly start a service but you can use the _Rundos command to start and stop services on a remote machine. There is a dos command called SC which stands for Service Control. This command is used to access the services menu and can be used on remote computers.
This is a simple script I wrote to enable a service on multiple machines. This is great if you have a list of pc’s that all need a certain service turned on.

- Advertisement -

#include
$infile = Fileopen(‘c:/infile.txt’,0)
If $infile = -1 Then
MsgBox(0, ‘Error’, ‘Unable to open file.’)
Exit
EndIf
while 1
$compname = FileReadLine($infile)
If @error = -1 Then ExitLoop
$rc = _RunDos(‘sc ‘ & $compname & ‘ START RemoteRegistry’)
Wend
Requirements: You need a file called infile.txt located on the c: drive. This file will need a list of computer names or ip address. One computer name or IP address per line with no extra spaces before or after the computer names and no blank lines.
This is an explanation of what the script does.
#include Is needed for the _Rundos command to work.
$infile = Fileopen(‘c:/infile.txt’,0) This opens the file named infile.txt in read only mode. It sets the file to a variable called $infile for later use.
If $infile = -1 Then
MsgBox(0, ‘Error’, ‘Unable to open file.’)
Exit
EndIf
This if statement checks to see if an error was returned while trying to open the file. If an error comes back a message box will alert you that the file was unable to be opened and will exit the script.
while 1
$compname = FileReadLine($infile)
If @error = -1 Then ExitLoop
$rc = _RunDos(‘sc ‘ & $compname & ‘ START RemoteRegistry’)
Wend
While 1 is a simple way to create a loop that will not end on its own. While statements continue to run until the result is 0. While 1 means it will always be 1 until something aborts the loop. $compname = FileReadLine($infile) is the command to read 1 line from the file variable $infile. The data that is retrieved is stored in a variable called $compname. If @error = -1 Then ExitLoop is the command to check for the end of file. Once there is no more data to read this command will exit the while loop. $rc = _RunDos(‘sc ‘ & $compname & ‘ START RemoteRegistry’) this command is what starts the remote services. _RunDos runs a command in Dos. The command that is being run in Dos is sc. All remote machines are accessed using the command. The & sign is what combines text and variables together. START is a required parameter of the sc command to start a service. You could replace this with STOP to stop a service. Finally RemoteRegistry is simply the service I chose for this demonstration. If you open up services.msc and open the properties of the service you wish to start, it will tell you the service name. Replace the service name where I have RemoteRegistry and it will work for any service you want.
This loop will loop through the infile.txt and start the service on all pc’s that are turned on and listed in the file. If the computer name is invalid or can not be resolved, there is a 15 to 30 second delay before it tries the next computer.

- Advertisement -
How do you start a service on a remote computer with Autoit?, Seekyt
General Contributor
Janice is a writer from Chicago, IL. She created the "simple living as told by me" newsletter with more than 12,000 subscribers about Living Better and is a founder of Seekyt.