Skip to content Skip to sidebar Skip to footer

Stubborn Ampersand With Invoke-webrequest Sql & Powershell

When I run powershell using invoke-webrequest on a URL without an ampersand everything works. But my URL's have ampersands in them. If I surround them by double quotes it works fro

Solution 1:

Add embedded"..."-quoting to the URL, which requires escaping as \"...\":

exec xp_cmdshell 'powershell Invoke-WebRequest-UseBasicParsing-Uri"\"https://example.com/getfile/12345&i=123\""-outfile C:\Downloads\test.txt'

This is necessary, because PowerShell's CLI (powershell.exe for Windows PowerShell, pwsh for PowerShell [Core] v6+), when used with the (implied) -Command (-c) option:

  • first removes "..." quoting around individual command-line arguments...
  • and then joins the stripped arguments with spaces and then interprets the resulting string as a PowerShell command line - and at that point quoting around the URL is required, because & is a PowerShell metacharacter.

Post a Comment for "Stubborn Ampersand With Invoke-webrequest Sql & Powershell"