Last active
August 10, 2023 08:47
-
-
Save roccoren/f6f390a93313acf38bf7b1c205f879fc to your computer and use it in GitHub Desktop.
get-subscriptions-creation-time
This file contains hidden or 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 | |
if [ $# -eq 0 ]; then | |
echo "Usage: $0 subscription-id-1 subscription-id-2 ..." | |
exit 1 | |
fi | |
subscription_ids=("$@") | |
get_subscription_creation_time() { | |
# Azure, the land of hoop jumping to get trivial info | |
if [[ ! "$1" =~ [0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12} ]]; then | |
echo -e 'This requires a subscription ID! 36 chars, looks something like:\n' | |
echo -e "\t7f50be07-41f8-4202-b3c9-489394f26a1f" | |
echo "" | |
exit 2 | |
fi | |
# bearer token | |
beary="$HOME/.azure.token" | |
# if not within the last 10 mins | |
if [ ! -s "$beary" -o `stat -c %Y "$beary"` -lt $(( `date +%s` - 1800 )) ]; then | |
az account get-access-token --query "{accessToken:accessToken}" -o tsv > "$beary" | |
fi | |
URL="https://management.chinacloudapi.cn/subscriptions/$1/resources?api-version=2020-06-01&%24expand=createdTime&%24select=name%2CcreatedTime" | |
# #https://management.chinacloudapi.cn/subscriptions/$subscription/resources?api-version=2020-06-01&%24expand=createdTime&%24select=name%2CcreatedTime" | |
curl -s \ | |
-H "'Accept-Encoding': 'gzip, deflate'" \ | |
-H "'Accept': '*/*'" \ | |
-H "'Connection': 'keep-alive'" \ | |
-H "'CommandName': 'rest'" \ | |
-H "'ParameterSetName': '--url --url-parameters'" \ | |
-H "Authorization: Bearer $(cat $beary)" \ | |
"$URL" | sed -e 's/^.*createdTime":"//' -e 's/".*$//' | tail -n 1 | |
# grab the very last (oldest, I hope) createdTime ^^^^ | |
} | |
# Usage: get_subscription_creation_time <subscription-ID> | |
for sub_id in "${subscription_ids[@]}"; do | |
echo "Subscription ID: $sub_id" | |
get_subscription_creation_time "$sub_id" | |
echo -e "\n" | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment