Powershell script is not working as expect output

I wanted to export logic app which are associated with ISE(Integration Service Environment). But the below snippet is not working as expect.

Define your subscription and ISE name

$SubscriptionId = “”
$ISEName = “” # Replace with your ISE name
$outputFilePath = “C:\Logic\DevISELogicApps.csv” # Specify your desired output file path

Connect to Azure

Connect-AzAccount
Set-AzContext -SubscriptionId $SubscriptionId

Strongly type the variable as an array to hold Logic App details

[Array]$logicAppsList = @()

Get all Resource Groups in the subscription

$resourceGroups = Get-AzResourceGroup

foreach ($rg in $resourceGroups) {
# Get all Logic Apps in the current Resource Group
$logicApps = Get-AzResource -ResourceGroupName $rg.ResourceGroupName -ResourceType “Microsoft.Logic/workflows”

foreach ($logicApp in $logicApps) {
    # Get Logic App details to check ISE association
    $logicAppDetails = Get-AzLogicApp -ResourceGroupName $rg.ResourceGroupName -Name $logicApp.Name

    # Filter Logic Apps deployed in the specified ISE
    if ($logicAppDetails.IntegrationServiceEnvironment -and $logicAppDetails.IntegrationServiceEnvironment.Name -eq $ISEName) {
        # Add to the list if it matches the ISE
        $logicAppsList += [pscustomobject]@{
            LogicAppName   = $logicApp.Name
            ResourceGroup  = $rg.ResourceGroupName
            ISEEnvironment = $ISEName
        }
    }
}

}

Export the results to CSV

$logicAppsList | Export-Csv -Path $outputFilePath -NoTypeInformation -Encoding UTF8

Write-Output “Export completed. File saved at $outputFilePath”

hi, can you please format all of your code as ‘code’ using the “preformatted text” button and then explain better what the problem is?

I wanted to export logic app which are associated with DEV ISE(Integration Service Environment in azure) Environment subscription.

But the below snippet is not working as expect.

Define your subscription and ISE name
$SubscriptionId = “”
$ISEName = “” # Replace with your ISE name
$outputFilePath = “C:\Logic\DevISELogicApps.csv” # Specify your desired output file path

Connect to Azure
Connect-AzAccount
Set-AzContext -SubscriptionId $SubscriptionId

Strongly type the variable as an array to hold Logic App details
[Array]$logicAppsList = @()

Get all Resource Groups in the subscription
$resourceGroups = Get-AzResourceGroup

foreach ($rg in $resourceGroups) {
# Get all Logic Apps in the current Resource Group
$logicApps = Get-AzResource -ResourceGroupName $rg.ResourceGroupName -ResourceType “Microsoft.Logic/workflows”

foreach ($logicApp in $logicApps) {
    # Get Logic App details to check ISE association
    $logicAppDetails = Get-AzLogicApp -ResourceGroupName $rg.ResourceGroupName -Name $logicApp.Name

    # Filter Logic Apps deployed in the specified ISE
    if ($logicAppDetails.IntegrationServiceEnvironment -and $logicAppDetails.IntegrationServiceEnvironment.Name -eq $ISEName) {
        # Add to the list if it matches the ISE
        $logicAppsList += [pscustomobject]@{
            LogicAppName   = $logicApp.Name
            ResourceGroup  = $rg.ResourceGroupName
            ISEEnvironment = $ISEName
        }
    }
}
}

Export the results to CSV
$logicAppsList | Export-Csv -Path $outputFilePath -NoTypeInformation -Encoding UTF8

Write-Output “Export completed. File saved at $outputFilePath”

rather than go round and round, here is your code with correct quotes, comment lines returned and correctly formatted for the forum:

#Define your subscription and ISE name
$SubscriptionId = ""
$ISEName = "" # Replace with your ISE name
$outputFilePath = "C:\Logic\DevISELogicApps.csv" # Specify your desired output file path

#Connect to Azure
Connect-AzAccount
Set-AzContext -SubscriptionId $SubscriptionId

#Get all Resource Groups in the subscription
$resourceGroups = Get-AzResourceGroup

foreach ($rg in $resourceGroups) {
    # Get all Logic Apps in the current Resource Group
    $logicApps = Get-AzResource -ResourceGroupName $rg.ResourceGroupName -ResourceType "Microsoft.Logic/workflows"

    $logicAppsList = foreach ($logicApp in $logicApps) {
        # Get Logic App details to check ISE association
        $logicAppDetails = Get-AzLogicApp -ResourceGroupName $rg.ResourceGroupName -Name $logicApp.Name

        # Filter Logic Apps deployed in the specified ISE
        if ($logicAppDetails.IntegrationServiceEnvironment -and $logicAppDetails.IntegrationServiceEnvironment.Name -eq $ISEName) {
            # Add to the list if it matches the ISE
            [pscustomobject]@{
                LogicAppName   = $logicApp.Name
                ResourceGroup  = $rg.ResourceGroupName
                ISEEnvironment = $ISEName
            }
        }
    }
}

#Export the results to CSV
$logicAppsList | Export-Csv -Path $outputFilePath -NoTypeInformation -Encoding UTF8

Write-Output "Export completed. File saved at $outputFilePath"

The one thing I changed is I removed the array definition and put the $logAppsList variable before the inner foreach loop to capture the PSCustomObjects if they’re created.

If the end result is that you have nothing exported to a CSV then I suspect there’s something not working as expected in these loops. I cannot test this code in any of my environments so I can’t help you there, but what I’d suggest is to use your IDE and manually iterate through your code.

$resourceGroups = Get-AzResourceGroup

do this, then inspect that variable, see how many resource groups there are. Pick one and define $rg as that, then step in to the next part:

$logicApps = Get-AzResource -ResourceGroupName $rg.ResourceGroupName -ResourceType "Microsoft.Logic/workflows"

Then inspect $logicApps and see what’s returned in there, and so on, until you can manually verify that the results you want are present, or not.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.