PowerShell: Executing PowerShell on Computer Lock

PowerShell can respond to system events such as locking or unlocking a session. Here is a fun sample. Provided you have your sound card turned on, your computer will say good-bye when you press WIN+L and welcome you back when you log on again:

function Start-Fun {
  $null = Register-ObjectEvent -InputObject ([Microsoft.Win32.SystemEvents]) -EventName "SessionSwitch" -Action {
    Add-Type -AssemblyName System.Speech 
    $synthesizer = New-Object -TypeName System.Speech.Synthesis.SpeechSynthesizer
       switch($event.SourceEventArgs.Reason) {
    'SessionLock'    { $synthesizer.Speak("See you later $env:username!") }
    'SessionUnlock'  { $synthesizer.Speak("Hey, welcome back $env:username!") }
    }
  }
}
function End-Fun {
    $events = Get-EventSubscriber | Where-Object { $_.SourceObject -eq [Microsoft.Win32.SystemEvents] }
    $jobs = $events | Select-Object -ExpandProperty Action
    $events | Unregister-Event
    $jobs | Remove-Job
}

One Response to “PowerShell: Executing PowerShell on Computer Lock”

  1. Aidan June 26, 2012 at 11:09 pm #

    Over the years I have had considerable eneirpexce with this old hack – not time to learn new tricks. This has been much of the issue behind the success of Windows in IT. The GUI has made management of the IT infrastructure much easier. The cost has been that today’s Administrators have become spoiled because they believe there is no learning with a GUI tool. Not true! The GUI is no different than CLI tools but does force tool designers to approach the layout of a tool in a much different way. By forcing all things through this much narrower and more defined interface we have been able to reuse past lessons in tool navigation.With CLI tools we have no visual interface to display navigation and usage clues so some knowledge it required. In the past this baseline knowledge was normal and attainable by most Admins. Look at Unix. How many shells does the average Unix system support. It used to be normal for Admins to learn all the Unix shells as products would be delivered with support scripts in any of the normal shells. Today Admins rely on the GUI the same as they relied on the older Unix shells. Newer “shells” in Windows seem like a return to the past and do not fit nicely into the modern Admin’s world. This is to be expected and, I suspect, is the normal evolution of the environment.PowerShell is new now and, as such, it will be looked at skeptically by IT Managers and Admins for some time. Mostly PowerShell will be used by vendors and developers to simplify deployment of application management tools. We already have seen one of the first of these in “PowerGUI” which provides a GUI platform for the deployment of PowerShell scripts. Maybe the modern Admin will never need to learn Windows Internals or PowerShell in the future. Most newer Admins that I have worked with have less knowledge about the technical aspects of computing and more knowledge about company policy and user satisfaction. This has been the drive of IT management since they are rated and funded based on these major TQM metrics. The IT effort has been shifted and elevated. The computer and the software along with remote vendor support has taken the technology out of Information Technology. It would be better to call it Information Technology Support. Further advances in computer automation will undoubtedly remove all need for technical expertise at the corporate level as the network is moved farther into the cloud.

Leave a Reply

revia in alcoholism