前言:最近在使用PowerShell研究Windows自動化測試,在此篇做個整理,皆使用 Win11 22H2 測試過。
.
執行一般程式的各種方式
#Way-1
& calc
#Way-2
$proc= Start-Process -Wait -FilePath "calc" -PassThru
Write-Host $proc.ExitCode
.
執行UWP程式
#查詢UWP名稱並執行, 此例$appid: Microsoft.WindowsCamera_8wekyb3d8bbwe
$appid= (Get-StartApps "Camera").AppId
start "shell:AppsFolder\${appid}"
#直接執行UWP
start "shell:AppsFolder\$(Get-StartApps "Camera" | select -ExpandProperty AppId)"
.
送出WIN + X 按鍵,整數Key Code 請參考此處
# Import the necessary namespace
Add-Type -TypeDefinition @"
using System;
using System.Runtime.InteropServices;
public class KeyboardHelper {
[DllImport("user32.dll")]
public static extern void keybd_event(byte bVk, byte bScan, uint dwFlags, UIntPtr dwExtraInfo);
}
"@
function SendWinCombinationKey([char]$key) {
Set-Variable -Option ReadOnly KEY_OP_PRESS -Value 0
Set-Variable -Option ReadOnly KEY_OP_RELEASE -Value 2
Set-Variable -Option ReadOnly KEY_CODE_LEFT_WIN -Value 91
#Press
[KeyboardHelper]::keybd_event($KEY_CODE_LEFT_WIN, 0, $KEY_OP_PRESS, [UIntPtr]::Zero)
[KeyboardHelper]::keybd_event($key, 0, $KEY_OP_PRESS, [UIntPtr]::Zero)
#Release
[KeyboardHelper]::keybd_event($KEY_CODE_LEFT_WIN, 0, $KEY_OP_RELEASE, [UIntPtr]::Zero)
[KeyboardHelper]::keybd_event($key, 0, $KEY_OP_RELEASE, [UIntPtr]::Zero)
}
SendWinCombinationKey "E" #WIN + E to execute file explorer
.
Seek a window
#by Window Title
$proc_list= Get-Process | Where-Object { $_.MainWindowTitle -like "Camera"
}
#by Process Name
$proc_list= Get-Process | Where-Object { $_.ProcessName -like "WindowsCamera" }
.
Move Window to Front ground
Add-Type @"
using System;
using System.Runtime.InteropServices;
public class SFW {
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool SetForegroundWindow(IntPtr hWnd);
}
"@
function SetForegroundWindow($windowTitle) {
$proc_list = Get-Process | Where-Object { $_.MainWindowTitle -like $windowTitle }
foreach($proc in $proc_list) {
$ret= [SFW]::SetForegroundWindow( $proc.MainWindowHandle )
}
}
SetForegroundWindow "Camera"
.
更改視窗大小,參數請見 ShowWindow API
$user32 = Add-Type -Name User32 -Namespace Win32 -PassThru -MemberDefinition '[DllImport("user32.dll")] public static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);'
function ShowWindow([string]$WindowTitle, [int]$nCmdShow) {
$proc_list = Get-Process | Where-Object { $_.MainWindowTitle -like $WindowTitle }
foreach($proc in $proc_list) {
$user32::ShowWindow($proc.MainWindowHandle, $nCmdShow)
}
}
ShowWindow "Camera" 3 #3:SW_MAXIMIZE, 5:SW_SHOW, 6:SW_MINIMIZE
.
使用 Win + Z 快速鍵實作 Left/Right Maximum,應該是 Win11 之後才支援
Add-Type @"
using System;
using System.Runtime.InteropServices;
public class SFW {
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool SetForegroundWindow(IntPtr hWnd);
}
"@
$proc_list = Get-Process | Where-Object { $_.MainWindowTitle -like "Camera" }
foreach($proc in $proc_list) {
[SFW]::SetForegroundWindow( $proc.MainWindowHandle )
sleep 1
SendWinCombinationKey "Z" #[WIN]+[Z] to trigger the snip layout
sleep 1
SendKeys "1"
sleep 1
SendKeys "2" #2:right maximum, 1:left maximum
sleep 1
SendKeys "{ESC}"
}
.
送出按鍵Event到指定的APP, Key Event字串請見SendKeys API。Volume Up/Down須直接送出整數key code,Win key須改用 [KeyboardHelper]::keybd_event()
$KEY_CODE_VOLUME_UP= 175
$KEY_CODE_VOLUME_DOWN= 174
function SendKeys($keys) {
$obj = New-Object -ComObject WScript.Shell
$obj.SendKeys( $keys )
}
#It seems we need to bring a windows to Foreground AND ShowWindow for sending a key event
SetForegroundWindow "Camera"
ShowWindow "Camera" 5 #3:SW_MAXIMIZE, 5:SW_SHOW, 6:SW_MINIMIZE
#Send key events
SendKeys "^n" #CTRL + n
SendKeys "+n" #SHIFT + n
SendKeys "%n" #ALT + n
SendKeys "{ESC}" #ESC
SendKeys "1" # number 1
SendKeys "123{enter}" #number 1,2,3, then ENTER
SendKeys $KEY_CODE_VOLUME_UP # Volume up