如何将此 Bash 脚本转换为 PowerShell
How to convert this Bash script to PowerShell
在 Terraform 中,我想检查资源组是否已经存在。目前我正在使用 Bash 脚本来检查这一点,但这显然不适用于 Windows。我的计划是将此脚本转换为 ps1.
我几乎没有使用 PowerShell 的经验,所以我不知道如何将它转换为 ps1。
这是 Bash 文件:
#!/bin/bash
eval "$(jq -r '@sh "GROUP_NAME=\(.group_name)"')"
result=$(az group exists -n $GROUP_NAME)
jq -n --arg exists "$result" '{"exists":$exists}'
我该如何开始?
这是您的 bash
脚本的等效 PowerShell,仅使用本机 PowerShell 功能;将代码放在 .ps1
文件中,比如说 foo.ps1
,然后将感兴趣的 JSON 输入通过管道传递给它;例如:
'{ "group_name": "foo" }' | .\foo.ps1
# Extract the .group_name property value from the JSON input
# provided via the pipeline and save it in variable $GROUP_NAME
$GROUP_NAME = ($Input | ConvertFrom-Json).group_name
# Call the Azure CLI with the group name as the argument.
$result = az group exists -n $GROUP_NAME
# Construct a hashtable with an 'exists' entry that contains the
# result obtained from Azure and convert it to pretty-printed JSON
# (use -Compress to get single-line, non-pretty-printed output).
# Note: Unlike jq's output, the text will *not* be colored (syntax-highlighted).
@{ exists = $result } | ConvertTo-Json
在 Terraform 中,我想检查资源组是否已经存在。目前我正在使用 Bash 脚本来检查这一点,但这显然不适用于 Windows。我的计划是将此脚本转换为 ps1.
我几乎没有使用 PowerShell 的经验,所以我不知道如何将它转换为 ps1。
这是 Bash 文件:
#!/bin/bash
eval "$(jq -r '@sh "GROUP_NAME=\(.group_name)"')"
result=$(az group exists -n $GROUP_NAME)
jq -n --arg exists "$result" '{"exists":$exists}'
我该如何开始?
这是您的 bash
脚本的等效 PowerShell,仅使用本机 PowerShell 功能;将代码放在 .ps1
文件中,比如说 foo.ps1
,然后将感兴趣的 JSON 输入通过管道传递给它;例如:
'{ "group_name": "foo" }' | .\foo.ps1
# Extract the .group_name property value from the JSON input
# provided via the pipeline and save it in variable $GROUP_NAME
$GROUP_NAME = ($Input | ConvertFrom-Json).group_name
# Call the Azure CLI with the group name as the argument.
$result = az group exists -n $GROUP_NAME
# Construct a hashtable with an 'exists' entry that contains the
# result obtained from Azure and convert it to pretty-printed JSON
# (use -Compress to get single-line, non-pretty-printed output).
# Note: Unlike jq's output, the text will *not* be colored (syntax-highlighted).
@{ exists = $result } | ConvertTo-Json