Jump to content
Sign in to follow this  
wosteen

Automated system shutdown

Recommended Posts

wosteen

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

Share this post


Link to post
Share on other sites
TurionAltec

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

Share this post


Link to post
Share on other sites
wosteen

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!

Share this post


Link to post
Share on other sites
TurionAltec

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.

Share this post


Link to post
Share on other sites
FireFox

@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.


 

OS : Win XP SP2 (32 bits) / Win 7 SP1 (64 bits) / Win 8 (64 bits) | Autoit version: latest stable / beta.
Hardware : Intel(R) Core(TM) i5-2400 CPU @ 3.10Ghz / 8 GiB RAM DDR3.

My UDFs : Skype UDF | TrayIconEx UDF | GUI Panel UDF | Excel XML UDF | Is_Pressed_UDF

My Projects : YouTube Multi-downloader | FTP Easy-UP | Lock'n | WinKill | AVICapture | Skype TM | Tap Maker | ShellNew | Scriptner | Const Replacer | FT_Pocket | Chrome theme maker

My Examples : Capture toolIP Camera | Crosshair | Draw Captured Region | Picture Screensaver | Jscreenfix | Drivetemp | Picture viewer

My Snippets : Basic TCP | Systray_GetIconIndex | Intercept End task | Winpcap various | Advanced HotKeySet | Transparent Edit control

 

Share this post


Link to post
Share on other sites
TurionAltec

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

Share this post


Link to post
Share on other sites
wosteen

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.

Share this post


Link to post
Share on other sites
FireFox

@TurionAltec

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.

I know that but cmd is more famous :)

Cheers, FireFox.


 

OS : Win XP SP2 (32 bits) / Win 7 SP1 (64 bits) / Win 8 (64 bits) | Autoit version: latest stable / beta.
Hardware : Intel(R) Core(TM) i5-2400 CPU @ 3.10Ghz / 8 GiB RAM DDR3.

My UDFs : Skype UDF | TrayIconEx UDF | GUI Panel UDF | Excel XML UDF | Is_Pressed_UDF

My Projects : YouTube Multi-downloader | FTP Easy-UP | Lock'n | WinKill | AVICapture | Skype TM | Tap Maker | ShellNew | Scriptner | Const Replacer | FT_Pocket | Chrome theme maker

My Examples : Capture toolIP Camera | Crosshair | Draw Captured Region | Picture Screensaver | Jscreenfix | Drivetemp | Picture viewer

My Snippets : Basic TCP | Systray_GetIconIndex | Intercept End task | Winpcap various | Advanced HotKeySet | Transparent Edit control

 

Share this post


Link to post
Share on other sites
wosteen

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

Share this post


Link to post
Share on other sites
Lemmens Peter

Thank you very much for your answer.

I will certainly try it.

Best regards,

Peter

Share this post


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
Sign in to follow this  

×