Send Keystrokes/Text to a VM through the Host OS

Hey,

It is possible to send key strokes and text to a Hyper V Virtual Machine using WMI on the host operating system. This can be handy when programming for an off network VM or if the VM loses network connectivity for any reason.

Here’s the code to do it, just change “MY_VM_NAME” to the name of the VM you want to play with.

More methods to invoke in the MSVM_Keyboard class can be found here: https://msdn.microsoft.com/en-us/library/hh850165(v=vs.85).aspx

A useful method of the MSVM_Keyboard class is the “TypeCtrlAltDel” method, which as the name implies, sends the Control Alt Delete keys to the VM. Code to accomplish this below:

A list of key codes to use, if you need to send key strokes themselves, can be found here: https://www.indigorose.com/webhelp/ams/Program_Reference/Misc/Virtual_Key_Codes.htm

Happy Scripting!

 

6 thoughts on “Send Keystrokes/Text to a VM through the Host OS

    1. Administrator Post author

      Hi Paul,

      Yes there is, all you need to do is change:
      “$Keyboard.InvokeMethod(“TypeKey”,”13″)” or “$Keyboard.InvokeMethod(“TypeText”,”Hello world!”)”

      to:

      “$Keyboard.TypeCtrlAltDel()”

      I’ll update the post to contain this info!

      Reply
  1. Paul

    Fantastic! I have been searching for this awhile . I have been taking snapshots of my VM’s and emailing them to myself, but just because you see a server screen with the logon does not mean it is not locked up. By sending a control alt delete I now get the sign on screen.

    I have experienced that when a hyper V locks up, you still see the sign on screen so this was invaluable and the only article I could find about sending keys from the host into the VM.

    Now if you could only specify any other combinations that are available as I tried to search WMI invoke keyboard and I only get the simple single tables – not a table to tell me what is available with the Keyboard.xxxx
    Thanks for your help.

    Reply
    1. Administrator Post author

      Hi Paul,

      You can obtain the methods of the keyboard class, I.e Keyboard.XXXX, by running:
      $Keyboard | Get-Member -MemberType Method

      After running this we can see that there is a method called “TypeScanCodes” this method allows us to send key combinations to the Guest OS. Each key has a “Scan Code”, a list of which can be found here:
      https://msdn.microsoft.com/en-us/library/aa299374(v=vs.60).aspx

      All we need to do is look up the decimal Scan Codes using the above lookup table, and we can send any key combination to the Hyper-V machine. For example to send the Alt+F4 key combination, we look up the decimal scan codes for the Left Alt key and the F4 Key…Left Alt = 56, F4 = 62.

      We then create a byte array in PowerShell with the decimal scan codes we looked up:
      $Codes = @([byte]56,[byte]62)

      Then we execute the method:
      $Keyboard.TypeScanCodes($Codes)

      Another example is sending the Escape key:
      $Codes = @([byte]1)
      $Keyboard.TypeScanCodes($Codes)

      Alternatively you could skip creating the array and send the arguments all in one line like so:
      $Keyboard.TypeScanCodes(@([byte]56,[byte]62))
      $Keyboard.TypeScanCodes(@([byte]1))

      I might write a post on this as it’s interesting stuff, hope it helps you out.

      Reply
  2. Paul

    That is exactly what I was wondering. Great. I appreciate your sharing your knowledge.
    I could not understand how to do function keys or alternate keys.

    As before thank you.

    Reply
  3. Pingback: Send Key combinations to a VM through the Host OS with WMI

Leave a Reply

Your email address will not be published. Required fields are marked *