question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Invoke-WebRequest: Cannot convert 'System.String[]' to the type 'System.Uri'

See original GitHub issue

When 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:closed
  • Created 3 years ago
  • Comments:7 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
vexx32commented, Mar 16, 2021

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:

$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

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.

0reactions
msftbot[bot]commented, Jan 20, 2022

This issue has been marked as answered and has not had any activity for 1 day. It has been closed for housekeeping purposes.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found