Windows 10: UIAutomation Events on Powershell

Discus and support UIAutomation Events on Powershell in Windows 10 BSOD Crashes and Debugging to solve the problem; I'm trying to listen UIAutomation events using Powershell, and wrote somethings like this. $propChangeHandler=... Discussion in 'Windows 10 BSOD Crashes and Debugging' started by richardchung, May 20, 2020 at 8:12 AM.

  1. UIAutomation Events on Powershell

    I'm trying to listen UIAutomation events using Powershell, and wrote somethings like this.

    $propChangeHandler= [System.Windows.Automation.AutomationPropertyChangedEventHandler]# <summary># Adds a handler for property-changed event in particular, a change in the enabled state.# </summary># <param name="element">The UI Automation element whose state is being monitored.</param>function Subscribe-PropertyChange{param[System.Windows.Automation.AutomationElement]$element[System.Windows.Automation.Automation]::AddAutomationPropertyChangedEventHandler$element, [System.Windows.Automation.TreeScope]::Element, $propChangeHandler$OnPropertyChange, [System.Windows.Automation]::AutomationElement.IsEnabledProperty}# <summary># Handler for property changes.# </summary># <param name="src">The source whose properties changed.</param># <param name="e">Event arguments.</param>$OnPropertyChange={param[System.Object]$src, [System.Windows.Automation.AutomationPropertyChangedEventArgs]$e [System.Windows.Automation.AutomationElement]$sourceElement = $src -as [System.Windows.Automation.AutomationElement] if $e.Property -eq [System.Windows.Automation.AutomationElement]::IsEnabledProperty { [System.Boolean]$enabled = $e.NewValue -as [System.Boolean] # TODO: Do something with the new value. # The element that raised the event can be identified by its runtime ID property. Write-Host"{0} is Enable" -f $sourceElement.Current.Name } else { # TODO: Handle other property-changed events. }}function Unsubscribe-PropertyChange{param[System.Windows.Automation.AutomationElement]$element if $propChangeHandler -ne $null { [System.Windows.Automation.Automation]::RemoveAutomationPropertyChangedEventHandler$element, $propChangeHandler }}

    But I got a problem.

    The AddautomationPropertyChangedEventHandlermethod will need a AutomationPropertyChangedEventHandler with an eventHandler as parameter as parameter, but both AutomationPropertyChangedEventHandler and eventHandler are functions.

    That means functionfunctionfunction

    Please find the $propChangeHandler$OnPropertyChange from the above code.

    Obviously writing it as $propChangeHandler$OnPropertyChange is not correct. How can I revise?

    richardchung, May 20, 2020 at 8:12 AM
  2. ddelo Win User

    Export All Administrative Events to Excel

    To analyze events, from the Windows Event Viewer, there is a simple way to export all Administrative Events to Excel, with PowerShell.

    Exporting all Administrative Events to Excel is a simple two Step process, as described here:

    Step 1 - Create the Administrative Events View .xml file
    1. Open Eventviewer (%windir%\system32\eventvwr.msc)
    2. Navigate to: Event Viewer (Local) > Custom Views > Administrative Events
    3. In the “Actions” pane select “Filter Current Custom View”.
    4. Select the the XML tab.
    5. Press Ctrl+A to select all the XML code of the Custom View.
    6. Open a notepad, paste the selected code and save the file to your Desktop as AdmEvtView.xml

    Step 2 - Create the csv file with the events
    1. Download the file, which contains the script ExportEvtCSV.ps1 and unzip it, on your Desktop.
      It's not a fancy script, just basic PowerShell commands to create a csv file on the Desktop.
    2. In Windows Search, type “ISE” (without the quotes) to open “Windows PowerShell ISE” and Run as administrator
    3. To allow running the script, change the ExecutionPolicy, for this session. To do that, in the Console pane type:
    4. In the Windows PowerShell ISE, open and run the script: ExportEvtCSV.ps1
      The script will create a csv file with a name YYYYMMDD.HHMM.csv on the Desktop
    5. When done, open the newly created .csv file, format the columns as needed and optionally save it as .xlsx, if you wish.
    That’s it! You now have all the Administrative Events in Excel for filtering and further analysis. UIAutomation Events on Powershell :)

    Now to the more technical hard stuff... *Confused

    There is a reason for running the script from within PowerShell ISE!

    It would be great if everything was also working perfectly, when running the script from an elevated PowerShell too.

    We can run it from an elevated PowerShell, which means that you just follow the Step 1, as above but for the Step 2 instead of the ISE you run the script from an elevated PowerShell.

    The problem is that it will work only for anybody who has en-US format for the dates. Everyone else, who has another format (i.e. en-GB, fr-FR, el-GR etc.), the dates are not translated properly by Excel (although the script uses the –UseCulture switch) and remain as text in the en-US format.

    I'm not sure if this a bug of the "export-csv" cmdlet, but although it runs the way it supposed to from within the ISE, from PowerShell there is a problem with the dates format.
    As I haven’t found a way to overcome this obstacle, any suggestion from the PowerShell gurus of the forum (like my good friend Shawn @Brink, for instance), is welcome.
  3. angstar Win User
  4. Drone Win User

    UIAutomation Events on Powershell

    PowerShell is open sourced and is available on Linux/macOS

    Little bit late .. PowerShell's 10th anniversary (Nov. 14)

UIAutomation Events on Powershell

  1. UIAutomation Events on Powershell - Similar Threads - UIAutomation Events Powershell

  2. Powershell

    in Windows 10 Customization
    Powershell: I am a total new comer to powershell so please forgive me if my question sounds stupid. I found the script below from Yuri Posidelov which I tweaked to activate a process and show the window and send keystrokes to shut down the process which works great. However it fails if...
  3. Powershell

    in Windows 10 Installation and Upgrade
    Powershell: Hi, ive been suffering lots of system hangs with event code 10016 in the last few weeks. The 2 culprits seem to be PerAppRuntimeBoker and ShellServiceHost. Having checked for causes/fixes ive found a thread that suggests modifying the registry to make administrators the...
  4. Powershell

    in Windows 10 Customization
    Powershell: What is Powershell and can I safely delete it
  5. Powershell

    in Windows 10 Customization
    Powershell: This post does not belong in "Windows settings." There is no category to actually post this discussion to. For all the effort Microsoft is making to embrace command line utilities via powershell, you'd think the forums would include a category similar to, I don't know,...
  6. powershell

    in Windows 10 Network and Sharing
    powershell: hi what is the Powershell Adminitrator command to check for corrupt file i.e. "slp /??"
  7. Powershell/Powershell admin missing

    in Windows 10 BSOD Crashes and Debugging
    Powershell/Powershell admin missing: When finding a solution to remove skype today I came across a post here where the solution was found to use a powershell command. So I went to open it up and found out it was missing. I looked into it and found some other people with a similar problem, and they went to check...
  8. Multiple "Powershell has stopped working" events

    in Windows 10 Support
    Multiple "Powershell has stopped working" events: A friend is getting many Powershell errors [img] Often 20 or more interruptions per session. The Application Event log shows many sets of 4 events Error 22/03/2018 13:05:08 Application Error 1000 (100) Error 22/03/2018 13:05:08 Application Error 1000 (100) Error...
  9. Access Denied while clearing Event Viewer error in Powershell

    in Windows 10 Support
    Access Denied while clearing Event Viewer error in Powershell: I am trying to cleat all the events in my event viewer as I had so many errors in the past 7 days. I opened powershell & ran as administrator & use this command wevtutil el | Foreach-Object {wevtutil cl “$_”} & I had some access denied errors. PS C:\Windows\system32>...
  10. PowerShell

    in Windows 10 Performance & Maintenance
    PowerShell: I have a 64-bit laptop running Win 10 Home. I just notice in All apps, there is an entry of PowerShell and PowerShell (x86). I realize the former is for 64-bit and the latter is for 32-bit. Question : In my 64-bit laptop, when I open PowerShell, which one am I...