Add-Type -AssemblyName System.Windows.Forms Add-Type -AssemblyName System.Drawing # ========================================================== # Win32 Mouse Functions # ========================================================== Add-Type @" using System; using System.Runtime.InteropServices; public static class InputHelper { // ====================================== // Legacy APIs (mouse_event) // ====================================== [DllImport("user32.dll")] public static extern bool SetCursorPos( int X, int Y ); [DllImport("user32.dll")] public static extern void mouse_event( uint dwFlags, uint dx, uint dy, uint dwData, UIntPtr dwExtraInfo ); public const uint LEFTDOWN = 0x0002; public const uint LEFTUP = 0x0004; // ====================================== // SendInput APIs // ====================================== [StructLayout(LayoutKind.Sequential)] public struct INPUT { public UInt32 type; public MOUSEINPUT mi; } [StructLayout(LayoutKind.Sequential)] public struct MOUSEINPUT { public Int32 dx; public Int32 dy; public UInt32 mouseData; public UInt32 dwFlags; public UInt32 time; public IntPtr dwExtraInfo; } [DllImport("user32.dll")] public static extern uint SendInput( uint nInputs, INPUT[] pInputs, int cbSize ); public const uint INPUT_MOUSE = 0; public const uint MOUSEEVENTF_LEFTDOWN = 0x0002; public const uint MOUSEEVENTF_LEFTUP = 0x0004; } "@ # ========================================================== # Mouse Click Function # ========================================================== function Invoke-HybridClick { param( [int]$X, [int]$Y ) # # Move mouse # [InputHelper]::SetCursorPos($X, $Y) | Out-Null Start-Sleep -Milliseconds 100 # # Legacy click # [InputHelper]::mouse_event( [InputHelper]::LEFTDOWN, 0, 0, 0, [System.UIntPtr]::Zero ) Start-Sleep -Milliseconds 25 [InputHelper]::mouse_event( [InputHelper]::LEFTUP, 0, 0, 0, [System.UIntPtr]::Zero ) Start-Sleep -Milliseconds 50 # # SendInput click # $down = New-Object InputHelper+INPUT $down.type = [InputHelper]::INPUT_MOUSE $down.mi.dwFlags = [InputHelper]::MOUSEEVENTF_LEFTDOWN $up = New-Object InputHelper+INPUT $up.type = [InputHelper]::INPUT_MOUSE $up.mi.dwFlags = [InputHelper]::MOUSEEVENTF_LEFTUP $inputs = @( $down, $up ) $inputSize = [System.Runtime.InteropServices.Marshal]::SizeOf($down) [InputHelper]::SendInput( $inputs.Count, $inputs, $inputSize ) | Out-Null Start-Sleep -Milliseconds 200 } # ========================================================== # Capture Mouse Position # ========================================================== function Get-ClickPosition { param( [string]$Prompt ) Write-Host "" Write-Host $Prompt -ForegroundColor Yellow Write-Host "Move the mouse over the control and press ENTER." Read-Host | Out-Null $pos = [System.Windows.Forms.Cursor]::Position return [PSCustomObject]@{ X = $pos.X Y = $pos.Y } } # ========================================================== # Field Selection Dialog # ========================================================== function Show-FieldSelectionDialog { param( [string[]]$FieldNames ) $form = New-Object System.Windows.Forms.Form $form.Text = "Select JSON Fields" $form.Width = 500 $form.Height = 600 $form.StartPosition = "CenterScreen" $label = New-Object System.Windows.Forms.Label $label.Text = "Select the fields to populate:" $label.AutoSize = $true $label.Left = 10 $label.Top = 10 $form.Controls.Add($label) $checkedList = New-Object System.Windows.Forms.CheckedListBox $checkedList.Left = 10 $checkedList.Top = 35 $checkedList.Width = 450 $checkedList.Height = 450 $checkedList.CheckOnClick = $true foreach ($field in $FieldNames) { [void]$checkedList.Items.Add($field, $true) } $form.Controls.Add($checkedList) $okButton = New-Object System.Windows.Forms.Button $okButton.Text = "OK" $okButton.Width = 100 $okButton.Height = 30 $okButton.Left = 120 $okButton.Top = 510 $okButton.DialogResult = [System.Windows.Forms.DialogResult]::OK $form.Controls.Add($okButton) $cancelButton = New-Object System.Windows.Forms.Button $cancelButton.Text = "Cancel" $cancelButton.Width = 100 $cancelButton.Height = 30 $cancelButton.Left = 240 $cancelButton.Top = 510 $cancelButton.DialogResult = [System.Windows.Forms.DialogResult]::Cancel $form.Controls.Add($cancelButton) $form.AcceptButton = $okButton $form.CancelButton = $cancelButton $result = $form.ShowDialog() if ($result -ne [System.Windows.Forms.DialogResult]::OK) { return $null } $selected = @() foreach ($item in $checkedList.CheckedItems) { $selected += [string]$item } return $selected } # ========================================================== # JSON File Selection # ========================================================== $fileDialog = New-Object System.Windows.Forms.OpenFileDialog $fileDialog.Title = "Select JSON File" $fileDialog.Filter = "JSON Files (*.json)|*.json|All Files (*.*)|*.*" if ($fileDialog.ShowDialog() -ne [System.Windows.Forms.DialogResult]::OK) { Write-Host "No file selected." exit } $jsonPath = $fileDialog.FileName # ========================================================== # Load JSON # ========================================================== try { $data = Get-Content $jsonPath -Raw | ConvertFrom-Json } catch { Write-Host "" Write-Host "Failed to load JSON." -ForegroundColor Red Write-Host $_.Exception.Message exit } if ($null -eq $data) { Write-Host "JSON contained no records." exit } if ($data -isnot [System.Array]) { $data = @($data) } # ========================================================== # Detect Fields # ========================================================== $firstRecord = $data[0] $fieldNames = @( $firstRecord.PSObject.Properties.Name ) if ($fieldNames.Count -eq 0) { Write-Host "No fields detected." exit } Write-Host "" Write-Host "Detected Fields:" -ForegroundColor Cyan foreach ($field in $fieldNames) { Write-Host " - $field" } # ========================================================== # Field Selection # ========================================================== $selectedFields = Show-FieldSelectionDialog -FieldNames $fieldNames if (-not $selectedFields -or $selectedFields.Count -eq 0) { Write-Host "No fields selected." exit } # ========================================================== # Capture Field Coordinates # ========================================================== $fieldMap = @{} Write-Host "" Write-Host "Field Mapping Setup" -ForegroundColor Cyan Write-Host "-----------------------------------------" foreach ($field in $selectedFields) { $fieldMap[$field] = Get-ClickPosition ` "Hover over the textbox for '$field'" } # ========================================================== # Capture Submit Button # ========================================================== $submitButton = Get-ClickPosition ` "Hover over the Submit / Save button" # ========================================================== # Test Mode Selection # ========================================================== $result = [System.Windows.Forms.MessageBox]::Show( "Run in Test Mode?`n`nYES = First record only (will submit)`nNO = Full run`nCANCEL = Exit", "Automation Mode", [System.Windows.Forms.MessageBoxButtons]::YesNoCancel, [System.Windows.Forms.MessageBoxIcon]::Question ) switch ($result) { ([System.Windows.Forms.DialogResult]::Yes) { $TestMode = $true } ([System.Windows.Forms.DialogResult]::No) { $TestMode = $false } default { exit } } # ========================================================== # Countdown # ========================================================== Write-Host "" Write-Host "Starting in 5 seconds..." Write-Host "Switch to the target application." for ($i = 5; $i -ge 1; $i--) { Write-Host $i Start-Sleep -Seconds 1 } # ========================================================== # Determine Records To Process # ========================================================== if ($TestMode) { $recordsToProcess = @($data[0]) } else { $recordsToProcess = $data } $totalRecords = $recordsToProcess.Count $currentRecord = 0 # ========================================================== # Main Loop # ========================================================== foreach ($record in $recordsToProcess) { $currentRecord++ Write-Host "" Write-Host "Processing record $currentRecord of $totalRecords" -ForegroundColor Yellow foreach ($field in $selectedFields) { $coords = $fieldMap[$field] # Write-Host "leftclicking" Invoke-HybridClick $coords.X $coords.Y Start-Sleep -Milliseconds 250 Invoke-HybridClick $coords.X $coords.Y Start-Sleep -Milliseconds 250 Invoke-HybridClick $coords.X $coords.Y Start-Sleep -Milliseconds 250 # Write-Host "ctrl+a" [System.Windows.Forms.SendKeys]::SendWait("^a") Start-Sleep -Milliseconds 250 # Write-Host "del" [System.Windows.Forms.SendKeys]::SendWait("{DELETE}") Start-Sleep -Milliseconds 250 $value = $record.$field if ($null -ne $value) { Set-Clipboard -Value ([string]$value) Start-Sleep -Milliseconds 100 # Write-Host $value # Write-Host "ctrl+v" [System.Windows.Forms.SendKeys]::SendWait("^v") } Start-Sleep -Milliseconds 500 } Invoke-HybridClick $submitButton.X $submitButton.Y Start-Sleep -Milliseconds 250 Invoke-HybridClick $submitButton.X $submitButton.Y Start-Sleep -Milliseconds 250 Write-Host "Submitted record $currentRecord" if ($TestMode) { Write-Host "" Write-Host "TEST MODE COMPLETE" -ForegroundColor Yellow Write-Host "Record populated. Submit button was clicked." $continueResult = [System.Windows.Forms.MessageBox]::Show( "Continue with a full run?", "Continue", [System.Windows.Forms.MessageBoxButtons]::YesNo, [System.Windows.Forms.MessageBoxIcon]::Question ) if ($continueResult -eq [System.Windows.Forms.DialogResult]::Yes) { $TestMode = $false } else { break } } Start-Sleep -Seconds 1 } # ========================================================== # Finished # ========================================================== Write-Host "" Write-Host "=====================================" -ForegroundColor Green Write-Host "Automation Complete" Write-Host "Records Processed: $currentRecord" Write-Host "=====================================" -ForegroundColor Green