如何将选项作为地图传递?

How to pass options as a map?

我遇到了这个 bash 脚本,我需要在 PowerShell 中做同样的事情。

vault write ssh-client-signer/roles/my-role -<<"EOH"
{
  "allow_user_certificates": true,
  "allowed_users": "*",
  "default_extensions": [
    {
      "permit-pty": ""
    }
  ],
  "key_type": "ca",
  "default_user": "ubuntu",
  "ttl": "30m0s"
}
EOH

我试过像这样使用多行字符串:

vault write ssh-client-signer/roles/my-role -@"
{
  "allow_user_certificates": true,
  "allowed_users": "*",
  "default_extensions": [
    {
      "permit-pty": ""
    }
  ],
  "key_type": "ca",
  "default_user": "ubuntu",
  "ttl": "30m0s"
}
"@

但是命令没有正确解析选项。

Failed to parse K=V data: invalid key/value pair "-@\n{\n  allow_user_certificates: true,\n  allowed_users: *,\n  default_extensions: [\n    {\n      permit-pty: \n": format must be key=value

我通过要求 vault 从 JSON 文件中读取选项,找到了使用 PowerShell 运行 我的命令的方法。

vault write ssh/roles/my-role "@my-role.json";

但这并没有回答原来的问题。

这是 echoargs 的输出。这样好像不行。

echoargs -@"
{
  "allow_user_certificates": true,
  "allowed_users": "*",
  "default_extensions": [
    {
      "permit-pty": ""
    }
  ],
  "key_type": "ca",
  "default_user": "ubuntu",
  "ttl": "30m0s"
}
"@


Arg 0 is <-@
{
  allow_user_certificates: true,
  allowed_users: *,
  default_extensions: [
    {
      permit-pty:
>
Arg 1 is <}
>
Arg 2 is <],
>
Arg 3 is <key_type:>
Arg 4 is <ca,
>
Arg 5 is <default_user:>
Arg 6 is <ubuntu,
>
Arg 7 is <ttl:>
Arg 8 is <30m0s
}
@>

如果您想解决这个问题,您可能必须将所有引号都反斜杠。如果您不能将 json 传送给它。

$myarg = @"
{
  "allow_user_certificates": true,
  "allowed_users": "*",
  "default_extensions": [
    {
      "permit-pty": ""
    }
  ],
  "key_type": "ca",
  "default_user": "ubuntu",
  "ttl": "30m0s"
}
"@ -replace '"','\"'


echoargs -$myarg

Arg 0 is <-{
  "allow_user_certificates": true,
  "allowed_users": "*",
  "default_extensions": [
    {
      "permit-pty": ""
    }
  ],
  "key_type": "ca",
  "default_user": "ubuntu",
  "ttl": "30m0s"
}>

您误解了 Bash 示例的工作原理,实际执行的命令是

vault write ssh-client-signer/roles/my-role -

其中 - 是从 stdin 读取值,<<"EOH" 是 heredoc 的开始。

对于 PowerShell 版本,您试图使用 herestring(不同于 heredoc),但由于 - 它被解释为裸字符串。 由于 PowerShell 缺少输入重定向,等效命令可能是

@"
{
  "allow_user_certificates": true,
  "allowed_users": "*",
  "default_extensions": [
    {
      "permit-pty": ""
    }
  ],
  "key_type": "ca",
  "default_user": "ubuntu",
  "ttl": "30m0s"
}
"@ | vault write ssh-client-signer/roles/my-role -

根据保管库处理数据的方式,也可以将其作为参数直接传递

vault write ssh-client-signer/roles/my-role @"
{
  "allow_user_certificates": true,
  "allowed_users": "*",
  "default_extensions": [
    {
      "permit-pty": ""
    }
  ],
  "key_type": "ca",
  "default_user": "ubuntu",
  "ttl": "30m0s"
}
"@