- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试在 powershell 中执行以下命令,但不知道如何转义 & 字符,因为这是 url 的一部分
az rest `
--method GET `
--uri ("https://graph.microsoft.com/v1.0/groups?`$count=true&`$filter=startsWith(displayName,'some+filter+text')&`$select=id,displayName") `
--headers 'Content-Type=application/json'
因为 & 字符是用来开始一个新的命令,它打破了 url 并想执行其余的。
有没有办法告诉 powershell 不要这样做?
最佳答案
Olaf's answer提供有效的解决方案;让我添加一个解释:
问题的根源是两种行为的汇合:
调用外部程序时,PowerShell按需根据给定参数值是否包含空格对每个参数执行双引号/em> - 否则,参数被传递 unquoted - 不管值是否在 PowerShell 命令中被 最初 引用(例如, cmd/c echo ab
、cmd/c echo 'ab'
和 cmd/c echo "ab"
都会导致 unquoted ab
作为命令行上的最后一个 token 传递,PowerShell 在幕后重建以最终用于执行)。
Azure az
CLI 作为批处理文件 (az.cmd
) 实现,当调用批处理文件时,解析给定参数的是 cmd.exe
;令人惊讶 - 并且可以说是不恰当的 - 它解析它们就好像命令是从 cmd.exe
session 中提交的一样。
因此,如果参数从 PowerShell 传递到批处理文件,该文件 (a) 不包含空格,但 (b) 包含 cmd.exe
元字符如&
,调用breaks。
一个简单的演示,使用 cmd/c echo
调用作为对批处理文件的调用的替代:
# !! Breaks, because PowerShell (justifiably) passes *unquoted* a&b
# !! when it rebuilds the command line to invoke behind the scenes.
PS> cmd /c echo 'a&b'
a
'b' is not recognized as an internal or external command,
operable program or batch file.
有三种解决方法:
"..."
引用:# OK, but with a CAVEAT:
# Works as of PowerShell 7.2, but arguably *shouldn't*, because
# PowerShell should automatically *escape* the embedded " chars. as ""
PS> cmd /c echo '"a&b"'
"a&b"
# Ditto, using an *expandable* (interpolating) PowerShell string:
PS> cmd /c echo "`"$HOME & Family; can't put a `$ value on that.`""
"C:\Users\jdoe & Family; can't put a $ value on that." # e.g.
--%
,stop-parsing token - 但请参阅 this answer 的底部部分--%
的局限性及其相关的陷阱。# OK, but with a CAVEAT:
# Requires "..." quoting, but doesn't recognize *PowerShell* variables,
# also doesn't support single-quoting and line continuation.
PS> cmd /c echo --% "a&b"
"a&b"
cmd/c
调用并传递一个包含批处理文件调用及其所有参数的 single 字符串,(最终)使用 cmd。 exe
的语法.# OK (remember, cmd /c echo stands for a call to a batch file, such as az.cmd)
# Inside the single string passed to the outer cmd /c call,
# be sure to use "...", as that is the only quoting cmd.exe understands.
PS> cmd /c 'cmd /c echo "a&b"'
"a&b"
# Ditto, using an *expandable* (interpolating) PowerShell string:
PS> cmd /c "cmd /c echo `"$HOME & Family; can't put a `$ value on that.`""
"C:\Users\jdoe & Family; can't put a $ value on that." # e.g.
后退一步:
现在,如果您不必担心所有这些事情会不会很好?特别是因为您可能不知道或不关心给定的 CLI - 例如 az
- 是否恰好发生 被实现为 作为批处理文件?
作为一个 shell,PowerShell 应该尽最大努力在幕后忠实地传递参数,并允许调用者专门专注于仅满足 PowerShell 的语法规则:
不幸的是,PowerShell 迄今为止(PowerShell 7.2)通常在这方面做得很差,不管 cmd.exe
的怪癖如何 - 请参阅this answer总结一下。
关于 cmd.exe
的(批处理文件调用)怪癖,PowerShell 可以在未来的版本中可以预见地弥补它们 - 但看起来不幸的是,这种情况不会发生;见 GitHub issue #15143 .
关于电源外壳 7 : using ampersand (&) in string literals,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70841814/
这是我在试验 JS 时的最新发现: (function() { return this; }).call('string literal'); // => [String: 'string liter
在 C 中有没有一种方法可以使用宏从字 rune 字创建字符串文字? 比如我有 'a' 我想创建字符串文字 "a" 澄清问题: #define A 'a' write(fd, "x=" CHAR2ST
令我困扰的是为什么术语“literal”被用来指代NSString和NSArray等类的实例。我只看到了用于引用 NSString 的术语,并且天真地认为它与“字面上”是一个字符串(位于引号之间)有关
我有以下使用 smarty 模板引擎的代码 在 php 文件中: $smarty->assign('SITE_URL', 'http://localhost/mis/'); 在 tpl 文件中:
我正在使用 regex101 tool 制作正则表达式并在解释字段中阅读 [.] - 文字字符 .[\.] - 匹配字符 .字面上地 我在“字面字符”和“字面字符”之间迷失了方向。 这两者有什么区别?
这个问题在这里已经有了答案: Single quotes vs. double quotes in C or C++ (15 个答案) 关闭 4 年前。 我正在阅读一本讨论“字 rune 字”与“字
浮点文字 float x = 3.2f; 的直接赋值和隐式转换为 float 的 double 之间的位表示是否存在差异 float x2 = 3.2;? 即是 #define EQUAL(FLOAT
这个极其简单的 Rust 程序: fn main() { let c = "hello"; println!(c); } 抛出以下编译时错误: error: expected a li
这个极其简单的 Rust 程序: fn main() { let c = "hello"; println!(c); } 抛出以下编译时错误: error: expected a li
是否有单行,类型 mapfilter ... es5+ higherfunction 或 ...?希望解决方案描述如何以及为什么和引用。如果有人知道为什么曾经不起作用。 timeTableKeys:是
我有包含这些声明的代码: class IRealNetDll; extern "C"__declspec(dllexport) IRealNetDll * CreateRealInstance(int
我有一个用于我们的一些服务的 SOAP 服务器 API,其中一位客户说他们不会与不提供至少 WS-I 基本配置文件合规性绑定(bind)的服务集成。 那么,问题是任何版本的 Delphi 都支持 Do
我已经尝试使用这两个循环以及列表理解。即使我正在尝试将数字转换为列表中的整型,两者都无法解析整数。
在代码中隐藏文字值的主要现有方法是什么,以便仅使用 hexdumper 或反编译器不容易跟踪它们? 例如,而不是编码: static final int MY_VALUE = 100; 我们可
我正在尝试使用文字来匹配 Empty Guid,但我不知道这里发生了什么: let [] EmptyGuid = System.Guid () let someFunction () = System
我正在尝试搜索 [Panels] like "*,*" 在工作表中,不幸的是,它没有准确检索该字符串,而是检索如下内容: [Panels] like "*blah,*" [Panels] like "
我看不出自然语言编程和文学编程之间的区别。如果有人解释,我将不胜感激。 最佳答案 Natural language programming是一种以近似于人类书写或说话的语言的形式向计算机表达指令的
我想从字符串数组中初始化一个 pydantic 文字 from typing import Literal from pydantic import BaseModel CLASS_NAME_VALU
我正在尝试在代码后面编写我的js代码。我定义了一个名为“contentPie”的文字,然后将 javascript 代码放入其文本中。但这不起作用。 contentPie.Text = "window
我有一个简单的数据集,可以使用如下行进行解析: R1 (a/30) to R2 (b/30), metric 30 我需要从上面得到的唯一数据如下: R1, a, 30, R2, 192.168.0.
我是一名优秀的程序员,十分优秀!