Skip to content

Instantly share code, notes, and snippets.

@Rugby-Ball
Created September 14, 2023 19:24
Show Gist options
  • Select an option

  • Save Rugby-Ball/ac98050011dd21940e6680cd0fa602a0 to your computer and use it in GitHub Desktop.

Select an option

Save Rugby-Ball/ac98050011dd21940e6680cd0fa602a0 to your computer and use it in GitHub Desktop.
For Azure, get inventory of items in a Subscription. #Utility #Public #Inventory #Azure
# Azure-Subscription-Inventory.ps1
<#
Description: For Azure, get inventory of items in a Subscription.
Written: Ed Walsh
PowerShell.Core tested: Yes
MS-Graph: No
Version: 1.0.0
Create Date: 9/14/2023
Revised Date: 9/14/2023
#>
$timestamp = get-date -format yyyyMMddHHmmss
$subfolder = if (($PSVersionTable.PSEdition) -eq "Core") { if ( $True -eq $iswindows ) { "\Documents\" } Else { "" } } Else { "\Documents\" }
$mydocuments = $home + $subfolder
$fileName = "Azure-Subscription-Inventory-" + [string]$timestamp + ".csv"
$filePath = Join-Path $mydocuments $fileName
# Use line below to login to Azure Account.
# connect-azAccount
$o = @()
Get-AzSubscription | ForEach-Object {
$subscriptionId = $_.Id
$subscriptionName = $_.Name
Set-AzContext -SubscriptionId $subscriptionId
Get-AzResourceGroup | ForEach-Object {
$resourceGroupName = $_.ResourceGroupName
Get-AzResource -ResourceGroupName $resourceGroupName | ForEach-Object {
if (!([string]::IsNullOrEmpty($_.Tags))) {
$tags = @()
$_.Tags.GetEnumerator() | ForEach-Object {
[string]$tags += $_.key + "=" + $_.value + "; "
}
}
else {
$tags = ""
}
$o += [PSCustomObject][ordered]@{
"Subscription-Name" = $subscriptionName;
"Resource-Group-Name" = $resourceGroupName;
"Resource-Name" = $_.Name;
"Location" = $_.Location;
"Resource-Type" = $_.ResourceType;
"ResourceID" = $_.ResourceID;
"Tags" = $tags
}
}
}
}
# $o | Out-GridView
$o | Export-Csv -NoTypeInformation -Path $filepath
Write-Output "Exported to: $filePath"
Write-Output "\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\|||||||////////////////////////////////////"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment