带有Invoke-Webrequest SQL&的顽固的“&"号电源外壳 [英] Stubborn Ampersand with Invoke-Webrequest SQL & Powershell

查看:75
本文介绍了带有Invoke-Webrequest SQL&的顽固的“&"号电源外壳的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在URL上使用 invoke-webrequest 来运行powershell时,没有连字符,一切正常.

When I run powershell using invoke-webrequest on a URL without an ampersand everything works.

但是我的URL中有&"号.如果我用双引号将其括起来,则可以在PowerShell中使用,但如果通过SQL Server进行操作则不能.

But my URL's have ampersands in them. If I surround them by double quotes it works from PowerShell, but not if I am doing it through my SQL Server.

试图正确组合转义字符被证明是一种痛苦.这是命令的示例:

Trying to get the right combination of escape characters is proving to be a pain in the butt. Here's an example of the command:

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

问题是& i = 123 上的&号.

如果我将-URI参数中的&"号更改为&" ,则它不起作用.如果我以肉汁克拉作为前缀(标签上方的`小),则无法使用.我还尝试用%26 替换它.

If I change the ampersand in the -URI parameter to "&" it does not work. If I prefix with the gravy carat (little ` above tab) it doesn't work. I have also tried to replace it with %26.

我的大脑在这里呆了几个小时.有什么建议吗?

Racking my brain here for hours. Any suggestions?

推荐答案

在URL中添加 embedded "..." -引用,这需要转义为 \&; ... \" :

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'

这是必需的,因为与(暗示的) powershell.exe ,对于PowerShell [Core] v6 +为 pwsh )> -Command ( -c )选项 [1] :

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[1]:

  • 首先删除单个命令行参数周围的"..." 引用...
  • ,然后用空格将剥离的参数连接起来,然后然后将结果字符串解释为PowerShell命令行-并需要在URL周围加上引号,因为& 是PowerShell元字符.
  • 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.

[1]请注意, pwsh 现在默认为 -File ,它需要一个 script文件.

[1] Note that pwsh now defaults to -File, which expects a script file.

这篇关于带有Invoke-Webrequest SQL&的顽固的“&"号电源外壳的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆