I'm testing a program that listens for key presses on remote machines, so I need to generate them somehow, and preferably only using powershell core.
I'm getting Access Denied (win32 exception or dotnetmethod exception) when I try to use SendKeys on a remote computer via invoke-command, even though credentials are supplied to the script with admin rights and it works fine locally.
I'm running many scripts in this way and this script is the only one thats fails (and it fails the same way on all operating systems)
e.g.
$script =
{
Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.SendKeys]::SendWait($item);
}
invoke-command -computername $machine -scriptblock $script -credential $cred
SendInput looks like it might be a promising replacement but I haven't been able to find an example that works for me.
Turns out the problem is due to not having access to a windows user session.
I solved it by creating a scheduled task on the remote machine and having that call the powershell script. This way the script runs in a user session.
Otherwise it seems to run in a gui-less session which cant call the keyboard and mouse APIs.