CmdLets/Send-KeyToProcess.ps1
|
# Load required assemblies Add-Type -AssemblyName System.Windows.Forms Add-Type -AssemblyName System.Drawing # Add Win32 API functions for window control Add-Type @" using System; using System.Runtime.InteropServices; public class WinAPI { [DllImport("user32.dll")] public static extern bool SetForegroundWindow(IntPtr hWnd); [DllImport("user32.dll")] public static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow); } "@ Function Send-KeyToProcess { [CmdletBinding()] param ( [Parameter(Mandatory=$true)] [string]$ProcessName, [Parameter(Mandatory=$true)] [string]$Key ) <# Supported Keys: Modifier Symbol Example Shift + "+A" → Shift + A Ctrl ^ "^C" → Ctrl + C Alt % "%F4" → Alt + F4 {Enter} {TAB} {ESC} {UP} {DOWN} {LEFT} {RIGHT} F1 to F12 -- Unsure which is correct {F1} to {F12} -- Unsure which is correct #> try { # Get the process $proc = Get-Process -Name $ProcessName -ErrorAction Stop | Select-Object -First 1 if (-not $proc.MainWindowHandle -or $proc.MainWindowHandle -eq 0) { Write-Error "Process '$ProcessName' has no main window." return } # Bring window to front #[WinAPI]::ShowWindowAsync($proc.MainWindowHandle, 5) | Out-Null # 5 = SW_SHOW #Start-Sleep -Milliseconds 200 [WinAPI]::SetForegroundWindow($proc.MainWindowHandle) | Out-Null Start-Sleep -Milliseconds 200 # Send the key [System.Windows.Forms.SendKeys]::SendWait($Key) Write-Host "Sent key '$Key' to process '$ProcessName'." } catch { Write-Error "Error: $_" } } |