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

**URL:** <https://forums.powershell.org/t/powershell-cannot-call-a-method-on-a-null-valued-expression/7841>\
**Category:** PowerShell Help\
**Created:** [January 11, 2017, 2:22pm UTC](https://forums.powershell.org/t/powershell-cannot-call-a-method-on-a-null-valued-expression/7841 "2017-01-11T14:22:12Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![anantp](https://avatars.discourse-cdn.com/v4/letter/a/c68b51/32.png) [@anantp](https://forums.powershell.org/u/anantp)\
**Post date:** [January 11, 2017, 2:22pm UTC](https://forums.powershell.org/t/powershell-cannot-call-a-method-on-a-null-valued-expression/7841/1 "2017-01-11T14:22:12Z")

</div>

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

```
	<ul>
		<li>
			<a href="#voice">My Voice</a>	</li>
			<li>
			<a href="#Handling">Extension </a> </li>

```

## 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

---

<div class="post-metadata">

**Author:** ![rob-simmers](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/rob-simmers/32/1010_2.png) [@rob-simmers](https://forums.powershell.org/u/rob-simmers)\
**Post date:** [January 11, 2017, 8:44pm UTC](https://forums.powershell.org/t/powershell-cannot-call-a-method-on-a-null-valued-expression/7841/2 "2017-01-11T20:44:37Z")

</div>

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"
}
```

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:40pm UTC](https://forums.powershell.org/t/powershell-cannot-call-a-method-on-a-null-valued-expression/7841/3 "2024-05-16T20:40:29Z")

</div>


