1

I have this script to block the keyboard that I want to run on all domain users who logon through schedule tasks while another script checks updates:

$code = @'
    [DllImport("user32.dll")]
    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)

I have set the scheduled task to run as system so that it will block the keyboard. The schedule task runs fine, I have tested it with set-content but the keyboard is not blocked at all. The script runs as expected if manually run.

Is this because it is an interactive script? I have tried with different flags ie -noprofile, -noexit, -nonI but does not help.

Any advice?

nicetohelp
  • 11
  • 1
  • 1
    If you configure your scheduled task to run in the context of the system account (`NT AUTHORITY\SYSTEM`), it will run in a different, hidden session that is separate from the session for an interactive user. – mklement0 Sep 17 '19 at 07:09
  • Thanks mklement0. That does make sense. Do you know how I can run it for each user in an interactive session as an administrator? – nicetohelp Sep 18 '19 at 03:17
  • Would using runas work using an administrator account and credentials as a lot of the users are not part of any administrative group or is there a better way I a missing? – nicetohelp Sep 18 '19 at 03:30
  • You could try [`psexec`](https://learn.microsoft.com/en-us/sysinternals/downloads/psexec) (must be installed), as shown in [this answer](https://stackoverflow.com/a/57982729/45375), but I don't know if that will work from a scheduled task. – mklement0 Sep 18 '19 at 03:36

0 Answers0