AutoHotkey Script to log off not working

This AutoHotkey script isn't working for me and I son't know why.

Goal:  To provide a simple way for users to log off of their Win pc's.

I'd like to put an icon (.exe) on their desktops and when they click it, be told via MsgBox their systems will log them off in xx minutes with (ok, cancel) buttons.  Clicking cancel would cancel the process, clicking ok would start the countdown to log off.

What's happening is that after I click ok, I wait and wait and wait....nothing happens.  I see the icon in the system tray and the process in the Task Manager, but no results (logoff).  Here's script:

#Persistent
LogOffTime := A_Now
LogOffTime += 1, Minute
FormatTime, LogOffTime, %LogOffTime%, Time

SetTimer, LogOff, 60000
MsgBox, 49, Auto Log-off, This workstation will be logged off in 1 minute at %LogOffTime%.`nPlease click Cancel to stay logged in.
IfMsgBox, Cancel
  ExitApp
Return

LogOff:  
Shutdown, 0
ExitApp
H. G.Asked:
Who is Participating?
 
Joe Winograd, Fellow&MVEDeveloperCommented:
Hi HG,
Your script works perfectly here, although I changed the "Shutdown, 0" command to a "MsgBox Shutdown reached", and the 60000 to 5000, for testing purposes. It displays "Shutdown reached" when either clicking OK or waiting 5 seconds; it exits when clicking Cancel.

So, the only think that I can think of is that the Shutdown command is not working, although there's nothing wrong with it, i.e., your syntax is correct. To start the troubleshooting, I suggest replacing the Shutdown command with a MsgBox to see if you get there.

As a side comment, you may find the AutoHotkey reboot script in this EE article to be interesting:

How to solve the problem of incorrect System Uptime being reported when a system has been up for a long time (approximately 50 days or more)

Regards, Joe
1
 
H. G.Author Commented:
The machine it didn't work on is an older Lenovo T420 but it worked on a Lenovo X240.  The 1st time it ran, it took about 5 minutes for me to actually see a log in screen the blue screen that says "logging off" with the spinning circle is what I saw.  I was happy it executed but was unsure if/when I was actually going to be logged off and presented a login in screen.  But finally it did.  I logged in and ran it again and it worked like a charm.

Any ideas why it didn't work on 420?  We have a few of those being used so I'd like to get it to run on them...perhaps I'll test on a different 420 and report back.
0
 
H. G.Author Commented:
The T420's aren't behaving.  The process is running but they aren't logging off.
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
Joe Winograd, Fellow&MVEDeveloperCommented:
Out of my office and on my phone, so can't give thorough response now. Will be back in a couple of hours. In the meantime, let me know what happens when you change the Shutdown to a MsgBox.
0
 
H. G.Author Commented:
The msgbox displays
0
 
Joe Winograd, Fellow&MVEDeveloperCommented:
The msgbox displays
That's what I expected. I think that the problem has nothing to do with the logic in your AutoHotkey script, which is solid. The issue, imo, is that the Shutdown command is not working, and I'm sure it has nothing to do with the fact that AutoHotkey is issuing it. You can prove that by issuing the Shutdown in a batch (.BAT) file.

I'm not an expert in the Shutdown command, but I do have three suggestions:

(1) Include the Force option, which means that the parameter in your AHK script should be 4 instead of 0 (documented in my system uptime article).

(2) Run the script elevated (i.e., Run as administrator).

(3) Google "shutdown command not working" — you'll get lots to study.

If none of that results in a solution, I suggest closing this question and posting a new one with a subject along the lines of "Shutdown command not working" (and different Topics, too). Since the subject of this one is about an AHK script not working (and two of the three Topics are Scripting Languages and AutoHotkey), it attracted folks with expertise in AHK and other scripting languages. I think you need to get different experts involved, and a new question with a different subject and different Topics would be the way to do it. Regards, Joe
0
 
Joe Winograd, Fellow&MVEDeveloperCommented:
I tested the script thoroughly...works perfectly here. The suggestions given in my posts should work for the asker.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.