Get Azure resources details using PowerShell

Hello scripters,

Today I want to share with you my script for gathering Azure resources details.
Script is connecting to Azure subscription and take all resources base on resource groups names provided as input parameter.
In the result table with Azure resources details such as Resource Group, Resource Name and Resource Type will be created and expoted to CSV file.
You can define folder to which CSV file should be exported by providing CSV Folder parameter.

Final script:

    [Parameter(Mandatory=$true, Position=1, HelpMessage = "Comma separated list of resource groups", ValueFromPipeline = $false)]
    [Parameter(Mandatory=$false, Position=2, HelpMessage = "CSV export folder", ValueFromPipeline = $false)]

$AzureContext = Get-AzureRmContext
    Write-Host "Please login to your Azure Account"

# Select Azure subscription
Write-Host "Please select Azure subscription and click OK"
$AzureSubscription = (Get-AzureRmSubscription | Out-GridView -Title "Choose Azure subscription and click OK" -PassThru)
Write-Output "Switching to Azure subscription: $($AzureSubscription.Name)"
Select-AzureRmSubscription -SubscriptionId $AzureSubscription.Id

$OutputArray = @()
$ResourceGroups = $ResourceGroups.Replace(' ','')
$resourceGroupsArray = $ResourceGroups.Split(',')

foreach($rg in $resourceGroupsArray){
   $resourceGroupExist = Get-AzureRmResourceGroup -Name $rg -ErrorAction SilentlyContinue
    $rgArray = Get-AzureRmResource -ResourceGroupName $rg | select Name, ResourceType, ResourceGroupName
    foreach($resource in $rgArray){
        $Object = New-Object PSObject -Property ([ordered]@{ 
                "Resource Group"   = $rg
                "Resource Name"    = $resource.Name
                "Resource Type"    = $resource.ResourceType
        $OutputArray += $Object
       Write-Host "Provided resource group name $rg does not exist in current subscription $($AzureSubscription.Id)" -ForegroundColor Red
Write-Host "Exporting results to CSV file..."
$Date = Get-Date -Format yyyyMMddmmHHss
$CSVName = $Environment+"_"+$Name+"_"+$Date
    $checkCSVFolder = Test-Path $CSVFolder
        $Path = (Get-Item $CSVFolder).Target
        $Path = (Get-Location).Path
$ExportPath = "$Path\$CSVName"
    $OutputArray | export-csv $ExportPath -NoTypeInformation
    Write-Host "Error during CSV export. Error: $($_.Exception.Message)" -ForegroundColor Red

Write-Host "CSV file has been exported to $ExportPath" -ForegroundColor Green

I hope that it will be usefull for some of you 🙂

One thought on “Get Azure resources details using PowerShell

  1. Hi There,
    We need to add [] in param [string[]]$ResourceGroups to accept multiple values.
    Also It would be great if you like to use begin{} process{} and end{} block in script.
    This will make it more redundant.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.