简体   繁体   中英

How to convert this Bash script to PowerShell

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?

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; eg:
'{ "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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM