wosteen Posted January 9, 2009 Posted January 9, 2009 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
TurionAltec Posted January 9, 2009 Posted January 9, 2009 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
wosteen Posted January 9, 2009 Author Posted January 9, 2009 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!
Bert Posted January 9, 2009 Posted January 9, 2009 http://www.aumha.org/win5/a/shutcut.php The Vollatran project My blog: http://www.vollysinterestingshit.com/
TurionAltec Posted January 9, 2009 Posted January 9, 2009 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.
FireFox Posted January 9, 2009 Posted January 9, 2009 @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.
TurionAltec Posted January 10, 2009 Posted January 10, 2009 (edited) 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 January 10, 2009 by TurionAltec
wosteen Posted January 12, 2009 Author Posted January 12, 2009 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.
FireFox Posted January 12, 2009 Posted January 12, 2009 @TurionAltecAnd 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.
Lemmens Peter Posted January 13, 2009 Posted January 13, 2009 wosteen,I got the same problem here :(shutdown does not work when not logged on)http://www.autoitscript.com/forum/index.php?showtopic=87572I see you got a solution, but I don't get it.Would you be so kind to post the 'working' script please ?Thanx,Peter
wosteen Posted January 22, 2009 Author Posted January 22, 2009 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
Lemmens Peter Posted January 22, 2009 Posted January 22, 2009 Thank you very much for your answer. I will certainly try it. Best regards, Peter
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now