简体   繁体   中英

How can I stop a user from logging out or locking a workstation using Powershell based on the response to a MessageBox?

I am trying to create a Powershell script that would pop up a message box whenever a user logs in, logs out, shuts down, or locks their workstation. Based on the response, I want to allow the users action to continue, or interrupt the users action. This is an example of what I have so far:

$msgBoxInput = [System.Windows.MessageBox]::Show('Have you clocked in/out?','Reminder','YesNo','Error')

switch  ($msgBoxInput) {

'Yes' {

## Continue with user action (ie. lock computer, logout, shutdown)

}

'No' {

## Stop user action (ie. lock computer, logout, shutdown, but not login action)

## Open clock in/out web page
[Diagnostics.Process]::Start('chrome.exe', 'https://url.to.web page')

}

}

Thanks in advance for any help you can provide!

You have to trap for each event type, interrupt it and have a scheduled task to fire your script actions or use a WMI event watcher for similar effort.

See this discussion on the similar topic, it's of course talking shutdowns, but the same type of approach is needed for the two you are after.

PowerShell window preventing shutdown

...it looks like you can set up a Event watcher using Register-ObjectEvent using [microsoft.win32.systemevents] and watching for either SessionEnding or SessionEnded. I haven't had time to test this out, but you could look at using something like this: $sysevent = [microsoft.win32.systemevents] Register-ObjectEvent -InputObject $sysevent -EventName "SessionEnding" -Action {"Shutdown/Logoff detected.";Get-Process -Name Powershell | Stop-Process -Force}

https://serverfault.com/questions/379830/powershell-window-preventing-shutdown

Remember, one can just hard shut down the system just by hitting the power button or pulling the cord. The is no software approach to stop that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM