カテゴリ[PowerShellでRPA]では、UI AutomationをPowerShellで使えるようにしたライブラリ「UI Automation PowerShell Extensions」を利用して、画面の操作を自動化する方法を紹介しています。操作対象画面は、Windows10の「タスクスケジューラ」です。
タスクスケジューラー

■処理フロー
 「タスクスケジューラ」を開く。
 「タスクの作成」を開く。
 「新しいトリガー」を開く。
 [日付]をクリックして入力する。
 [時刻]をクリックして入力する。
dateTimePicker(新しいトリガー)

■スクリプト
スクリプトを実行するときは、PowerShellを「管理者として実行」する必要があります。

#-----------------------------------------------------------------
Import-Module C:\UIAutomation\UIAutomation.0.8.7B3.NET35\UIAutomation.dll
[UIAutomation.Preferences]::Highlight=$false
#-----------------------------------------------------------------
#数字キーのKeycode定義
#-----------------------------------------------------------------
$KEYCODE = @(
    [WindowsInput.Native.VirtualKeycode]::VK_0
    ,[WindowsInput.Native.VirtualKeycode]::VK_1
    ,[WindowsInput.Native.VirtualKeycode]::VK_2
    ,[WindowsInput.Native.VirtualKeycode]::VK_3
    ,[WindowsInput.Native.VirtualKeycode]::VK_4
    ,[WindowsInput.Native.VirtualKeycode]::VK_5
    ,[WindowsInput.Native.VirtualKeycode]::VK_6
    ,[WindowsInput.Native.VirtualKeycode]::VK_7
    ,[WindowsInput.Native.VirtualKeycode]::VK_8
    ,[WindowsInput.Native.VirtualKeycode]::VK_9
)

#-----------------------------------------------------------------
#数字文字列の内容を数字キーのKeycodeで入力する。
#-----------------------------------------------------------------
function TextToKeyPress($wndw, $numtxt){
    for($i=0; $i -lt $numtxt.Length; $i++ ){
        $wndw.Keyboard.KeyPress($KEYCODE[[int]$numtxt.Substring($i,1)]) | Out-Null
    }
}

#「タスクスケジューラ」を開く。
Start-Process C:\Windows\System32\taskschd

#ウィンドウ表示を待つために待つ。
Start-Sleep -S 2

#[操作(A)]-[タスク作成(R)]をキー操作して「タスクの作成」を開く。
$wndwsch = Get-UiaWindow -Name 'タスク スケジューラ'
$wndwsch.Keyboard.KeyPress([WindowsInput.Native.VirtualKeycode]::MENU) | Out-Null
$wndwsch.Keyboard.KeyPress([WindowsInput.Native.VirtualKeycode]::VK_A) | Out-Null
$wndwsch.Keyboard.KeyPress([WindowsInput.Native.VirtualKeycode]::VK_R) | Out-Null

#ウィンドウ表示を待つために待つ。
Start-Sleep -S 2

#[トリガー]タブで[新規(N)]ボタンをクリックして「新しいトリガー」を開く。
$wndwtask = Get-UiaWindow -Name 'タスクの作成'
$wndwtask | Get-UiaTabItem -Name 'トリガー' | Invoke-UiaTabItemClick | Out-Null
$wndwtask | Get-UiaButton -Name '新規(N)...' | Invoke-UiaButtonClick | Out-Null

#ウィンドウ表示を待つために待つ。
Start-Sleep -S 2

#「新しいトリガー」ウィンドウを取得する。
$wndwnew =  Get-UiaWindow -Name '新しいトリガー'
$pane = $wndwnew | Get-UiaGroup -Name '設定' | Get-UiaPane -AutomationId 'tableLayoutPanelSchedule'

#[日付]をクリックして入力する。
$pane | Get-UiaComboBox -AutomationId 'dateTimePickerDate' | Invoke-UiaControlClick | Out-Null
TextToKeyPress $wndwnew "2020"
$wndwnew.Keyboard.KeyPress([WindowsInput.Native.VirtualKeycode]::RIGHT) | Out-Null
TextToKeyPress $wndwnew "02"
$wndwnew.Keyboard.KeyPress([WindowsInput.Native.VirtualKeycode]::RIGHT) | Out-Null
TextToKeyPress $wndwnew "01"
$wndwnew.Keyboard.KeyPress([WindowsInput.Native.VirtualKeycode]::RIGHT) | Out-Null

#[時刻]をクリックして入力する。
$pane | Get-UiaComboBox -AutomationId 'dateTimePickerTime' | Invoke-UiaControlClick | Out-Null
TextToKeyPress $wndwnew "12"
$wndwnew.Keyboard.KeyPress([WindowsInput.Native.VirtualKeycode]::RIGHT) | Out-Null
TextToKeyPress $wndwnew "34"
$wndwnew.Keyboard.KeyPress([WindowsInput.Native.VirtualKeycode]::RIGHT) | Out-Null
TextToKeyPress $wndwnew "56"
$wndwnew.Keyboard.KeyPress([WindowsInput.Native.VirtualKeycode]::RIGHT) | Out-Null

コメント

このブログにコメントするにはログインが必要です。