AutomationFocusChangedEventHandler: доза события не запускается в powershell

#powershell #microsoft-ui-automation

#powershell #microsoft-ui-automation

Вопрос:

Я пытаюсь обнаружить изменение фокуса с помощью сценария powershell, это мой код

 start calc

Write-Host "Loading MS UIA assemblies"
[void][System.Reflection.Assembly]::LoadWithPartialName("UIAutomationClient")
[void][System.Reflection.Assembly]::LoadWithPartialName("UIAutomationTypes")
[void][System.Reflection.Assembly]::LoadWithPartialName("UIAutomationProvider")
[void][System.Reflection.Assembly]::LoadWithPartialName("UIAutomationClientsideProviders")



try
{
    # WORKAROUND: There is a weird bug: first call fails ...
    [Windows.Automation.ClientSettings]::RegisterClientSideProviderAssembly([UIAutomationClientsideProviders.UIAutomationClientSideProviders].Assembly.GetName())
}
catch {}
# ... second call succeeds:
[Windows.Automation.ClientSettings]::RegisterClientSideProviderAssembly([UIAutomationClientsideProviders.UIAutomationClientSideProviders].Assembly.GetName())


$focusedElem = [Windows.Automation.AutomationElement]::FocusedElement #this one work fine


Write-Host "name: " $focusedElem.Current.Name
Write-Host "ControlType: " $focusedElem.Current.ControlType
Write-Host "ProcessId: " $focusedElem.Current.ProcessId

$onFocusChange = { # this dose not fired !!!!
    param([Sytem.Object]$src, [Windows.Automation.AutomationFocusChangedEventArgs]$e)
    start notepad
}

$focusChangeHandler = [Windows.Automation.AutomationFocusChangedEventHandler]($onFocusChange)

[Windows.Automation.Automation]::AddAutomationFocusChangedEventHandler($focusChangeHandler)

Start-Sleep -Seconds 5 #durring this sleep i change the focused window, but nothing happen :(

[Windows.Automation.Automation]:: RemoveAutomationFocusChangedEventHandler($focusChangeHandler)
  

есть идеи, почему этот скрипт не работает, или какие-либо идеи о том, как отслеживать изменение фокуса в powershell без внешних инструментов?