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:

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

$AzureContext = Get-AzureRmContext
if(!$AzureContext){
    Write-Host "Please login to your Azure Account"
    Login-AzureRmAccount
}

# 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
   if($resourceGroupExist){
    
    $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
    }
   }
   else{
       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
if($CSVFolder){
    $checkCSVFolder = Test-Path $CSVFolder
    if($checkCSVFolder){
        $Path = (Get-Item $CSVFolder).Target
    }
    else{
        $Path = (Get-Location).Path
    }
}
    
$ExportPath = "$Path\$CSVName"
Try{
    $OutputArray | export-csv $ExportPath -NoTypeInformation
}
Catch{
    Write-Host "Error during CSV export. Error: $($_.Exception.Message)" -ForegroundColor Red
    break
}


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

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

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.