Format output in single lines

The following code output is returning blank records as well as the records I’m looking for:

# Parameter prompting for Azure subscription
PARAM(
    [string] [Parameter(Mandatory = $True, HelpMessage = "Choose subscription you want be inventoried")] $SubscriptionName
    )

#Login to Azure
Connect-AzAccount
 
#Select Azure Subscription
Get-AzSubscription -SubscriptionName $SubscriptionName | Select-AzSubscription

$rg = Get-AzResourceGroup | ?{ $_.ProvisioningState -eq "succeeded" } | select -expandproperty resourcegroupname
$results = 
foreach ($resourcegroup in $rg)
{
    $azResource = Get-AzResource -ResourceGroupName $resourcegroup | ? {$_.Type -match "Microsoft.Sql"}

    [PSCustomObject]@{
                     #Subscription  = $SubscriptionName
                     ResourceGroup = $azResource.ResourceGroupName
                     ResourceName  = $azResource.Name
                     ResourceType  = $azResource.ResourceType
                     #Location      = $azResource.Location
                     }
}

# Return the results in table format
$results

However, the output is returning bunched up as well as blank lines:

ResourceGroup                                                    ResourceName
-------------                                                    ------------


{test1, test1, test1, test1...}                                  {sim-data, sim-data/dac-sta, sim-data/master, sim-data/ddd-ddd...}
{test2, test2, test2, test2...}                                  {tst-data, tst-data/dac-sta, tst-data/master, tst-data/ddd-ddd...}

I’d like for it return in tabular format but a line for each resource?

Thanks!

If the variable $azResource is an array and you should use another inner loop inside your existing loop to access the individual elements. You may rename the to $azResourceGroup. Then you could actually keep the PSCustomObject creation inside the newly created loop. :wink:

That was it! Thx!

Here’s the new code:

t# Parameter prompting for Azure subscription
PARAM(
    [string] [Parameter(Mandatory = $True, HelpMessage = "Choose subscription you want be inventoried")] $SubscriptionName
    )

#Login to Azure
Connect-AzAccount
 
#Select Azure Subscription
Get-AzSubscription -SubscriptionName $SubscriptionName | Select-AzSubscription

$rg = Get-AzResourceGroup | ?{ $_.ProvisioningState -eq "succeeded" } | select -expandproperty resourcegroupname
$results = 
foreach ($resourcegroup in $rg)
{
    $azResourceGroup = Get-AzResource -ResourceGroupName $resourcegroup | ? {$_.Type -match "Microsoft.Sql"}
    foreach ($azResource in $azResourceGroup) 
    {
    [PSCustomObject]@{
                     #Subscription  = $SubscriptionName
                     ResourceGroup = $azResource.ResourceGroupName
                     ResourceName  = $azResource.Name
                     ResourceType  = $azResource.ResourceType
                     #Location      = $azResource.Location
                     }
    }
}

# Return the results in table format
$results | ft

Great. I’m glad to hear that it helped. :slightly_smiling_face:

Thanks for sharing. :+1:t4: :love_you_gesture:t4: