Forum Discussion
Powershell 7 compatible to Powershell 5?
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.
1 Reply
- boneyfrancisIron Contributor
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.
Per https://docs.microsoft.com/en-us/powershell/scripting/whats-new/breaking-changes-ps6?view=powershell-7.1:
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.
- Invoke-WebRequest now supports basic HTML Parsing only. Invoke-WebRequest always returns aBasicHtmlWebResponseObject object. The ParsedHtml and Forms properties have been removed.
More discussion on this limitation: https://github.com/PowerShell/PowerShell/issues/2867