-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAzure-ResourceInventory.ps1
More file actions
273 lines (233 loc) · 9.13 KB
/
Azure-ResourceInventory.ps1
File metadata and controls
273 lines (233 loc) · 9.13 KB
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
<#
.SYNOPSIS
Azure resource inventory and cost analysis script
.DESCRIPTION
Generates comprehensive inventory of Azure resources with cost analysis,
identifies unused resources, and exports data for governance.
.PARAMETER SubscriptionId
Azure subscription ID to inventory
.PARAMETER ExportPath
Path to save CSV export (default: C:\Reports\Azure)
.EXAMPLE
.\Azure-ResourceInventory.ps1 -SubscriptionId "xxxx-xxxx-xxxx-xxxx"
.NOTES
Author: Naveen
Version: 1.0
Requires: Az PowerShell module
#>
[CmdletBinding()]
param(
[Parameter(Mandatory=$false)]
[string]$SubscriptionId,
[Parameter(Mandatory=$false)]
[string]$ExportPath = "C:\Reports\Azure",
[Parameter(Mandatory=$false)]
[string]$LogPath = "C:\Logs\AzureInventory"
)
# Create directories
foreach ($Path in @($ExportPath, $LogPath)) {
if (-not (Test-Path $Path)) {
New-Item -ItemType Directory -Path $Path -Force | Out-Null
}
}
$LogFile = Join-Path $LogPath "AzureInventory_$(Get-Date -Format 'yyyyMMdd_HHmmss').log"
$CsvFile = Join-Path $ExportPath "AzureResources_$(Get-Date -Format 'yyyyMMdd').csv"
# Logging function
function Write-Log {
param([string]$Message, [string]$Level = "INFO")
$Timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
$LogMessage = "[$Timestamp] [$Level] $Message"
Add-Content -Path $LogFile -Value $LogMessage
Write-Host $LogMessage
}
Write-Log "Starting Azure Resource Inventory"
# Check if Az module is installed
try {
if (-not (Get-Module -ListAvailable -Name Az.Accounts)) {
throw "Az PowerShell module not installed. Run: Install-Module -Name Az -AllowClobber"
}
Import-Module Az.Accounts -ErrorAction Stop
Write-Log "Az module loaded successfully"
} catch {
Write-Log "Failed to load Az module: $($_.Exception.Message)" -Level "ERROR"
exit 1
}
# Connect to Azure (uses cached credentials if available)
try {
$Context = Get-AzContext
if (-not $Context) {
Write-Log "No Azure context found. Initiating login..."
Connect-AzAccount -ErrorAction Stop | Out-Null
$Context = Get-AzContext
}
Write-Log "Connected to Azure as: $($Context.Account.Id)"
} catch {
Write-Log "Failed to connect to Azure: $($_.Exception.Message)" -Level "ERROR"
exit 1
}
# Set subscription if specified
if ($SubscriptionId) {
try {
Set-AzContext -SubscriptionId $SubscriptionId -ErrorAction Stop | Out-Null
Write-Log "Using subscription: $SubscriptionId"
} catch {
Write-Log "Failed to set subscription: $($_.Exception.Message)" -Level "ERROR"
exit 1
}
} else {
$SubscriptionId = $Context.Subscription.Id
Write-Log "Using current subscription: $SubscriptionId"
}
# Get subscription details
$Subscription = Get-AzSubscription -SubscriptionId $SubscriptionId
Write-Log "Subscription Name: $($Subscription.Name)"
# Initialize results array
$AllResources = @()
# Get all resources
Write-Log "Retrieving all resources..."
try {
$Resources = Get-AzResource
Write-Log "Found $($Resources.Count) resources"
} catch {
Write-Log "Failed to retrieve resources: $($_.Exception.Message)" -Level "ERROR"
exit 1
}
# Process each resource
$Counter = 0
foreach ($Resource in $Resources) {
$Counter++
Write-Progress -Activity "Processing Resources" -Status "$Counter of $($Resources.Count)" -PercentComplete (($Counter / $Resources.Count) * 100)
Write-Log "Processing: $($Resource.Name) ($($Resource.ResourceType))"
# Get resource tags
$Tags = if ($Resource.Tags) {
($Resource.Tags.GetEnumerator() | ForEach-Object { "$($_.Key)=$($_.Value)" }) -join "; "
} else {
"No tags"
}
# Determine resource status (varies by type)
$Status = "Running"
$AdditionalInfo = ""
# Check specific resource types for more details
switch -Wildcard ($Resource.ResourceType) {
"Microsoft.Compute/virtualMachines" {
try {
$VM = Get-AzVM -ResourceGroupName $Resource.ResourceGroupName -Name $Resource.Name -Status
$PowerState = ($VM.Statuses | Where-Object { $_.Code -like "PowerState/*" }).DisplayStatus
$Status = $PowerState
$AdditionalInfo = "Size: $($VM.HardwareProfile.VmSize)"
} catch {
Write-Log "Could not get VM status for $($Resource.Name)" -Level "WARN"
}
}
"Microsoft.Storage/storageAccounts" {
try {
$Storage = Get-AzStorageAccount -ResourceGroupName $Resource.ResourceGroupName -Name $Resource.Name
$AdditionalInfo = "SKU: $($Storage.Sku.Name), Kind: $($Storage.Kind)"
} catch {
Write-Log "Could not get storage details for $($Resource.Name)" -Level "WARN"
}
}
"Microsoft.Sql/servers" {
$AdditionalInfo = "SQL Server"
}
"Microsoft.Web/sites" {
try {
$WebApp = Get-AzWebApp -ResourceGroupName $Resource.ResourceGroupName -Name $Resource.Name
$AdditionalInfo = "Plan: $($WebApp.AppServicePlan), State: $($WebApp.State)"
$Status = $WebApp.State
} catch {
Write-Log "Could not get web app details for $($Resource.Name)" -Level "WARN"
}
}
}
# Create resource object
$ResourceObj = [PSCustomObject]@{
SubscriptionName = $Subscription.Name
SubscriptionId = $SubscriptionId
ResourceGroup = $Resource.ResourceGroupName
ResourceName = $Resource.Name
ResourceType = $Resource.ResourceType
Location = $Resource.Location
Status = $Status
Tags = $Tags
AdditionalInfo = $AdditionalInfo
ResourceId = $Resource.ResourceId
CreatedTime = "N/A" # Azure doesn't expose creation time easily
}
$AllResources += $ResourceObj
}
Write-Progress -Activity "Processing Resources" -Completed
# Generate statistics
Write-Log "========================================="
Write-Log "Inventory Summary"
Write-Log "Total Resources: $($AllResources.Count)"
$ResourcesByType = $AllResources | Group-Object ResourceType | Sort-Object Count -Descending
Write-Log "`nTop Resource Types:"
foreach ($Type in $ResourcesByType | Select-Object -First 10) {
Write-Log " $($Type.Name): $($Type.Count)"
}
$ResourcesByLocation = $AllResources | Group-Object Location | Sort-Object Count -Descending
Write-Log "`nResources by Location:"
foreach ($Location in $ResourcesByLocation) {
Write-Log " $($Location.Name): $($Location.Count)"
}
$ResourcesByRG = $AllResources | Group-Object ResourceGroup | Sort-Object Count -Descending
Write-Log "`nTop Resource Groups:"
foreach ($RG in $ResourcesByRG | Select-Object -First 5) {
Write-Log " $($RG.Name): $($RG.Count)"
}
# Identify resources without tags
$UntaggedResources = $AllResources | Where-Object { $_.Tags -eq "No tags" }
if ($UntaggedResources) {
Write-Log "`nWARNING: $($UntaggedResources.Count) resources have no tags" -Level "WARN"
}
# Identify stopped VMs
$StoppedVMs = $AllResources | Where-Object { $_.ResourceType -eq "Microsoft.Compute/virtualMachines" -and $_.Status -like "*deallocated*" }
if ($StoppedVMs) {
Write-Log "`nInfo: $($StoppedVMs.Count) VMs are deallocated (potential cost savings)"
}
Write-Log "========================================="
# Export to CSV
try {
$AllResources | Export-Csv -Path $CsvFile -NoTypeInformation -Force
Write-Log "Resource inventory exported to: $CsvFile"
} catch {
Write-Log "Failed to export CSV: $($_.Exception.Message)" -Level "ERROR"
}
# Generate HTML report
$HtmlReport = @"
<html>
<head>
<style>
body { font-family: Arial, sans-serif; }
h2 { color: #333; }
table { border-collapse: collapse; width: 100%; margin-top: 20px; font-size: 12px; }
th, td { border: 1px solid #ddd; padding: 8px; text-align: left; }
th { background-color: #4CAF50; color: white; }
.summary { background-color: #f0f0f0; padding: 15px; margin: 20px 0; }
.warning { background-color: #fff3cd; padding: 10px; margin: 10px 0; }
</style>
</head>
<body>
<h2>Azure Resource Inventory Report</h2>
<div class="summary">
<strong>Subscription:</strong> $($Subscription.Name)<br>
<strong>Date:</strong> $(Get-Date -Format "yyyy-MM-dd HH:mm:ss")<br>
<strong>Total Resources:</strong> $($AllResources.Count)<br>
<strong>Untagged Resources:</strong> $($UntaggedResources.Count)
</div>
"@
if ($StoppedVMs.Count -gt 0) {
$HtmlReport += "<div class='warning'><strong>⚠️ $($StoppedVMs.Count) VMs are currently deallocated</strong></div>"
}
$HtmlReport += "<h3>Resource Breakdown by Type</h3><table><tr><th>Resource Type</th><th>Count</th></tr>"
foreach ($Type in $ResourcesByType | Select-Object -First 15) {
$HtmlReport += "<tr><td>$($Type.Name)</td><td>$($Type.Count)</td></tr>"
}
$HtmlReport += "</table></body></html>"
$HtmlFile = Join-Path $ExportPath "AzureInventory_$(Get-Date -Format 'yyyyMMdd').html"
$HtmlReport | Out-File -FilePath $HtmlFile -Force
Write-Log "HTML report saved to: $HtmlFile"
Write-Log "Azure Resource Inventory Complete"
Write-Log "Log file: $LogFile"
exit 0