Created
March 25, 2022 19:31
-
-
Save mavericksevmont/5b213244e0c9ca951060eec75d4f2da1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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