Jan 04 2021 04:07 PM
I got many issues when I tried to migrate the scripts developed in PS5 to PS7, such as Form, DataGridView and so on.
For a simple example, I have the following code working fine in PS 5.1.18362.1171
However, when running in PS 7.0.2, I got the error:
InvalidOperation: You cannot call a method on a null-valued expression.
$p=(Invoke-WebRequest -Uri "https://finance.yahoo.com/quote/bngo/key-statistics?p=bngo")
$t=$p.ParsedHtml.getElementsByTagName('table')[2]
Is there a way to migrate code easily or I have to completely rewrite a lot of code? Thanks.
Jan 05 2021 07:49 AM
Hi @Yuansheng Zhang ,
I verified your code on PS5 and PS7, and confirmed that "$p" lists ParsedHtml in PS5 but not in PS7; this is why you're getting the null-valued expression error because $t is looking for $p.ParsedHtml.
The underlying .NET API of the Web Cmdlets has been changed to System.Net.Http.HttpClient. This change provides many benefits. However, this change along with a lack of interoperability with Internet Explorer have resulted in several breaking changes within Invoke-WebRequest and Invoke-RestMethod.
More discussion on this limitation: https://github.com/PowerShell/PowerShell/issues/2867