sharpharp Posted March 2, 2010 Posted March 2, 2010 Hi there, Is there anyway of running a script when a PC is logged off. I do not want to use GPEDIT, but an automated way of running a script when the user logs off or shuts down. Is it possible to use autoit to either: 1) edit the registry to run a vbscript on logoff? 2) run an autoit script at startup that stays in the background and monitors for a windows logoff / shutdown and then runs a task? Any help would be appreciated. Sharpy
Bert Posted March 2, 2010 Posted March 2, 2010 Take a look at bigfix.com. They have software you can use that will do the job. The Vollatran project My blog: http://www.vollysinterestingshit.com/
sharpharp Posted March 2, 2010 Author Posted March 2, 2010 Can you be a bit more specific, i.e. a link to this software you talk off? I searched on their website, and not find anything.
sharpharp Posted March 3, 2010 Author Posted March 3, 2010 PCs are not in domain, and having to go to each PC manually to run gpedit and add the script to logoff isn't a good idea. So i want a way of running a script when a PC logs off or shuts down (automated not manually via GPEDIT) There must be a way to do it with AutoIT, anyone?
Yashied Posted March 3, 2010 Posted March 3, 2010 #703101 My UDFs: iKey | FTP Uploader | Battery Checker | Boot Manager | Font Viewer | UDF Keyword Manager | Run Dialog Replacement | USBProtect | 3D Axis | Calculator | Sleep | iSwitcher | TM | NetHelper | File Types Manager | Control Viewer | SynFolders | DLL Helper Animated Tray Icons UDF Library | Hotkeys UDF Library | Hotkeys Input Control UDF Library | Caret Shape UDF Library | Context Help UDF Library | Most Recently Used List UDF Library | Icons UDF Library | FTP UDF Library | Script Communications UDF Library | Color Chooser UDF Library | Color Picker Control UDF Library | IPHelper (Vista/7) UDF Library | WinAPI Extended UDF Library | WinAPIVhd UDF Library | Icon Chooser UDF Library | Copy UDF Library | Restart UDF Library | Event Log UDF Library | NotifyBox UDF Library | Pop-up Windows UDF Library | TVExplorer UDF Library | GuiHotKey UDF Library | GuiSysLink UDF Library | Package UDF Library | Skin UDF Library | AITray UDF Library | RDC UDF Library Appropriate path | Button text color | Gaussian random numbers | Header's styles (Vista/7) | ICON resource enumeration | Menu & INI | Tabbed string size | Tab's skin | Pop-up circular menu | Progress Bar without animation (Vista/7) | Registry export | Registry path jumping | Unique hardware ID | Windows alignment More...
sharpharp Posted March 4, 2010 Author Posted March 4, 2010 #Include <HotKey.au3> Global Const $VK_ESCAPE = 0x1B Global Const $VK_L = 0x4C _HotKeyAssign(BitOR($CK_WIN, $VK_L), 'MyFunc') _HotKeyAssign(BitOR($CK_CONTROL, $VK_ESCAPE), 'Quit') While 1 Sleep(100) WEnd Func MyFunc() MsgBox(0, '', 'WIN-L has been pressed!') DllCall('user32.dll', 'int', 'LockWorkStation') EndFunc ;==>MyFunc Func Quit() Exit EndFunc ;==>Quit#703101 I had a look at this code, and it works well with WIN+L and shows a message box. Being a poor coder, can anyone help change this code, so that it pops up the box when someone attempts to LOGOFF or SHUTDOWN? Many thanks in advance.
Yashied Posted March 4, 2010 Posted March 4, 2010 I gave you the link... Global Const $WM_QUERYENDSESSION = 0x0011 GUICreate('') GUIRegisterMsg($WM_QUERYENDSESSION, 'WM_QUERYENDSESSION') While 1 Sleep(1000) WEnd Func WM_QUERYENDSESSION($hWnd, $iMsg, $wParam, $lParam) ; Do something SoundPlay(@WindowsDir & '\media\tada.wav', 1) Return 'GUI_RUNDEFMSG' EndFunc ;==>WM_QUERYENDSESSION My UDFs: iKey | FTP Uploader | Battery Checker | Boot Manager | Font Viewer | UDF Keyword Manager | Run Dialog Replacement | USBProtect | 3D Axis | Calculator | Sleep | iSwitcher | TM | NetHelper | File Types Manager | Control Viewer | SynFolders | DLL Helper Animated Tray Icons UDF Library | Hotkeys UDF Library | Hotkeys Input Control UDF Library | Caret Shape UDF Library | Context Help UDF Library | Most Recently Used List UDF Library | Icons UDF Library | FTP UDF Library | Script Communications UDF Library | Color Chooser UDF Library | Color Picker Control UDF Library | IPHelper (Vista/7) UDF Library | WinAPI Extended UDF Library | WinAPIVhd UDF Library | Icon Chooser UDF Library | Copy UDF Library | Restart UDF Library | Event Log UDF Library | NotifyBox UDF Library | Pop-up Windows UDF Library | TVExplorer UDF Library | GuiHotKey UDF Library | GuiSysLink UDF Library | Package UDF Library | Skin UDF Library | AITray UDF Library | RDC UDF Library Appropriate path | Button text color | Gaussian random numbers | Header's styles (Vista/7) | ICON resource enumeration | Menu & INI | Tabbed string size | Tab's skin | Pop-up circular menu | Progress Bar without animation (Vista/7) | Registry export | Registry path jumping | Unique hardware ID | Windows alignment More...
sharpharp Posted March 4, 2010 Author Posted March 4, 2010 (edited) Global Const $WM_QUERYENDSESSION = 0x0011 GUICreate('') GUIRegisterMsg($WM_QUERYENDSESSION, 'WM_QUERYENDSESSION') While 1 Sleep(1000) WEnd Func WM_QUERYENDSESSION($hWnd, $iMsg, $wParam, $lParam) ; Do something run("c:\windows\notepad.exe") SoundPlay(@WindowsDir & '\media\tada.wav', 1) Return 'GUI_RUNDEFMSG' EndFunc ;==>WM_QUERYENDSESSION Ok, I just want to start with something basic like running notepad when the PC logs off or shuts down. I've tried your script, and added the line above to run notepad and nothing happens. Windows logs off as normal and notepad does not run. Please be patient, i'm no expert, just trying to figure this out, any help is appreciated. Thanks, Sharpy Edited March 4, 2010 by sharpharp
Bert Posted March 4, 2010 Posted March 4, 2010 Can you explain what you want to run at shutdown? That may be helpful. Saying "I want to run a program at shutdown" doesn't help. Saying "I want to run "Insert name of app here" at shutdown" would help. The Vollatran project My blog: http://www.vollysinterestingshit.com/
sharpharp Posted March 4, 2010 Author Posted March 4, 2010 (edited) Can you explain what you want to run at shutdown? That may be helpful. Saying "I want to run a program at shutdown" doesn't help. Saying "I want to run "Insert name of app here" at shutdown" would help.Ok, I have a VBS script which checks if a CD has been left in the tray, so I would like AutoIT to be able to run my vbscript when a User logs off a PC or shuts down.I want to achieve the same thing Local Group Policy - GPEDIT, User Config, Windows Settings, Script, Logoff would achieve (but without having to actually manually do this). Ideally I want to create an installer package which runs on a standalone pc that installs my vbscript in the logoff and shutdown (same as GPEDIT can do), so that my vbscript runs when a users logs off or shuts down.So thats the story behind it, I've tried to script editing of the registry to emulate the keys altered by doing a manual GPEDIT, but nothing works, unless you manually do GPEDIT, add logoff script. Believe me, I've tried lots of methods to no avail.So I thought the only way to do this was to somehow run an AutoIT script at startup, have it run in the background, then when a user logs off, or shuts down, the AutoIT script would send an onautoitexit trigger to run my vbscript to check if a CD is left in tray before carring on with the log off/shutdown process. Sorry to go on, but you did ask what i am trying to achieve.Thanks,Sharpy Edited March 4, 2010 by sharpharp
sharpharp Posted March 8, 2010 Author Posted March 8, 2010 So volly or anyone, any ideas? Am wondering if possible to monitor for a logoff and then run a scipt?
zorphnog Posted March 8, 2010 Posted March 8, 2010 You should be able to add the shutdown script to the GPO via registry and file modification. I did this and it worked for me:Create your GPO shutdown script manually in GPEdit.msc (Computer Configuration->Windows Settings->Scripts)Export the registry key: HKLM\Software\Policies\Microsoft\Windows\System\Scripts\ShutdownSave a copy of the file: C:\WINDOWS\System32\GroupPolicy\Machine\Scripts\scripts.ini (hidden folder/file)Manually remove your shutdown script from the GPO via GPEdit.mscImport the registry file saved in step #2Copy the saved scripts.ini from step #3 to C:\WINDOWS\System32\GroupPolicy\Machine\Scripts\scripts.iniOpen your GPEdit shutdown script manager and the entry should be there. Now that you know the basic steps you can easily convert this into a script that will automatically add the shutdown script to the local group policy.
gkrapp Posted March 8, 2010 Posted March 8, 2010 Hi, why do you not use the task scheduler with "On disconnect from user session" as the trigger?
sharpharp Posted March 8, 2010 Author Posted March 8, 2010 (edited) You should be able to add the shutdown script to the GPO via registry and file modification. I did this and it worked for me:Create your GPO shutdown script manually in GPEdit.msc (Computer Configuration->Windows Settings->Scripts)Export the registry key: HKLM\Software\Policies\Microsoft\Windows\System\Scripts\ShutdownSave a copy of the file: C:\WINDOWS\System32\GroupPolicy\Machine\Scripts\scripts.ini (hidden folder/file)Manually remove your shutdown script from the GPO via GPEdit.mscImport the registry file saved in step #2Copy the saved scripts.ini from step #3 to C:\WINDOWS\System32\GroupPolicy\Machine\Scripts\scripts.iniOpen your GPEdit shutdown script manager and the entry should be there. Now that you know the basic steps you can easily convert this into a script that will automatically add the shutdown script to the local group policy.I've added my script manually via GPEDIT, computer config, windows settings, scripts, shutdownBut when i got to registry, there is nothing under HKLM\Software\Policies\Microsoft\Windows\System\There is no Shutdown key... (I am using Windows 7)I have browsed the registry under HKLM and found my script under 2 different locations:1) HKLM\software\microsoft\windows\currentversion\group policy\scripts\shutdown\0\02) HKLM\software\microsoft\windows\currentversion\group policy\state\machine\scripts\shutdown\0\0I exported both keys, copied off the scripts.ini, removed script from gpedit, then imported both registry keys, overwrote the scripts.ini with the one i copied off.Checked in gpedit, and the script is there under shutdown.I then attempt shutdown, and the PC shuts down without running my script... Edited March 8, 2010 by sharpharp
zorphnog Posted March 9, 2010 Posted March 9, 2010 I'm using XP and I don't have a Win7 machine at the moment so I can't test it. The steps I provided do work for XP with the exception of also exporting the HKLM\Software\Microsoft\Windows\CurrentVersion\GroupPolicy\State\Machine\Scripts\Shutdown key. The only thing that I can see that might be an issue is if you are exporting the ..\shutdown\0\0 key instead of the ..\shutdown key. From your description it sounds like you are exporting the ..\shutdown\0\0, so I would try exporting the ..\shutdown key since there are some values in the ..\shutdown\0 key that need to be copied as well.
sharpharp Posted March 9, 2010 Author Posted March 9, 2010 (edited) I'm using XP and I don't have a Win7 machine at the moment so I can't test it. The steps I provided do work for XP with the exception of also exporting the HKLM\Software\Microsoft\Windows\CurrentVersion\GroupPolicy\State\Machine\Scripts\Shutdown key.The only thing that I can see that might be an issue is if you are exporting the ..\shutdown\0\0 key instead of the ..\shutdown key. From your description it sounds like you are exporting the ..\shutdown\0\0, so I would try exporting the ..\shutdown key since there are some values in the ..\shutdown\0 key that need to be copied as well.I've done further testing: This time I manually added by script via gpedit.msc, user config, windows settings, scripts, logoff -> added my vbs script. When logging off, perfect my script works.So I then exported the "whole" of my registry, and copied off the scripts.ini from c:\windows\system32\group policy\user\scriptsWent back into gpedit, removed my script. Then, I ovewrite the scripts.ini with the one i copied over, and re-import the entire registry.Gpedit shows my script is there, and when logging off, my vbs script does runs...!!!!SO, when i'm importing the ENTIRE registry, something else is being written too which is not one of the keys mentioned.I've tried comparing before and after with windiff, but that doesn't make any sense.Gotta get to the bottom of this... Edited March 9, 2010 by sharpharp
BlueLord Posted July 4, 2010 Posted July 4, 2010 I gave you the link... Global Const $WM_QUERYENDSESSION = 0x0011 GUICreate('') GUIRegisterMsg($WM_QUERYENDSESSION, 'WM_QUERYENDSESSION') While 1 Sleep(1000) WEnd Func WM_QUERYENDSESSION($hWnd, $iMsg, $wParam, $lParam) ; Do something SoundPlay(@WindowsDir & '\media\tada.wav', 1) Return 'GUI_RUNDEFMSG' EndFunc ;==>WM_QUERYENDSESSION Hy, I'm new in this language program. I find this post and i't useful for me. When i trie to run a .cmd file at logoff/shutdown i have this error Can somebody help me?
LJB001 Posted August 22, 2010 Posted August 22, 2010 After a bit of hacking, here is a way to run a program using AutoIT at shutdown. Launch this script with 1 to 5 parameters per the spec for ShellExecuteWait.$WM_QUERYENDSESSION = 0x0011GUIRegisterMsg($WM_QUERYENDSESSION, "Cancel_Shutdown")GUICreate("ShellExecuteAtShutdown")GUISetSTate(@SW_HIDE)Global $b_ShutdownInitiated = FalseWhile $b_ShutdownInitiated = FalseWEndSwitch $CmdLine[0] Case 5 ShellExecuteWait ($CmdLine[1], $CmdLine[2], $CmdLine[3], $CmdLine[4], $CmdLine[5]) Case 4 ShellExecuteWait ($CmdLine[1], $CmdLine[2], $CmdLine[3], $CmdLine[4]) Case 3 ShellExecuteWait ($CmdLine[1], $CmdLine[2], $CmdLine[3]) Case 2 ShellExecuteWait ($CmdLine[1], $CmdLine[2]) Case 1 ShellExecuteWait ($CmdLine[1])EndSwitchSwitch RegRead("HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer", "Shutdown Setting") Case 1 ShutDown (0) Case 4 ShutDown (2) Case Else ShutDown (1)EndSwitchFunc Cancel_Shutdown($hWndGUI, $MsgID, $WParam, $LParam) $b_ShutdownInitiated = True Return FalseEndFunc
LJB001 Posted August 22, 2010 Posted August 22, 2010 After a bit of hacking, here is a way to run a program using AutoIT at shutdown. Launch this script with 1 to 5 parameters per the spec for ShellExecuteWait. Also, best to insert a "Sleep (1000)" into the While/WEnd. Although the lack of it didn't seem to affect my PC's performance, I noticed that TaskManager reported my CPU was at 100% utilizatin with 99% in this process. After inserting the "Sleep (1000)" the CPU utilizatin dropped back to less than 1% without affecting performance of this process..... $WM_QUERYENDSESSION = 0x0011 GUIRegisterMsg($WM_QUERYENDSESSION, "Cancel_Shutdown") GUICreate("ShellExecuteAtShutdown") GUISetSTate(@SW_HIDE) Global $b_ShutdownInitiated = False While $b_ShutdownInitiated = False Sleep (1000) WEnd Switch $CmdLine[0] Case 5 ShellExecuteWait ($CmdLine[1], $CmdLine[2], $CmdLine[3], $CmdLine[4], $CmdLine[5]) Case 4 ShellExecuteWait ($CmdLine[1], $CmdLine[2], $CmdLine[3], $CmdLine[4]) Case 3 ShellExecuteWait ($CmdLine[1], $CmdLine[2], $CmdLine[3]) Case 2 ShellExecuteWait ($CmdLine[1], $CmdLine[2]) Case 1 ShellExecuteWait ($CmdLine[1]) EndSwitch Switch RegRead("HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer", "Shutdown Setting") Case 1 ShutDown (0) Case 4 ShutDown (2) Case Else ShutDown (1) EndSwitch Func Cancel_Shutdown($hWndGUI, $MsgID, $WParam, $LParam) $b_ShutdownInitiated = True Return False EndFunc
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