Skip to content

Commit e12ed61

Browse files
committed
feat: azuer get all subs inventory
1 parent f487eee commit e12ed61

3 files changed

Lines changed: 103 additions & 0 deletions

File tree

‎scripts/azure/.gitignore‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
azure_full_inventory**/**

‎scripts/azure/.tool-versions‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
python 3.11.9
2+
azure-cli 2.84.0
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
#!/usr/bin/env bash
2+
# inventory all subscriptions you have access to (very common in enterprises)
3+
4+
set -euo pipefail
5+
6+
command -v jq >/dev/null || { echo "Install jq: sudo apt install jq"; exit 1; }
7+
command -v timeout >/dev/null || { echo "Install coreutils: sudo apt install coreutils"; exit 1; }
8+
9+
output_dir="azure_full_inventory_$(date +%Y%m%d_%H%M%S)"
10+
mkdir -p "$output_dir"
11+
12+
echo "Collecting enabled subscriptions..."
13+
subscriptions=$(az account list --query '[?state==`Enabled`].{name:name, id:id}' --output tsv)
14+
15+
[ -z "$subscriptions" ] && { echo "No enabled subs."; exit 1; }
16+
17+
total_resources=0
18+
processed=0
19+
skipped_empty=0
20+
21+
echo "Found:"
22+
echo "$subscriptions" | awk '{print " - " $1 " (" $2 ")"}'
23+
24+
echo ""
25+
echo "Output → $output_dir/"
26+
echo "────────────────────────────────────────"
27+
28+
while IFS=$'\t' read -r sub_name sub_id; do
29+
((++processed))
30+
echo ""
31+
echo "[$processed] $sub_name ($sub_id)"
32+
33+
safe_name=$(echo "$sub_name" | tr -s '[:space:]' '_' | tr -cd '[:alnum:]_-')
34+
output_file="${output_dir}/inventory_${safe_name}_${sub_id:0:8}.csv"
35+
err_log="${output_file}.err"
36+
37+
echo " Checking resource count (with retries)..."
38+
39+
count=""
40+
attempts=0
41+
max_attempts=3
42+
43+
while [ $attempts -lt $max_attempts ] && [ -z "$count" ]; do
44+
((++attempts))
45+
echo " Attempt $attempts/$max_attempts..."
46+
count=$(az resource list --subscription "$sub_id" --query 'length([])' --output tsv) && break
47+
echo " → Failed or hung on attempt $attempts. Retrying in 10s..."
48+
sleep 10
49+
done
50+
51+
if [ -z "$count" ]; then
52+
echo " → Count query failed after $max_attempts attempts. Skipping."
53+
az resource list --subscription "$sub_id" --query 'length([])' --output tsv --debug > "${err_log}.debug" 2>&1
54+
echo " Debug saved to ${err_log}.debug"
55+
continue
56+
fi
57+
58+
echo " → Count result: $count"
59+
60+
if [ "$count" = "0" ]; then
61+
echo " → 0 resources → skipping."
62+
((++skipped_empty))
63+
continue
64+
fi
65+
66+
echo " Exporting full list (timeout 180s)..."
67+
json_temp="${output_file}.json.tmp"
68+
69+
if ! timeout 180 az resource list \
70+
--subscription "$sub_id" \
71+
--query '[].{Name:name, Type:type, ResourceGroup:resourceGroup, Location:location, SubscriptionId:subscriptionId, ID:id}' \
72+
--output json > "$json_temp" 2>"$err_log"; then
73+
echo " → Export timed out/failed (see $err_log)."
74+
rm -f "$json_temp"
75+
continue
76+
fi
77+
78+
if [ -s "$json_temp" ]; then
79+
jq -r 'if length == 0 then empty else (.[0] | keys_unsorted | join(",")), (.[] | [.[]] | join(",")) end' \
80+
< "$json_temp" > "$output_file" 2>>"$err_log"
81+
82+
if [ -s "$output_file" ]; then
83+
line_count=$(wc -l < "$output_file")
84+
res_count=$((line_count - 1))
85+
total_resources=$((total_resources + res_count))
86+
echo " → $res_count resources exported"
87+
else
88+
echo " → jq failed or empty output"
89+
fi
90+
rm -f "$json_temp"
91+
else
92+
echo " → No data returned (check $err_log)"
93+
fi
94+
95+
done <<< "$subscriptions"
96+
97+
echo ""
98+
echo "────────────────────────────────────────"
99+
echo "Processed $processed subs | Skipped $skipped_empty empty subs | Total resources: $total_resources"
100+
echo "Files saved in: $output_dir/"

0 commit comments

Comments
 (0)