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

This topic contains 1 reply, has 2 voices, and was last updated by Profile photo of Rob Simmers Rob Simmers 6 months, 1 week ago.

  • Author
    Posts
  • #61746
    Profile photo of anant p
    anant p
    Participant

    All,
    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.

      Thanks

  • #61761
    Profile photo of Rob Simmers
    Rob Simmers
    Participant

    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) {
            ($myLink).click()
        }
        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"
    }
    

You must be logged in to reply to this topic.