-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathGet-AzureInventory.ps1
More file actions
64 lines (57 loc) · 2.35 KB
/
Get-AzureInventory.ps1
File metadata and controls
64 lines (57 loc) · 2.35 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
<#
.SYNOPSIS
Get an inventory of your Azure environment
.DESCRIPTION
The script exports your Azure inventory as a CSV file. It builds different CSV files for each subscription with resources.
.EXAMPLE
PS C:\> .\Get-AzureEnvironment.ps1
Gets all resources in all available subscriptions and exports them to CSV files
.EXAMPLE
PS C:\> .\Get-AzureEnvironment.ps1 -SubscriptionId <subscription-id>
Gets all resources in a particular subscription and exports them to CSV files
.PARAMETER SubscriptionId
(optional) Specifies the subscription.
.LINK
https://github.com/cloudchristoph/AzureInventory
#>
[CmdletBinding()]
param (
# (optional) Specifies the subscription.
[Parameter()]
[string]$SubscriptionId = ""
)
Write-Verbose -Message "Getting subscriptions"
try {
if ($SubscriptionId.Length -eq 0) {
$subscriptions = Get-AzSubscription
} else {
$subscriptions = Get-AzSubscription -SubscriptionId $SubscriptionId
}
}
catch [Microsoft.Open.Azure.AD.CommonLibrary.AadNeedAuthenticationException] {
Write-Warning -Message "You're not connected. Connecting now."
Connect-AzAccount
if ($SubscriptionId.Length -eq 0) {
$subscriptions = Get-AzSubscription -ErrorAction Stop
} else {
$subscriptions = Get-AzSubscription -SubscriptionId $SubscriptionId -ErrorAction Stop
}
}
Write-Verbose -Message "Found $($subscriptions.Length) subscription(s)"
$subscriptions | Export-Csv -Path "./subscriptions.csv"
foreach ($subscription in $subscriptions) {
$pathInventory = "./inventory_$($subscription.Id).csv"
$pathMarketplace = "./marketplace_$($subscription.Id).csv"
Write-Verbose -Message "Getting resources from subscription $($subscription.Name)"
Select-AzSubscription -SubscriptionObject $subscription
$ressources = Get-AzResource | Select-Object ResourceName, ResourceGroupName, ResourceType, Sku
if ($ressources.Length -gt 0) {
Write-Verbose -Message "$($ressources.Length) resources found"
$ressources | Export-Csv -Path $pathInventory
$marketplaceItems = $ressources | Where-Object { $_.ResourceType -notlike "Microsoft.*" }
if ($marketplaceItems.Length -gt 0) {
Write-Verbose -Message "$($marketplaceItems.Length) marketplace resources found"
$marketplaceItems | Export-Csv -Path $pathMarketplace
}
}
}