I am trying to execute a script when the user locks the computer. This is how my script looks like:
$OnLock =
{
Write-Host -ForeGround Green "System Locked"
}
$sysevent = [microsoft.win32.systemevents]
Register-ObjectEvent -InputObject $sysevent -EventName "SessionSwitch" -Action {$OnLock} -SourceIdentifier "ExecuteOnLock"
The problem is that it does not print anything on the console window but if i write the code in the Action switch, it works fine.
Register-ObjectEvent -InputObject $sysevent -EventName "SessionSwitch" -SourceIdentifier "ExecuteOnLock" -Action {Write-Host -ForeGround Green "System Locked"}
Is there something i am missing while calling $OnLock
script block?