Frequent Powershell crashes with "Windows.Automation.ElementNotAvailable" error with extensive use of UIAutomation and try-catch blocks

Jul 11, 2014 at 5:53 PM
I am experiencing frequent Powershell crashes with extensive use of UIAutomation and try-catch blocks. Not sure if this is specific to UIAutomation but I sorely need some advice how to workaround this problem.

Periodically, I will get popup dialog box that Powershell has stopped working and the script will freeze. (Dismissing the dialog will exit the script.) Windows Application Event log will show WER (Windows Error Reporting) details with a reference to a local Report.wer file. The contents of the file describe crash because some item (not specified) is not found with "Windows.Automation.ElementNotAvailable" error.

However running the script again crashes in another location in the script (with same kind of error), successfully getting past the place where the first crash was said to not find some unknown item.

Because I am dealing with an application where one or more dialogs will show up under certain circumstances in an unknown order, I use try-catch blocks. They seem to work, but I encounter these crashes seemingly only in a script when using several independent try-catch blocks.

Because the crash can occur in more than one place in the same script, I consider it might be a resource or memory issue, so I have increased the memory used by PowerShell, but that did not help.

What to do? Any advice?

(Is there a way to get the error report to indicate which item was not found?)

============ Example of Report.wer file: =================
Version=1
EventType=PowerShell
EventTime=130495710772338545
ReportType=1
Consent=1
UploadTime=130495710772828545
ReportIdentifier=92f53781-091b-11e4-bd2a-586d8f50ad6d
Response.BucketId=50022624
Response.BucketTable=21
Response.type=4
Sig[0].Name=NameOfExe
Sig[0].Value=powershell.exe
Sig[1].Name=FileVersionOfSystemManagementAutomation
Sig[1].Value=6.1.7601.17514
Sig[2].Name=InnermostExceptionType
Sig[2].Value=m.Windows.Automation.ElementNotAvailable
Sig[3].Name=OutermostExceptionType
Sig[3].Value=m.Windows.Automation.ElementNotAvailable
Sig[4].Name=DeepestPowerShellFrame
Sig[4].Value=unknown
Sig[5].Name=DeepestFrame
Sig[5].Value=MS.Internal.Automation.UiaCoreApi.CheckError
Sig[6].Name=ThreadName
Sig[6].Value=unknown
DynamicSig[1].Name=OS Version
DynamicSig[1].Value=6.1.7601.2.1.0.256.4
DynamicSig[2].Name=Locale ID
DynamicSig[2].Value=1033
UI[3]=powershell has stopped working
UI[4]=Windows can check online for a solution to the problem.
UI[5]=Check online for a solution and close the program
UI[6]=Check online for a solution later and close the program
UI[7]=Close the program
State[0].Key=Transport.DoneStage1
State[0].Value=1
State[1].Key=DataRequest
State[1].Value=iData=1/nDumpFile=//Upload//iCab//423be1e6a77a4f8db0dee5bf8fe0fb75-caf65010b2c35e8de06a752cf0d52b18-5-90244335840-PowerShell-6-1-7601-2.cab/nDumpServer=watson.microsoft.com/nResponseServer=watson.microsoft.com/nResponseURL=//dw//GenericFour.asp?iBucket=90244335840&szCab=423be1e6a77a4f8db0dee5bf8fe0fb75.cab&EventType=PowerShell&BucketHash=caf65010b2c35e8de06a752cf0d52b18&MID=D904BD65-48DB-4029-86A7-02F23BB270BA/nBucket=50022624/nBucketTable=21/nResponse=1/n
FriendlyEventName=PowerShell
ConsentKey=PowerShell
AppName=powershell
AppPath=C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
=============== Example of Event log message: ============

Fault bucket 50022624, type 21
Event Name: PowerShell
Response: Not available
Cab Id: 0

Problem signature:
P1: powershell.exe
P2: 6.1.7601.17514
P3: m.Windows.Automation.ElementNotAvailable
P4: m.Windows.Automation.ElementNotAvailable
P5: unknown
P6: MS.Internal.Automation.UiaCoreApi.CheckError
P7: unknown
P8:
P9:
P10:

Attached files:

These files may be available here:
C:\Users\<username>\AppData\Local\Microsoft\Windows\WER\ReportArchive\Critical_powershell.exe_808458abe78775ed6aefed15d0db4b6d4e6794a_160194e1

Analysis symbol:
Rechecking for solution: 0
Report Id: 92f53781-091b-11e4-bd2a-586d8f50ad6d

Report Status: 0

Thanks for any advice or help.
Coordinator
Jul 11, 2014 at 6:14 PM
Hi joespr,
I believe I have fixed this or a very similar issue - right now I can't say whether it's published or not yet.
Initially the issue I faced grew from the wizard cmdlets, specifically the Invoke-UiaWizard cmdlet, but was not limited to these cmdlets.
The issue, however, didn't throw a dialog, it threw an internal MS UI Automation exception that could suddenly spoil the script execution.

Fortunately, I have a keyboard at work with "run a process" button (a Microsoft's keyboard with the Calc button that runs calc.exe). I have even bought the same for home. :)
If you run something like (I forgot the exact script I tested with :( ) Get-UiaWindow -n calc* -Seconds 300 and run and close some windows called calc* (I used my fingers and the MS keyboard), it often throws an internal exception, and go out of the waiting for a window.
Probably, it was fixed after I uploaded the last binaries - I'll build today a version and upload it as non-public beta to allow you to run the latest binaries in the hope that the issue could be fixed.
Coordinator
Jul 12, 2014 at 8:50 AM
I uploaded the current binaries as 0.8.7 Beta 3.
Just to learn whether the problem is fixed or still in the code.
Unfortunately, when I introduced new logging, calls to cmdlets and object model were added to the logging, but many failures like "control not found" weren't. I'll add failure output to the log ~next week.
Jul 14, 2014 at 4:09 PM
Thanks apetrovsky for the quick response and the build.

When I attempt to run with this new 0.8.7 Beta 3, I get "Could not load file or assembly " messages:

Microsoft Windows [Version 6.1.7601]
Copyright (c) 2009 Microsoft Corporation. All rights reserved.

C:\Tools\PowerShell>powershell
Windows PowerShell
Copyright (C) 2009 Microsoft Corporation. All rights reserved.

PS C:\Tools\PowerShell> ipmo ($env:USERPROFILE + "\Documents\WindowsPowerShell\M
odules\UIAutomation\UIAutomation.dll")
Import-Module : Could not load file or assembly 'file:///C:\Users\username\
Documents\WindowsPowerShell\Modules\UIAutomation\UIAutomation.dll' or one of it
s dependencies. Operation is not supported. (Exception from HRESULT: 0x80131515
)
At line:1 char:5
  • ipmo <<<< ($env:USERPROFILE + "\Documents\WindowsPowerShell\Modules\UIAutoma
    tion\UIAutomation.dll")
    • CategoryInfo : NotSpecified: (:) [Import-Module], FileLoadExcep
      tion
    • FullyQualifiedErrorId : System.IO.FileLoadException,Microsoft.PowerShell
      .Commands.ImportModuleCommand

PS C:\Tools\PowerShell>

This of course worked with previous build (0.8.7 Beta 2).
Perhaps, is there some version dependency?

Thanks,
joespr
Coordinator
Jul 14, 2014 at 5:58 PM
Have you unchecked the Unblock button in the file Properties dialog of the archive you have downloaded?
It's default security feature of contemporary Windows systems, to block files users downloaded from the Internet.
Just to unblock the archive, unpack and the contents of the archive is also unblocked.
Jul 14, 2014 at 9:09 PM
Thanks apetrovsky, for the reminder. Yes, the download security feature stopped the loading of the module. Once I "unblocked" the files, I can load the module.

I am running the automation now to see the state of things. So far I have not encountered the original error condition, but I need to do more testing. I will comment here when I have done more testing.

Thanks
joespr
Jul 17, 2014 at 10:29 PM
Thanks for the build.

I do not see the worst of these errors, "Windows.Automation.ElementNotAvailable ", in the new build

I still get errors, but it appears that the application being tested also has a memory leak or some other instabilities. Therefore it is hard to pin down where or what is going on, and where the root cause is. But it seems UIAutomation is more stable.

Thanks
joespr