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!
noteirak
Site Moderator
Posts: 5231
Joined: 13. Jan 2012, 11:14
Primary OS: Debian other
VBox Version: OSE Debian
Guest OSses: Debian, Win 2k8, Win 7
Contact:

Re: Send Mouseclick to GUI through PowerShell

Post by noteirak »

You need to use the machine object from the session. Also, the session object must come from the VBox object with the corresponding method. Your issue is that $SESSION_GUEST does not have a console object, since it was not correctly initialisated
Hyperbox - Virtual Infrastructure Manager - https://apps.kamax.lu/hyperbox/
Manage your VirtualBox infrastructure the free way!
Post Reply