Share via


Controlling the mouse on a virtual machine

Continuing on my series of how to automate virtual machines - you can also script the mouse on a virtual machine.  Take a look at this chunk of code:

$VMName = "Windows 10 Enterprise"
$VMCS = Get-WmiObject -Namespace root\virtualization\v2 -Class Msvm_ComputerSystem -Filter "ElementName='$($VMName)'"
$mouse = $VMCS.GetRelated("Msvm_SyntheticMouse")
$mouse.SetAbsolutePosition(300,450) | out-null
$mouse.ClickButton(2) | out-null

What this PowerShell code does is:

  • Get the WMI object for the virtual machine
  • Get the mouse for the virtual machine
  • Set the mouse position to 300,450
  • Click the second mouse button

Interesting piece of information here: Hyper-V emulates a mouse with 5 buttons.

Cheers,
Ben

Comments

  • Anonymous
    March 14, 2016
    Very interesting. I suppose this can be used in System Center Orchestrator 2012 R2?
    • Anonymous
      March 15, 2016
      To be honest - I do not know how you would make that work