powershell - cannot call a method on a null-valued expression

Welcome Forums General PowerShell Q&A powershell - cannot call a method on a null-valued expression

This topic contains 1 reply, has 2 voices, and was last updated by

2 years, 2 months ago.

  • Author
  • #61746

    Points: 0
    Rank: Member

    I am trying to execute a powershell script in windows scheduler and getting the following error. "cannot call a method on a null-valued expression". The script runs fine when I execute outside the scheduler. The script logs in to a web page with credentials and has to click on a navigation-tab to enter additional details. The script fails where it tries to "click" the navigation tab. I am using Powershell version 2.0 on windows 2008 R2 and the IE version is 11.0.9600.18524.

    here is the html piece from the webpage

    • My Voice
    • Extension
    • Here is the code where it is failing in scheduler. (the code works well outside scheduler)
      ($IE.Document.getElementsByTagName("a") | where { $_.innerText -match "Extension" }).click()

      I tried to search with href , but still it returns null value while executing through windows scheduler. But works well when executed outside scheduler.
      ($IE.Document.getElementsByTagName("a") | ?{$_.href -match "`#Handling"}

      Any one has any suggestions to make it work in scheduler.


  • #61761

    Points: 638
    Helping Hand
    Rank: Major Contributor

    It's doubtful that if it works manually that your query is incorrect. Is the script running under different credentials, a different workstation\server, etc. that would cause issues? You can add some logging to the script to do some troubleshooting and determine where the failure is occuring:

    if ($IE) {
        $document = $IE.Document
        $links = $document.getElementsByTagName("a")
        Add-Content -Path C:\Logs\my.log -Value ("Found {0} links" -f $links.Count)
        $myLink = $links | where { $_.innerText -match "Extension" }
        if ($myLink) {
        else {
            Add-Content -Path C:\Logs\my.log -Value "No links found matching Extension"
    else {
        Add-Content -Path C:\Logs\my.log -Value "Internet Explorer object could not be instantiated"

The topic ‘powershell - cannot call a method on a null-valued expression’ is closed to new replies.

denizli escort samsun escort muğla escort ataşehir escort kuşadası escort