I have a PS script I’ve been using for a couple years that GETs data from a REST API. Sometimes has more than 100 rows, so I have paging set up to look if the header link contains rel=“next”. It’s been working perfectly.
I recently copied all of my scripts to a new Windows Server 2022 vm. When did that, I started using PowerShell 7 (x64) (currently running 7.2.7) and didn’t think anything about it until I ran a script today that failed to GET more than 100 rows. Stopped at 100 and didn’t proceed to other pages. When I run this same script on the same server in Windows PowerShell (5.1.20348 Rev. 617), it works perfectly and parses all pages until it reaches the end.
Why is that? Why is the code below working in ‘old’ PowerShell, but not PS 7?
Hi Olaf, thank you for the reply. You got me on the write track. As a PowerShell novice, I’m not clear on why the exact same script was working fine on PowerShell 5, but not on PowerShell 7. I ran them side by side, and it worked in one, but not the other without any changes.
Regardless, like you suggested, I looked to see what my while statement was evaluating, and it was this for the first page of 100 (with identifying info removed):
The result does contain rel=“next” so it was looping back just fine in PS 5, but also contains rel=“first”. Would PowerShell 5 see that first rel=“next”, then go back to the Do loop? And PowerShell 7 evaluate the whole line and fail to match because it sees rel=“next” at the end?
Because I wasn’t sure why the behavior was different between the two versions of PowerShell, I modified my loop to be a Do…Until with the Until statement being this since my last page contains rel=“last”:
Until ($header.Contains('rel="last"'))
Which probably simplifies things anyway. It is working in PowerShell 7 with this.
What did you do all theses years … to not become a pro?
That will happen once in a wile since PowerShell core is actually new developed and multiplatformed while Windows PowerShell only works on Windows. Of course some cmdlets behave differently since some of them have to use functions they aren’t available on non Windows systems.
Great to hear that you’ve found a solution and thanks for sharing.
hah! OK OK. I’m a little more than a novice, but definitely not a pro. My excuse is I work for an underfunded state university and have to wear too many hats to become a pro in most things. ‘jack of all trades’ type thing.