Here is a PowerShell function called Lock-Screen that can lock the screen and prohibit user interaction. There can be a custom message, and the screen...
Here is a PowerShell function called Lock-Screen that can lock the screen and prohibit user interaction. There can be a custom message, and the screen can be dimmed during lockout.
Here is an example call:
PS> Lock-Screen-LockSeconds4-DimScreen-Title'Go away and come back in {0} seconds.'
Source Code to Use Lock-Screen
And this is the source code to run in order to use Lock-Screen:
FunctionLock-Screen
{
[CmdletBinding()]
param
(
# number of seconds to lock
[int]
$LockSeconds=10,# message shown. Use {0} to insert remaining seconds
# do not use {0} for a static message
[string]
$Title='wait for {0} more seconds...',# dim screen
[Switch]
$DimScreen
)
# when run without administrator privileges, the keyboard will not be blocked!
# get access to API functions that block user input
# blocking of keyboard input requires admin privileges
$code=@'
[DllImport("user32.dll")]
public static extern int ShowCursor(bool bShow);
[DllImport("user32.dll")]
public static extern bool BlockInput(bool fBlockIt);
'@$userInput=Add-Type-MemberDefinition$code-NameBlocker-NamespaceUserInput-PassThru# get access to UI functionality
Add-Type-AssemblyNamePresentationFrameworkAdd-Type-AssemblyNamePresentationCore# set window opacity
$opacity=1if ($DimScreen) { $opacity=200 }
# create a message label
$label=New-Object-TypeNameWindows.Controls.Label$label.FontSize=60$label.FontFamily='Consolas'$label.FontWeight='Bold'$label.Background='Transparent'$label.Foreground='Blue'$label.VerticalAlignment='Center'$label.HorizontalAlignment='Center'# create a window
$window=New-Object-TypeNameWindows.Window$window.WindowStyle='None'$window.AllowsTransparency=$true$color= [Windows.Media.Color]::FromArgb($opacity,0,0,0)
$window.Background= [Windows.Media.SolidColorBrush]::new($color)
$window.Opacity=0.8$window.Left=$window.Top=0$window.WindowState='Maximized'$window.Topmost=$true$window.Content=$label# block user input
$null=$userInput::BlockInput($true)
$null=$userInput::ShowCursor($false)
# show window and display message
$null=$window.Dispatcher.Invoke{
$window.Show()
$LockSeconds..1|ForEach-Object {
$label.Content= ($title-f$_)
$label.Dispatcher.Invoke([Action]{},'Background')
Start-Sleep-Seconds1
}
$window.Close()
}
# unblock user input
$null=$userInput::ShowCursor($true)
$null=$userInput::BlockInput($false)
}
Administrator Privileges Required for Full Lock Functionality
Note that Lock-Screen needs to be run with Administrator privileges in order to completely lock user input.
psconf.eu – PowerShell Conference EU 2019 – June 4-7, Hannover Germany – visit www.psconf.eu There aren’t too many trainings around for experienced PowerShell scripters where you really still learn something new. But there’s one place you don’t want to miss: PowerShell Conference EU – with 40 renown international speakers including PowerShell team members and MVPs, plus 350 professional and creative PowerShell scripters. Registration is open at www.psconf.eu, and the full 3-track 4-days agenda becomes available soon. Once a year it’s just a smart move to come together, update know-how, learn about security and mitigations, and bring home fresh ideas and authoritative guidance. We’d sure love to see and hear from you!