如何将JSON(字符串数据)传递到PowerShell?
我将以下内容作为参数传递给powershell(w7上的v4): 但是PS被挂在JSON上。我尝试过将\double quotes\分隔开来,并将-jsonContent后面的所有内容放在“单引号”中,但都没有用 以下是PS运行的Windows 7(PS4)环境: 注意:“…”混淆指的是同一个目录。注意,所有文件都在同一个目录中 运行批处理文件,启动整个过程:如何将JSON(字符串数据)传递到PowerShell?,json,powershell,batch-file,syntax,quoting,Json,Powershell,Batch File,Syntax,Quoting,我将以下内容作为参数传递给powershell(w7上的v4): 但是PS被挂在JSON上。我尝试过将\double quotes\分隔开来,并将-jsonContent后面的所有内容放在“单引号”中,但都没有用 以下是PS运行的Windows 7(PS4)环境: 注意:“…”混淆指的是同一个目录。注意,所有文件都在同一个目录中 运行批处理文件,启动整个过程: "C:\...\script.bat" > "C:\...\script-output.txt" 2>&1
"C:\...\script.bat" > "C:\...\script-output.txt" 2>&1
这将运行script.bat
,并输出到script output.txt
<代码>脚本.bat是一个长的单行程序:
%WINDIR%\sysnative\windowspowershell\v1.0\powershell.exe -ExecutionPolicy Bypass -File "C:\...\customscript.PS1" --% -fileName DropFileToCreate -jsonContent "{ "c": "some setting", "d": "unknown", "b": "some thing", "a": 1 }" -debugWrite
图例:
DropFileToCreate
-传递给PS脚本的文件名,用于在同一目录中创建文件
-jsonContent
-脚本中的命名参数(参见下面的customscript.PS1
标题)
在上面的示例中,JSON是:
{“c”:“某些设置”,“d”:“未知”,“b”:“某些设置”
事物“,”a“:1}”
-debugWrite
-开关参数(此处用于启用写主机调试)
最后,介绍一下customscript.PS1
:
Param (
[Parameter(Mandatory = $True)]
[String]
$fileName,
[Parameter(Mandatory = $True)]
$jsonContent,
[Parameter(Mandatory = $False)]
[Switch]
$debugWrite = $False
)
[...]
如果将JSON表示为:
{
"c": "some setting",
"d": "unknown",
"b": "some thing",
"a": 1
}
tl;dr
您的整个“…”
-包含的JSON字符串已嵌入“
,必须将其转义为\”
(原文如此;命令简化):
请继续阅读,了解何时需要额外转义、-File
调用与-Command
调用的区别,以及调用shell(从中调用powershell.exe
的shell)的重要性
注:
- 这个答案主要讨论Windows PowerShell可执行文件
的使用,但它同样适用于PowerShell核心可执行文件PowerShell.exe
,底部有一节介绍如何从pwsh
调用bash
- 下面从PowerShell本身调用的部分,特别是
所需的语法,也适用于将JSON传递给其他程序,如-File
curl.exe
PowerShell.exe
:
# With a literal string:
powershell -File ./script.ps1 "{ \"c\": \"some setting\", \"unknown\": \"b\" }"
# With an expandable string (expanded by the caller):
powershell -File ./script.ps1 "{ \"c\": \"some %USERNAME%\", \"unknown\": \"b\" }"
# With a literal string:
powershell -Command ConvertFrom-Json '"{ \"c\": \"some setting\", \"unknown\": \"b\" }"'
# With an expandable string (expanded by the caller):
powershell -Command ConvertFrom-Json '"{ \"c\": \"some %USERNAME%\", \"unknown\": \"b\" }"'
# With a literal string:
powershell -File ./script.ps1 '{ \"c\": \"some setting\", \"unknown\": \"b\" }'
# With an expandable string (expanded by the caller):
powershell -File ./script.ps1 "{ \`"c\`": \`"some $env:OS\`", \`"unknown\`": \`"b\`" }"
# With a literal string:
powershell -Command ConvertFrom-Json '''"{ \"c\": \"some setting\", \"unknown\": \"b\" }"'''
# With an expandable string (expanded by the caller):
powershell -Command ConvertFrom-Json "'{ \`"c\`": \`"some $env:OS\`", \`"unknown\`": \`"b\`" }'"
# With a literal string:
pwsh -File ./script.ps1 '{ "c": "some setting", "unknown": "b" }'
# With an expandable string (expanded by the caller):
pwsh -File ./script.ps1 "{ \"c\": \"some $USER\", \"unknown\": \"b\" }"
# With a literal string:
pwsh -Command ConvertFrom-Json \''{ "c": "some setting", "unknown": "b" }'\'
# With an expandable string (expanded by the caller):
pwsh -Command ConvertFrom-Json "'{ \"c\": \"some $USER\", \"unknown\": \"b\" }'"
- 无论您是从
(命令提示符/批处理文件)还是从PowerShell本身(或者,在PowerShell内核中,从类似POSIX的shell(如cmd.exe
)调用,都可以bash
- 是否将参数传递给
(内联命令)或powershell-Command
(脚本路径)powershell-File
{“c”:“某些设置”…}
无法识别为单个参数,因为包含空格,并且没有被括在引号中;后面添加的命令包含“…”
,缺少对嵌入的“
的转义
以下命令使用简化的JSON字符串演示了所讨论场景所需的语法
要使-File
命令可运行,请在当前目录中创建一个script.ps1
文件,其内容如下:ConvertFrom Json$Args[0]
从批处理文件
cmd.exe
/调用
- 嵌入的
必须作为“
转义(即使您在内部使用PowerShell时也会使用\”
)`
- 重要提示:
- 如果JSON文本包含
metacharacters(总是在cmd.exe
运行之间),则必须\“…\”
-分别对其进行转义,因为^
,由于未将cmd.exe
识别为转义的\
,因此认为这些子字符串没有引号;e、 例如,”
必须作为\“some&setting\”
转义;此处需要转义的\“some^&setting\”
元字符为:cmd.exe
和|<>^
- 如果JSON文本包含
-风格的环境变量引用如cmd.exe
是插值的-%USERNAME%
没有文字字符串语法,它只识别插入发生的地方,cmd.exe
,就像在无引号的令牌中一样。“…”
如果要按原样传递这样的令牌,即禁止插值,则转义语法取决于您是从命令行还是从批处理文件调用,遗憾的是:前者使用
,后者使用%^USERNAME%
,有关详细信息,请参阅%%USERNAME%%
- 请注意,
调用如何简单地添加另一层引号,方法是将-Command
字符串封装在“…”
中。这是必需的,因为对于“…”
PowerShell,它将收到的参数视为PowerShell源代码,而不是文字参数(后者是使用-Command
时发生的情况);如果不是为了封闭的-File
,在解释之前,整个封闭的“…”
将被剥离“…”
-文件
:
# With a literal string:
powershell -File ./script.ps1 "{ \"c\": \"some setting\", \"unknown\": \"b\" }"
# With an expandable string (expanded by the caller):
powershell -File ./script.ps1 "{ \"c\": \"some %USERNAME%\", \"unknown\": \"b\" }"
# With a literal string:
powershell -Command ConvertFrom-Json '"{ \"c\": \"some setting\", \"unknown\": \"b\" }"'
# With an expandable string (expanded by the caller):
powershell -Command ConvertFrom-Json '"{ \"c\": \"some %USERNAME%\", \"unknown\": \"b\" }"'
# With a literal string:
powershell -File ./script.ps1 '{ \"c\": \"some setting\", \"unknown\": \"b\" }'
# With an expandable string (expanded by the caller):
powershell -File ./script.ps1 "{ \`"c\`": \`"some $env:OS\`", \`"unknown\`": \`"b\`" }"
# With a literal string:
powershell -Command ConvertFrom-Json '''"{ \"c\": \"some setting\", \"unknown\": \"b\" }"'''
# With an expandable string (expanded by the caller):
powershell -Command ConvertFrom-Json "'{ \`"c\`": \`"some $env:OS\`", \`"unknown\`": \`"b\`" }'"
# With a literal string:
pwsh -File ./script.ps1 '{ "c": "some setting", "unknown": "b" }'
# With an expandable string (expanded by the caller):
pwsh -File ./script.ps1 "{ \"c\": \"some $USER\", \"unknown\": \"b\" }"
# With a literal string:
pwsh -Command ConvertFrom-Json \''{ "c": "some setting", "unknown": "b" }'\'
# With an expandable string (expanded by the caller):
pwsh -Command ConvertFrom-Json "'{ \"c\": \"some $USER\", \"unknown\": \"b\" }'"
使用-命令
:
# With a literal string:
powershell -File ./script.ps1 "{ \"c\": \"some setting\", \"unknown\": \"b\" }"
# With an expandable string (expanded by the caller):
powershell -File ./script.ps1 "{ \"c\": \"some %USERNAME%\", \"unknown\": \"b\" }"
# With a literal string:
powershell -Command ConvertFrom-Json '"{ \"c\": \"some setting\", \"unknown\": \"b\" }"'
# With an expandable string (expanded by the caller):
powershell -Command ConvertFrom-Json '"{ \"c\": \"some %USERNAME%\", \"unknown\": \"b\" }"'
# With a literal string:
powershell -File ./script.ps1 '{ \"c\": \"some setting\", \"unknown\": \"b\" }'
# With an expandable string (expanded by the caller):
powershell -File ./script.ps1 "{ \`"c\`": \`"some $env:OS\`", \`"unknown\`": \`"b\`" }"
# With a literal string:
powershell -Command ConvertFrom-Json '''"{ \"c\": \"some setting\", \"unknown\": \"b\" }"'''
# With an expandable string (expanded by the caller):
powershell -Command ConvertFrom-Json "'{ \`"c\`": \`"some $env:OS\`", \`"unknown\`": \`"b\`" }'"
# With a literal string:
pwsh -File ./script.ps1 '{ "c": "some setting", "unknown": "b" }'
# With an expandable string (expanded by the caller):
pwsh -File ./script.ps1 "{ \"c\": \"some $USER\", \"unknown\": \"b\" }"
# With a literal string:
pwsh -Command ConvertFrom-Json \''{ "c": "some setting", "unknown": "b" }'\'
# With an expandable string (expanded by the caller):
pwsh -Command ConvertFrom-Json "'{ \"c\": \"some $USER\", \"unknown\": \"b\" }'"
从PowerShell本身调用
- 从PowerShell调用会消除转义
元字符的需要,因为不涉及cmd.exe
cmd.exe
- PowerShell的字符串引用规则适用,这简化了问题,但遗憾的是,您仍然需要手动
-转义嵌入的\
字符。;请参阅背景信息“
- 更新:PowerShell Core 7.2.0-preview.5引入了一个,
,无需本手册PSNativeCommandArgumentPassing
-escaping;ev\
- 更新:PowerShell Core 7.2.0-preview.5引入了一个,