Scenario:
Its a usual web automation requirement to setup auto login capability for certain sites through Internet Explorer browser. Most common implementation involves using internetexplorer.application COM object and accessing the web page HTML elements using DOM methods like getElementByID, getElementsByName, getElementsByTagName, etc.,
However, there are numerous complaints that while implementing COM automation scripts in VBScript, JavaScript, PowerShell, etc., you’ll find that none of the DOM methods fetches any results nor does auto-login to the website. Further $ie.READYSTATE will return just undefined or null. The general script to wait for IE to finish loading will never proceeds further.
In Specific to PowerShell, you’ll encounter below possible errors which all are related to the same cause and requires below mentioned fix.
PS C:\> C:\Temp\Auto-Launch-Login-IE-Browser.ps1
Method invocation failed because [System.__ComObject#{d30c1661-cdaf-11d0-8a3e-00c04fc9e26e}] doesn’t contain a method named ‘getElementsByTagName’.
At C:\packaging\Auto-Launch-Login-IE-Browser.ps1:10 char:37
+ $elements = $ie.getElementsByTagName <<<< (“*”)
+ CategoryInfo : InvalidOperation: (getElementsByTagName:String) [], RuntimeException
+ FullyQualifiedErrorId : MethodNotFoundYou cannot call a method on a null-valued expression.
At C:\Temp\Auto-Launch-Login-IE-Browser.ps1:21 char:35
+ if ($ie.Document.getElementsByName <<<< (“userId”)) {
+ CategoryInfo : InvalidOperation: (getElementsByName:String) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNullPS C:\>
PS C:\> $ie | Get-Member
Get-Member : The following exception occurred while retrieving the string representation for property “Application” : “
The object invoked has disconnected from its clients. (Exception from HRESULT: 0x80010108 (RPC_E_DISCONNECTED))”
At line:1 char:17
+ $ie | Get-Member <<<<
+ CategoryInfo : NotSpecified: (:) [Get-Member], ExtendedTypeSystemException
+ FullyQualifiedErrorId : CatchFromBasePropertyToString,Microsoft.PowerShell.Commands.GetMemberCommand
PS C:\>
Solution:
The above reported errors and behaviour is being caused by User Access Control (UAC) settings on your Windows system. By default UAC is enabled on Windows 7 and above desktops as well as on Windows Server 2008 and above server systems. The default UAC settings prevents execution of COM automation in general. When you have UAC enabled and set to default value, you can have your COM automation scripts executed fine by using “Run as Administrator” option and it required administrator access. To let your automation work for normal users login, you require to disable the UAC on your Windows Systems. Once you disable UAC, your scripts will just work fine and performs the auto-login to your websites.
References: