-
Notifications
You must be signed in to change notification settings - Fork 0
/
azure.sh
executable file
·79 lines (66 loc) · 1.92 KB
/
azure.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#!/bin/sh
set -euo pipefail
export AZURE_CORE_NO_COLOR=1
export AZURE_CORE_ONLY_SHOW_ERRORS=1
MANAGEMENT_GROUPS=""
SUBSCRIPTIONS=""
while getopts "hs:m:" opt; do
case $opt in
s)
SUBSCRIPTIONS="${SUBSCRIPTIONS} ${OPTARG}"
;;
m)
MANAGEMENT_GROUPS="${MANAGEMENT_GROUPS} ${OPTARG}"
;;
h)
echo "Usage: $0 [-s subscription] [-m management_group]"
echo " -s subscription Limit to this subscription (can be used multiple times)"
echo " -m management_group Limit to this management group (can be used multiple times)"
echo " -h Show this help"
exit 0
;;
\?)
exit 1
;;
:)
exit 1
;;
esac
done
if ! command -v az >/dev/null; then
echo "This script requires azure-cli"
exit 1
fi
if ! command -v jq >/dev/null; then
echo "This script requires jq"
exit 1
fi
if ! az extension show --name resource-graph &>/dev/null; then
echo "This script requires the Azure resource-graph extension"
echo "You can install it by running:"
echo " az extension add --name resource-graph"
exit 1
fi
if [ -n "${MANAGEMENT_GROUPS}" ] && [ -n "${SUBSCRIPTIONS}" ]; then
echo "You can only specify one of -s or -m"
exit 1
fi
management_groups_flag=""
if [ -n "${MANAGEMENT_GROUPS}" ]; then
management_groups_flag="--management-group ${MANAGEMENT_GROUPS}"
fi
subscriptions_flag=""
if [ -n "${SUBSCRIPTIONS}" ]; then
subscriptions_flag="--subscriptions ${SUBSCRIPTIONS}"
fi
echo "type,kind,count"
skip_token=""
while true
do
resources="$(az graph query -q 'Resources | summarize count() by type, kind | sort by type asc, kind asc' --first 1000 --skip-token "$skip_token" ${management_groups_flag} ${subscriptions_flag})"
echo "$resources" | jq -re '.data | .[] | [.type, .kind, .count_] | @csv'
skip_token="$(echo "$resources" | jq -re '.skip_token // empty')"
if [ -z "$skip_token" ]; then
break
fi
done