Share via

powershell script is not working as expect

Pradeep Khantwal 60 Reputation points
2024-10-28T16:37:01.5233333+00:00

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

$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

Create an array to hold the Logic App details

$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"

Windows for business | Windows Server | User experience | PowerShell
0 comments No comments

1 answer

Sort by: Most helpful
  1. Rich Matheisen 48,116 Reputation points
    2024-10-28T18:57:36.5433333+00:00

    I expect you may be getting an error when you try to add the second PSCustomObject to the $logicAppsList. If that's correct, you need to strongly type the variable as an [array].

    If you don't, the first iteration of the inner foreach loop succeeds, but $logicAppsList then becomes a single PSCustomObject. The second iteration fails because the first PSCustomObject (the one in the $logicAppsList variable) doesn't understand the "+" operator.

    Was this answer helpful?


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.