如何将此 Bash 脚本转换为 PowerShell

发布于 2025-01-09 23:43:29 字数 357 浏览 1 评论 0原文

在 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}'

我怎样才能开始呢?

In Terraform I want to check if a resource group already exists or not. Currently I am using a Bash script to check this but this obviously won't work on Windows. My plan is to convert this script to ps1.

I have little to no experience with PowerShell so I have no clue how to convert it to ps1.

This is the Bash file:

#!/bin/bash 

eval "$(jq -r '@sh "GROUP_NAME=\(.group_name)"')"
result=$(az group exists -n $GROUP_NAME)

jq -n --arg exists "$result" '{"exists":$exists}'

How can I make a start on this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

故事灯 2025-01-16 23:43:29

这是与 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

Here is the PowerShell equivalent of your bash script, using only native PowerShell features; place the code in a .ps1 file, say foo.ps1, and pipe the JSON input of interest to it; e.g.:
'{ "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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文