Jump to content

Using SC.exe to create a service

Recommended Posts

I created a compiled AutoIt script which automatically shuts down a computer at 11pm every night for all of the computers on the domain. I was able to install this as a service using sc.exe and srvany.exe but for some reason the compiled script will not actually run when it is supposed to. Srvany.exe and my script are both running in the task manager as processes, the shutdown script just is not executing when it is supposed to.

Has anybody else tried this and/or have any ideas on what I can do to get the script to work as a service?

If I just have the file load at startup on the computers, it works fine; it is only when it is running as a service that it does not work.

FYI: the reason I want to run it as a service is so the computers will shut down even if they are logged off or have not yet logged on. It is to save electricity. :lmao:

Thank you,


Link to post
Share on other sites

what OS? modify for your os...

from the server:

use a Scheduled Task to automate a program like the following at whatever time you see fit...

;if it's a large network maybe you can automate listing their names
$computersOnYourDomain = StringSplit("comp1,comp2,comp3,comp4",",") 

;;;;send a shutdown signal to each computer on the domain

For $shutdown = 1 to $computersOnYourDomain[0]
        Run(@Comspec & "/c shutdown \\$computersOnYourDomain[$shutdown]")  

;;;give a couple mins to finish

;;;;make sure it shutdown
For $checkAwake = 1 to $computersOnYourDomain[0]
$pinging = Ping(//$computersOnYourDomain[$checkAwake])
    If $pinging > 0 Then
     Run(@Comspec & "/c shutdown \\$computersOnYourDomain[$shutdown]")

;;;;add some code above to make sure your satisfied with the shutdown process. pick one...
Run(@Comspec & "/c shutdown")

i dont have a network to test this on...(actually im on a Mac =)

EDIT: forgot to turn off the serv :lmao:

Edited by Alterego
Link to post
Share on other sites

Actually, the script is very simple. I have a server that creates a file on a mapped drive called "ShutdownComputers.txt" at 11pm and then waits for about 20 minutes and then deletes that file.

Each computer has a script running which checks to see if that file exists every 5 minutes. So if a computer sees that file on the mapped drive, it will use the AutoIt Shutdown command and power itself off.

This has worked great for computers that are logged on, but I wanted to be able to shut down computers that had not been logged on yet, or those that had been logged off but not shut down.

So, I did some research and found sc.exe and srvany.exe and used those to turn the shutdown script into a service. However, now it is not working; I do not know whether it is not checking or not shutting down or both.

I am not sure how to make sure a service is able to interact with the desktop. The frustrating thing is that my script is exactly the same except for the fact that it now runs as a service rather than just starting when a user logs onto the computer.

I am going to do some more testing to see if it is just the compiled AutoIt script that has problems. I may just create a simple script that sends a message box every few minutes and make that a service and see if it runs.

Link to post
Share on other sites

If it's not logged in, can it see the mapped drive?

"I thoroughly disapprove of duels. If a man should challenge me, I would take him kindly and forgivingly by the hand and lead him to a quiet place and kill him." - Mark TwainPatient: "It hurts when I do $var_"Doctor: "Don't do $var_" - Lar.
Link to post
Share on other sites

If it's not logged in, can it see the mapped drive?


Interesting question. I do not know the answer to that.

However, when I tested the service-based shutdown script, I tested it on machines that were logged in and I had them point to the directory via named pipes rather than a mapped drive so that was not an issue as to why it was not working.

Link to post
Share on other sites

If the aim is to shutdown the computers at 11:00, wouldn't it be much simpler to shedule a task on each computers just to do that?

To do it centrally you could have a script pinging addresses in your range, then run a shutdown based on the IP being alive.


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    No registered users viewing this page.

  • Create New...