Invoke-WebRequest: Cannot convert 'System.String[]' to the type 'System.Uri'
See original GitHub issueWhen querying the Office 365 Management API with PowerShell Core (7.1.3), it fails to correctly parse/process certain hyperlinks. PowerShell 5.1 works.
I believe this is due to the presence of $
in the URL.
A quick in-line fix for my script to work under PS v7 is simply $URL = ($URL -replace "\$","``$")
Steps to reproduce
$headerParams = @{'Authorization'="Bearer TOKENVALUE"}
$URL = "https://manage.office.com/api/v1.0/TENANCYGUID/activity/feed/subscriptions/content?contenttype=Audit.General&endtime=2021-03-16T13:21:48.1859513Z&starttime=2021-03-15T13:21:48.1859513Z&nextpage=20210315183117827000466$20210315132148185000000$emea0010"
Invoke-WebRequest -Method Get -Headers $headerParams -Uri $URL
Expected behavior
It works.
Actual behavior
Cannot convert 'System.String[]' to the type 'System.Uri' required by parameter 'Uri'. Specified method is not supported.
Environment data
PSVersion 7.1.3
PSEdition Core
GitCommitId 7.1.3
OS Microsoft Windows 10.0.19042
Platform Win32NT
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
WSManStackVersion 3.0
Issue Analytics
- State:
- Created 3 years ago
- Comments:7 (1 by maintainers)
Top Results From Across the Web
Struggeling with Invoke-WebRequest - Microsoft Q&A
The error message that keeps popups up would be "Cannot convert 'System.Object[]' to the type 'System.Uri' required by parameter 'Uri'.
Read more >Struggling with URIs : r/PowerShell
I keep getting an error when I am trying to Invoke-WebRequest with a proxy: I get this error : Cannot convert 'System.Object[]' to...
Read more >trying to download files using Invoke-WebRequest
I get an error stating "Invoke-WebRequest : Cannot convert 'System.Object[]' to the type 'System.Uri' required by parameter 'Uri'.
Read more >Invoke-WebRequest with dynamic host fails [duplicate]
Invoke -WebRequest : Cannot bind parameter 'Uri'. Cannot convert value "http:///service" to type "System.Uri". Error: "Invalid URI: The hostname ...
Read more >Powershell Invoke-RestMethod
Error: "Invoke-RestMethod : Cannot bind parameter 'Headers'. Cannot convert the "authorization: Token 297fecXXXXXXXXXXXXXXd31eff5" value of type "System.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
This is expected.
$emea0010
is a valid variable name, and in a double-quoted string PowerShell will attempt to interpolate its value. If the variable doesn’t exist, it will be replaced by an empty string (effectively removing the variable name from the string).The most straightforward solution is to use single quotes when defining the URL, this disables the interpolation and tells PowerShell to treat the string as literal:
As you mentioned, you can also escape the
$
with a backtick to indicate that symbol is literal and does not constitute the start of a variable name.This issue has been marked as answered and has not had any activity for 1 day. It has been closed for housekeeping purposes.