Try four at this post, the other three vaporized....
-----
I would like to run a script when my pc is locked and unlocked. Not finding lock/unlock events, I decided to create my own locking mechanism that could be called with a Start Menu shortcut with a keyboard combination.
My first attempt (listed below) calls a function when started, locks the workstation with a RunDll call, waits while the screen saver process is active (borrowed from a scripting guys article), and when the machine is unlocked calls the Unlocked function. The problem is that our network policy has screen savers disabled, so I need another way to detect when the machine is locked. I looked around and learned that when "locked", windows actually switches the desktop to the Winlogon desktop (1). It seems like I should be able to make a call to User32.dll (2) to determin which desktop is active. But I just do not know enough about it to put the pieces of this puzzle together.
Think you could help me out?
Thanks
(1) http://www.codeproject.com/KB/system/RemoteUnlock.aspx
(2) http://waynes-world-it.blogspot.com/2008/04/unlocking-xp2003-without-passwords.html
function Locked {
# Do stuff before locking the workstation
}
function Unlocked {
# Do stuff after unlocking the workstation
}
function WaitForUnlock {
$ScreenSaverPath = (get-item "HKCU:\control panel\desktop\").GetValue("scrnsave.exe")
while ([boolean] (get-process | where-object {$_.Path -like $ScreenSaverPath})) {
Sleep .5
}
Read-Host "Until I find a better way to detect when the system is unlocked Hit Enter to Continue"
}
"{0:G} Locked" -f $([datetime]::Now)
. Locked
rundll32.exe "user32.dll,LockWorkStation"
WaitForUnlock
. Unlocked
"{0:G} Unlocked" -f $([datetime]::Now)