If a PowerShell script needs to perform critical steps, and user interaction must be prohibited, you can use API calls to temporarily disable all keyboard input. Locking keyboard input does require Administrator privileges.
Here is a script illustrating how to block all keyboard input for 4 seconds:
# 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 administrator privileges
$code = @'
public static extern bool BlockInput(bool fBlockIt);
$userInput = Add-Type -MemberDefinition $code -Name Blocker -Namespace UserInput -PassThru
# block user input
$null = $userInput::BlockInput($true)
Write-Warning "Your input has been disabled for 4 seconds..."
Start-Sleep -Seconds 4
# unblock user input
$null = $userInput::BlockInput($false)
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!
ReTweet this Tip!
Funny, it never gave me control back. The final line never executed. I had to press the power button on my machine and pretend I was shutting the system down to regain input control. I waited 30 seconds before that...