How to block input with Powershell

  • Find C# function signature
  • Code review
  • Full script

Powershell has many built-in cmdlets that allows us to automate our environment, but sometimes we need even more.

For that, there’s a powerful cmdlet which we’ve already used here in another article, called “Add-Type” which is capable of adding a .NET class into a powershell session, and not just that, we can even call only a method of a class that Add-Type will define and generate the class.

If you’re not understanding of what I’m talking about, I recommend you to check out a note that I’ve already prepared for better explanation of this context:

Note – APIs & .NET Architecture


Find C# function signature

First we need the C# signature of the BlockInput method to store it into a variable, we can search for that at pinvoke.net, see how in the gif below:

Now we can add the BlockInput method in our Powershell session as a static method of a class that Add-Type creates!

We just need to store it into a variable, that will represent the class and allow us to access it’s methods 🙂

The C# signature must be enclosed by @” “@ to successfuly be stored into the variable:

$signature = @"
  [DllImport("user32.dll")]
  public static extern bool BlockInput(bool fBlockIt);
"@

$block = Add-Type -MemberDefinition $signature -Name DisableInput -Namespace DisableInput -PassThru

Code review

Store C# signature of the method into $signature.

MemberDefinition:
Specify the method definition stored in $signature variable.

Name / Namespace:
just specify name and the namespace of the class, which you can choose whatever you want, it’s a mandatory parameter though.

PassThru:
Since Add-Type cmdlet does not return any objects, the variable $block would have nothing inside whitout it.


Full script

We’re are ready to call our method, note that the definition of this method is a bool definition, That means we must specify if it will be true or false:

 public static extern bool BlockInput(bool fBlockIt)


Bottom Line

Cool isn’t it?  but what real scenario this could be useful you ask?

Actually this can be useful in several ways, it depends on your creativity, in my case I used this, combined with a key stroke sequence to interact with a package installation GUI in software center from SCCM, the software was very old and it didn’t have silent switches.

So I needed to block the user input during the key stroke sequence execution to guarantee everything would work just fine!

Anyway, I think all the knowledge required to accomplish this script it’s the real great stuff here, and you can go and explore it by yourself now!

Please, if this article was helpful, share it with your mates! 😉


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.