Send Mouseclick to GUI through PowerShell

Discussion about using the VirtualBox API, Tutorials, Samples.
Post Reply
RandomScrub
Posts: 1
Joined: 4. Aug 2024, 22:08

Send Mouseclick to GUI through PowerShell

Post by RandomScrub »

I am trying to partially automate some tasks. I have the GUI running and would like to send some mouse clicks to it through PowerShell while still being able to manually intervene if necessary. I know for keys I can use:

Code: Select all

vboxmanage controlvm "vmname" keyboardputscancode <hex>
Unfortunately, this is not possible for mouse clicks. So I tried to directly use the API:

Code: Select all

$vBox = New-Object -ComObject VirtualBox.VirtualBox
$MACHINE = $vBox.FindMachine("Windows10")
$SESSION_ROOT = New-Object -ComObject VirtualBox.Session
$SESSION_GUEST = New-Object -ComObject VirtualBox.Session
$MACHINE.LockMachine($SESSION_ROOT, 1) # Locking the machine in Shared LockType
$SESSION_GUEST.Console.Mouse.putMouseEventAbvsolute(632, 687, 0, 0, 1)
However, this always yields the following error.

Code: Select all

You cannot call a method on a null-valued expression.
At D:\loren\Documents\test.ps1:9 char:1
+ $SESSION_GUEST.Console.Mouse.putMouseEventAbvsolute(632, 687, 0, 0, 1 ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull
According to my understanding, this should work. But apparently it doesn't. Any help to get this to run would be appreciated!
Post Reply