Last active
November 6, 2023 08:46
-
-
Save withakay/83e471374f4b9d0ecab16be4acc174be to your computer and use it in GitHub Desktop.
azs - An Azure subscription switcher bash script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# A bash script to switch Azure subscriptions | |
# save this script as `azs` somewhere in your path and make it executable | |
# then you can run `azs` to see a list subscriptions and easily switch between them | |
show_help() { | |
echo "Usage: $0 [options]" | |
echo "" | |
echo "Options:" | |
echo " -v Verbose output. Show details of the selected Azure subscription." | |
echo " -a, --all Include all subscriptions, ignoring the exclude file." | |
echo " -h, --help Show this help message and exit." | |
echo "" | |
echo "This script selects an Azure subscription from a list and sets it as the current subscription." | |
echo "By default, it excludes subscriptions listed in ~/.azs/excluded." | |
echo "" | |
echo "The exclude file ~/.azs/excluded should contain one subscription name per line" | |
echo "that you want to exclude from the list. For example:" | |
echo "" | |
echo " Subscription1" | |
echo " Subscription2" | |
echo " Subscription3" | |
echo "" | |
} | |
# Initialize variables | |
VERBOSE=0 | |
INCLUDE_ALL=0 | |
# Process the input arguments | |
while getopts "vah-:" opt; do | |
case $opt in | |
v) | |
VERBOSE=1 | |
;; | |
a) | |
INCLUDE_ALL=1 | |
;; | |
h) | |
show_help | |
exit 0 | |
;; | |
-) | |
case "${OPTARG}" in | |
all) | |
INCLUDE_ALL=1 | |
;; | |
help) | |
show_help | |
exit 0 | |
;; | |
*) | |
echo "Invalid option: --$OPTARG" >&2 | |
exit 1 | |
;; | |
esac | |
;; | |
\?) | |
echo "Invalid option: -$OPTARG" >&2 | |
exit 1 | |
;; | |
esac | |
done | |
# Initialize the excluded subscriptions array if not including all | |
excluded_subscriptions=() | |
if [ $INCLUDE_ALL -eq 0 ] && [ -f "$HOME/.azs/excluded" ]; then | |
while IFS= read -r line; do | |
excluded_subscriptions+=("$line") | |
done < "$HOME/.azs/excluded" | |
fi | |
echo "Azure subscriptions:" | |
subscriptions=$(az account list --query "[].{Name:name, Id:id}" --output tsv) | |
if [ $? -ne 0 ]; then | |
echo "Failed to retrieve Azure subscriptions. Are you logged in?" | |
exit 1 | |
fi | |
# Store subscription names and IDs in separate arrays | |
names=() | |
ids=() | |
while IFS=$'\t' read -r name id; do | |
# Check if the subscription is not in the excluded list | |
if [[ ! " ${excluded_subscriptions[@]} " =~ " ${name} " ]]; then | |
names+=("$name") | |
ids+=("$id") | |
fi | |
done <<< "$subscriptions" | |
# Check if we got any subscriptions | |
if [ ${#names[@]} -eq 0 ]; then | |
echo "No Azure subscriptions found. Make sure you are logged. (az login)." | |
exit 1 | |
fi | |
echo "" # This will add the blank line before displaying the menu | |
# Use the select command to create a menu | |
PS3=$'\nPlease select the subscription you want to switch to: ' | |
select opt in "${names[@]}"; do | |
if [ 1 -le "$REPLY" ] && [ "$REPLY" -le ${#names[@]} ]; then | |
# Setting the subscription | |
az account set --subscription "${ids[$REPLY-1]}" | |
if [ $? -eq 0 ]; then | |
echo "" | |
echo "Switched to subscription '${opt}' (${ids[$REPLY-1]})" | |
# Check if verbose flag is set | |
if [ $VERBOSE -eq 1 ]; then | |
# Show detailed information of the selected subscription | |
echo "" | |
echo "Showing details of the selected subscription:" | |
echo "" | |
az account show --subscription "${ids[$REPLY-1]}" | |
fi | |
break | |
else | |
echo "Failed to switch to subscription '${opt}' (${ids[$REPLY-1]})" | |
exit 1 | |
fi | |
else | |
echo "Invalid option. Please try again." | |
fi | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment