Powershell 7 compatible to Powershell 5?

%3CLINGO-SUB%20id%3D%22lingo-sub-2031338%22%20slang%3D%22en-US%22%3EPowershell%207%20compatible%20to%20Powershell%205%3F%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2031338%22%20slang%3D%22en-US%22%3E%3CP%3EI%20got%20many%20issues%20when%20I%20tried%20to%20migrate%20the%20scripts%20developed%20in%20PS5%20to%20PS7%2C%20such%20as%20Form%2C%20DataGridView%20and%20so%20on.%3C%2FP%3E%3CP%3EFor%20a%20simple%20example%2C%20I%20have%20the%20following%20code%20working%20fine%20in%20PS%205.1.18362.1171%3C%2FP%3E%3CP%3EHowever%2C%20when%20running%20in%20PS%207.0.2%2C%20I%20got%20the%20error%3A%3C%2FP%3E%3CP%3E%3CFONT%20color%3D%22%23FF0000%22%3EInvalidOperation%3A%20You%20cannot%20call%20a%20method%20on%20a%20null-valued%20expression.%3C%2FFONT%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%24p%3D(Invoke-WebRequest%20-Uri%20%22%3CA%20href%3D%22https%3A%2F%2Ffinance.yahoo.com%2Fquote%2Fbngo%2Fkey-statistics%3Fp%3Dbngo%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Ffinance.yahoo.com%2Fquote%2Fbngo%2Fkey-statistics%3Fp%3Dbngo%3C%2FA%3E%22)%3C%2FP%3E%3CP%3E%24t%3D%24p.ParsedHtml.getElementsByTagName('table')%5B2%5D%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EIs%20there%20a%20way%20to%20migrate%20code%20easily%20or%20I%20have%20to%20completely%20rewrite%20a%20lot%20of%20code%3F%20Thanks.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-2031338%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EWindows%20PowerShell%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2033330%22%20slang%3D%22en-US%22%3ERe%3A%20Powershell%207%20compatible%20to%20Powershell%205%3F%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2033330%22%20slang%3D%22en-US%22%3E%3CP%3EHi%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F124684%22%20target%3D%22_blank%22%3E%40Yuansheng%20Zhang%3C%2FA%3E%26nbsp%3B%2C%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20verified%20your%20code%20on%20PS5%20and%20PS7%2C%20and%20confirmed%20that%20%22%24p%22%20lists%20%3CSTRONG%3EParsedHtml%3C%2FSTRONG%3E%20in%20PS5%20but%20not%20in%20PS7%3B%20this%20is%20why%20you're%20getting%20the%20null-valued%20expression%20error%20because%20%24t%20is%20looking%20for%20%24p.%3CSTRONG%3EParsedHtml%3C%2FSTRONG%3E.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EPer%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fpowershell%2Fscripting%2Fwhats-new%2Fbreaking-changes-ps6%3Fview%3Dpowershell-7.1%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3Ehttps%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fpowershell%2Fscripting%2Fwhats-new%2Fbreaking-changes-ps6%3Fview%3Dpowershell-7.1%3C%2FA%3E%3A%3C%2FP%3E%3CP%3EThe%20underlying%20.NET%20API%20of%20the%20Web%20Cmdlets%20has%20been%20changed%20to%20System.Net.Http.HttpClient.%20This%20change%20provides%20many%20benefits.%20However%2C%20this%20change%20along%20with%20a%20lack%20of%20interoperability%20with%20Internet%20Explorer%20have%20resulted%20in%20several%20breaking%20changes%20within%20Invoke-WebRequest%20and%20Invoke-RestMethod.%3C%2FP%3E%3CUL%3E%3CLI%3EInvoke-WebRequest%20now%20supports%20basic%20HTML%20Parsing%20only.%20Invoke-WebRequest%20always%20returns%20aBasicHtmlWebResponseObject%20object.%20The%20ParsedHtml%20and%20Forms%20properties%20have%20been%20removed.%3C%2FLI%3E%3C%2FUL%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EMore%20discussion%20on%20this%20limitation%3A%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Fgithub.com%2FPowerShell%2FPowerShell%2Fissues%2F2867%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3Ehttps%3A%2F%2Fgithub.com%2FPowerShell%2FPowerShell%2Fissues%2F2867%3C%2FA%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E
Occasional Contributor

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

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...:

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