Jump to content

Automated system shutdown


Recommended Posts

I'm writing a utility that will run as a Windows scheduled task. It will shutdown the system at a specified time with a user prompt allowing the shutdown to be aborted. This way, when we roll it out across the network, it won't force a shutdown at 6:00pm on a user who's working late.

It works well so long as someone is logged on. What I'm having issues with is if no one is logged on. I don't need to display a message if the machine isn't logged on. Can I check if the system is logged on, and not display a MsgBox, just shutdown?

Thanks!

Wayne

$x = MsgBox(0,"Shutdown","In accordance with company power saving policies, your computer will be shutdown in 5 minutes." & @CRLF & @CRLF & "If you wish to abort this process, click the OK button below.",500)

If $x = -1 Then
    ; Code 13 = 1 (shutdown) + 4 (force) + 8 (power down)
    Shutdown(13)
Else
    MsgBox(0,"Shutdown Aborted","Please remember to power off your computer when you are done.")
EndIf
Link to comment
Share on other sites

Processexists("Explorer.exe")

If Processexists("Explorer.exe")==0 Then
Shutdown(13)
Else

$x = MsgBox(0,"Shutdown","In accordance with company power saving policies, your computer will be shutdown in 5 minutes." & @CRLF & @CRLF & "If you wish to abort this process, click the OK button below.",500)

If $x = -1 Then
    ; Code 13 = 1 (shutdown) + 4 (force) + 8 (power down)
    Shutdown(13)
Else
    MsgBox(0,"Shutdown Aborted","Please remember to power off your computer when you are done.")
EndIf
EndIf

Link to comment
Share on other sites

Thank you Turion. While this does indeed work (and was exactly what I was looking for), I now have another problem.

The program works fine while logged in. The check for explorer.exe while not logged in works as expected. I added a write to a debug file to verify in the if statement. The Shutdown(13) just doesn't work while not logged in. I also tried doing

Run(@ComSpec & " /c " & "shutdown.exe -s -f", @TempDir, @SW_HIDE)

instead of the Shutdown(13). That doesn't work either. Any ideas?

Thanks!

Link to comment
Share on other sites

shutdown -s -f -t 0 ?

How is the script being launched? Is it in Task scheduler? Does the account it's running under have permissions to shut down? I've launched scripts from task scheduler that send a logged out PC to stadby, and it worked. Haven't down shutdown.

Link to comment
Share on other sites

@TurionAltec

Its run from cmd, or by batch batch file that is run from cmd too...

ShellExecute("cmd.exe","shutdown -s -f -t 0")

-s = shutdown

-f = force end of process

-t = time before shutdown

for more help tape "shutdown" in cmd

Cheers, FireFox.

Link to comment
Share on other sites

Firefox,

I know how shutdown works, I was suggesting alternative parameters, and I was asking under what context the script was being executed (Task scheduler, remotely from another PC using something like PSexec, as a service, etc), as that could affect the permissions available to do a shutdown.

And shutdown is not an internal command of cmd.exe, but rather a separate executable, so you can actually call it directly if you wish.

Give this a shot:

run("shutdown.exe /i")

I think you'll find that it will run directly(without launching cmd) and give you a shutdown dialog box

Edited by TurionAltec
Link to comment
Share on other sites

Thanks everyone for your help. Turion's suggestion was dead-on, my implementation was not however. LOL

I wedged his (her?) suggestion into the code I already had re-worked a couple of times, and I wound up with a situation where I had an undefined variable if it was not logged in, thus the failure. All is well in code land again.

Link to comment
Share on other sites

  • 2 weeks later...

wosteen,

I see you got a solution, but I don't get it.

Would you be so kind to post the 'working' script please ?

Thanx,

Peter

Of course! I apologize for the delay. Here's the complete script. It's not much, but works perfectly.

I also added an option to the shutdown command that writes an entry to the Windows Event Log stating the reason for the shutdown.

; Run as a scheduled windows job to shutdown machine with prompt if user is still online.

$x = 0

; Is someone logged on?
If ProcessExists("explorer.exe") Then
    $x = MsgBox(0,"Shutdown","In accordance with SDCC power saving policies, your computer will be shutdown in 5 minutes." & @CRLF & @CRLF & "If you wish to abort this process, click the OK button below.",10)
EndIf

If $x = 1 Then
    MsgBox(0,"Shutdown Aborted","Please remember to power off your computer when you are done.")
Else
    Run(@ComSpec & " /c " & "shutdown.exe -s -f -t 0 -d p:0:0 -c " & Chr(34) & "System shutdown for power saving measures" & Chr(34), @TempDir, @SW_HIDE)   
EndIf
Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...