Jump to content

Run script at Logoff (without Group Policy)


Recommended Posts

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

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

#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

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.

Link to comment
Share on other sites

I gave you the link...

:mellow:

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
Link to comment
Share on other sites

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 by sharpharp
Link to comment
Share on other sites

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 by sharpharp
Link to comment
Share on other sites

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\Shutdown
  • Save 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.msc
  • Import the registry file saved in step #2
  • Copy the saved scripts.ini from step #3 to C:\WINDOWS\System32\GroupPolicy\Machine\Scripts\scripts.ini
Open 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.
Link to comment
Share on other sites

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\Shutdown
  • Save 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.msc
  • Import the registry file saved in step #2
  • Copy the saved scripts.ini from step #3 to C:\WINDOWS\System32\GroupPolicy\Machine\Scripts\scripts.ini
Open 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, shutdown

But 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\0

2) HKLM\software\microsoft\windows\currentversion\group policy\state\machine\scripts\shutdown\0\0

I 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 by sharpharp
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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\scripts

Went 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 by sharpharp
Link to comment
Share on other sites

  • 3 months later...

I gave you the link...

:blink:

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?

post-58816-12782711973394_thumb.jpg

Link to comment
Share on other sites

  • 1 month later...

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 = 0x0011

GUIRegisterMsg($WM_QUERYENDSESSION, "Cancel_Shutdown")

GUICreate("ShellExecuteAtShutdown")

GUISetSTate(@SW_HIDE)

Global $b_ShutdownInitiated = False

While $b_ShutdownInitiated = False

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

Link to comment
Share on other sites

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

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...