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:

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

Shutdown, 0
H. G.Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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.
H. G.Author Commented:
The T420's aren't behaving.  The process is running but they aren't logging off.
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook 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.
H. G.Author Commented:
The msgbox displays
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
Joe Winograd, Fellow&MVEDeveloperCommented:
I tested the script perfectly here. The suggestions given in my posts should work for the asker.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Scripting Languages

From novice to tech pro — start learning today.