Skip to content

Instantly share code, notes, and snippets.

@mavericksevmont
Created March 25, 2022 19:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mavericksevmont/5b213244e0c9ca951060eec75d4f2da1 to your computer and use it in GitHub Desktop.
Save mavericksevmont/5b213244e0c9ca951060eec75d4f2da1 to your computer and use it in GitHub Desktop.
Function Find-Window {
[CmdletBinding()]Param (
[string]$WindowName,
[int]$Retries = 10,
[int]$SleepInterval = 1000
)
[int]$CurrentTry = 0;
[bool]$WindowsFound = $false;
Add-Type -AssemblyName Microsoft.VisualBasic
Add-Type -AssemblyName System.Windows.Forms
Do {
$CurrentTry++;
Start-Sleep -Milliseconds $SleepInterval
Try { [Microsoft.VisualBasic.Interaction]::AppActivate($windowName) ; $windowFound = $true; }
Catch { Write-Verbose "[$currentTry out of $Retries] Waiting for Window with title '$WindowName'" ; $WindowFound = $false }
} While ($CurrentTry -lt $retries -and $WindowFound -eq $false)
return $WindowFound
}
Add-Type -AssemblyName Microsoft.VisualBasic
Add-Type -AssemblyName System.Windows.Forms
start calc
Find-Window -WindowName 'Calculator' -Verbose
Start-Sleep -Milliseconds 500
[System.Windows.Forms.SendKeys]::SendWait('2')
Start-Sleep -Milliseconds 500
[System.Windows.Forms.SendKeys]::SendWait('{ADD}')
Start-Sleep -Milliseconds 500
[System.Windows.Forms.SendKeys]::SendWait('2')
Start-Sleep -Milliseconds 500
[System.Windows.Forms.SendKeys]::SendWait('{ENTER}')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment