https://tmurata.hatenadiary.org/entry/20110217/1297947373
キーボードを押したことを送信するのはSendKeysで楽に行えるがマウスクリックを送信するのはC#のコードを介してWindowsAPIを叩いてやらなければできない。
上URLにもやり方が書いてあるが、こっちは出来るだけシンプルに書いてみた。
Add-Type -Language CSharp -ReferencedAssemblies System.Windows.Forms,System.Drawing -TypeDefinition @' using System.Runtime.InteropServices; public class MouseUtil { [DllImport("user32.dll")] private static extern uint SendInput(uint nInputs, INPUT[] pInputs, int cbSize); private const int MOUSEEVENTF_LEFTDOWN = 0x0002; // 左ボタン Down private const int MOUSEEVENTF_LEFTUP = 0x0004; // 左ボタン Up [StructLayout(LayoutKind.Sequential)] private struct MOUSEINPUT { public int dx; public int dy; public int mouseData; public int dwFlags; public int time; public System.IntPtr dwExtraInfo; } [StructLayout(LayoutKind.Sequential)] private struct INPUT { public int type; public MOUSEINPUT mi; } public static void SendClick(int x, int y) { // カーソル位置を設定 System.Windows.Forms.Cursor.Position = new System.Drawing.Point(x, y); //struct 配列の宣言 INPUT[] input = new INPUT[2]; //左ボタン Down input[0].mi.dwFlags = MOUSEEVENTF_LEFTDOWN; //左ボタン Up input[1].mi.dwFlags = MOUSEEVENTF_LEFTUP; //イベントの一括生成 SendInput(2, input, Marshal.SizeOf(input[0])); } } '@ # 使用例:ペイントブラシを起動して点を描く Start-Process -WindowStyle Maximized pbrush.exe sleep 1 [MouseUtil]::SendClick(50, 200) # 1回目のクリックでウインドウをアクティブにして sleep 1 [MouseUtil]::SendClick(50, 200) # 2回目のクリックで点を描く