gpt4 book ai didi

bash - 如何将此 Bash 脚本转换为 PowerShell

转载 作者:行者123 更新时间:2023-12-03 05:22:13 25 4
gpt4 key购买 nike

在 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,仅使用 native 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

关于bash - 如何将此 Bash 脚本转换为 PowerShell,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71266384/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com