Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何将JSON(字符串数据)传递到PowerShell?_Json_Powershell_Batch File_Syntax_Quoting - Fatal编程技术网

如何将JSON(字符串数据)传递到PowerShell?

如何将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

我将以下内容作为参数传递给powershell(w7上的v4):

但是PS被挂在JSON上。我尝试过将\double quotes\分隔开来,并将-jsonContent后面的所有内容放在“单引号”中,但都没有用

以下是PS运行的Windows 7(PS4)环境:

注意:“…”混淆指的是同一个目录。注意,所有文件都在同一个目录中

运行批处理文件,启动整个过程:

    "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.exe
    的使用,但它同样适用于PowerShell核心可执行文件
    pwsh
    ,底部有一节介绍如何从
    bash
    调用

  • 下面从PowerShell本身调用的部分,特别是
    -File
    所需的语法,也适用于将JSON传递给其他程序,如
    curl.exe

PowerShell CLI所需的语法取决于,即使用参数调用
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\" }'"
  • 无论您是从
    cmd.exe
    (命令提示符/批处理文件)还是从PowerShell本身(或者,在PowerShell内核中,从类似POSIX的shell(如
    bash
    )调用,都可以

  • 是否将参数传递给
    powershell-Command
    (内联命令)或
    powershell-File
    (脚本路径)

无论如何,您最初的尝试都不可能成功,因为literal
{“c”:“某些设置”…}
无法识别为单个参数,因为包含空格,并且没有被括在引号中;后面添加的命令包含
“…”
,缺少对嵌入的
的转义

以下命令使用简化的JSON字符串演示了所讨论场景所需的语法

要使
-File
命令可运行,请在当前目录中创建一个
script.ps1
文件,其内容如下:
ConvertFrom Json$Args[0]


从批处理文件
cmd.exe
/调用
  • 嵌入的
    必须作为
    \”
    转义(即使您在内部使用PowerShell时也会使用
    `

  • 重要提示:

    • 如果JSON文本包含
      cmd.exe
      metacharacters
      (总是在
      \“…\”
      运行之间),则必须
      ^
      -分别对其进行转义
      ,因为
      cmd.exe
      ,由于未将
      \
      识别为转义的
      ,因此认为这些子字符串没有引号;e、 例如,
      \“some&setting\”
      必须作为
      \“some^&setting\”
      转义;此处需要转义的
      cmd.exe
      元字符为:
      和|<>^
  • cmd.exe
    -风格的环境变量引用
    %USERNAME%
    是插值的-
    cmd.exe
    没有文字字符串语法,它只识别插入发生的地方,
    “…”
    ,就像在无引号的令牌中一样。
    如果要按原样传递这样的令牌,即禁止插值,则转义语法取决于您是从命令行还是从批处理文件调用,遗憾的是:前者使用
    %^USERNAME%
    ,后者使用
    %%USERNAME%%
    ,有关详细信息,请参阅

  • 请注意,
    -Command
    调用如何简单地添加另一层引号,方法是将
    “…”
    字符串封装在
    “…”
    中。这是必需的,因为对于
    -Command
    PowerShell,它将收到的参数视为PowerShell源代码,而不是文字参数(后者是使用
    -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