Changing Connection References in Power Automate using PowerShell

Loading

In Power Automate, connections represent authenticated links to external services such as SharePoint, OneDrive, SQL databases, and other Microsoft and third-party services. Sometimes, you may need to change the reference of a connection in a flow, especially if the original connection is invalid, expired, or if you’re migrating to a new service account or different credentials.

PowerShell provides a way to manage and automate these tasks, although modifying connection references in Power Automate flows directly through PowerShell can be complex. However, using the PowerApps and Power Automate cmdlets, it’s possible to automate connection updates within flows.

This guide will walk you through the steps of changing connection references in Power Automate using PowerShell.


Step 1: Install Required PowerShell Modules

Before managing Power Automate flows and connections, you must install the required PowerShell modules. These modules allow you to interact with the Power Platform and automate tasks such as modifying connection references.

Installing PowerShell Modules:

Install-Module -Name Microsoft.PowerApps.Administration.PowerShell -Force -AllowClobber
Install-Module -Name Microsoft.PowerApps.PowerShell -Force -AllowClobber

After installing the modules, you can authenticate and manage your Power Automate environment.


Step 2: Authenticate with Power Platform

Before executing any actions in Power Automate, you need to authenticate to your Power Platform account. Use the Add-PowerAppsAccount cmdlet to log in.

Authenticate:

Add-PowerAppsAccount

This will prompt you to enter your credentials, which should be associated with your Power Automate environment.


Step 3: List All Connections in Power Automate

To change a connection reference in Power Automate, you first need to identify which connections are being used in your flows. You can do this by listing all the connections in your environment.

List Connections:

$EnvironmentName = "<YourEnvironmentName>"  # Replace with your environment name
$Connections = Get-AdminConnection -EnvironmentName $EnvironmentName
$Connections | Select-Object DisplayName, ConnectionName, ConnectorId, State | Format-Table -AutoSize

This command will list all connections in your environment, showing the DisplayName, ConnectionName, ConnectorId, and State of each connection.


Step 4: Find the Flow That Uses the Connection

Once you know the connection you want to update, you need to identify the flows that are using this connection. Unfortunately, there isn’t a direct cmdlet to list the connections used by a specific flow in Power Automate, but you can export the flow’s details and manually search for the connection reference.

Export Flow Details:

$FlowName = "<YourFlowName>"  # Replace with your flow's name
$FlowDetails = Get-Flow -EnvironmentName $EnvironmentName -FlowName $FlowName
$FlowDetails | Select-Object -Property Name, Definition | Format-Table -AutoSize

The Definition property of the flow contains the flow’s JSON schema, which includes all the references to connections.


Step 5: Update the Connection Reference in the Flow

To update a connection reference in a flow, you typically need to update the connection ID used within the flow’s definition. Power Automate flows store connection references as part of their JSON definitions, and modifying the connection references requires altering these definitions.

Modify Connection Reference:

To automate this, you’ll need to follow these steps:

  1. Export the flow’s definition using PowerShell (as shown in Step 4).
  2. Find the connection reference in the flow’s JSON.
  3. Update the connection reference to point to the new connection ID.
  4. Re-import the updated flow.

Example of Updating the Flow Definition:

# Replace this with the actual Flow ID and your connection details
$FlowId = "<YourFlowId>"
$OldConnectionReference = "<OldConnectionReference>" # Old connection ID to replace
$NewConnectionReference = "<NewConnectionReference>" # New connection ID to use

# Export the flow's definition
$FlowDefinition = Get-FlowDefinition -EnvironmentName $EnvironmentName -FlowName $FlowId

# Update the connection reference in the definition (this is an example; modify according to your flow's JSON structure)
$UpdatedFlowDefinition = $FlowDefinition.Definition -replace $OldConnectionReference, $NewConnectionReference

# Update the flow with the new definition
Set-FlowDefinition -EnvironmentName $EnvironmentName -FlowName $FlowId -Definition $UpdatedFlowDefinition
Write-Host "Connection reference updated successfully."

In this script:

  • We retrieve the flow’s JSON definition using Get-FlowDefinition.
  • We replace the old connection reference with the new one in the flow’s definition.
  • We use Set-FlowDefinition to update the flow with the new connection reference.

Step 6: Reimport the Flow (If Necessary)

If you exported the flow’s definition manually or outside of PowerShell, you might need to re-import it after making the changes.

Import the Updated Flow:

Import-Flow -EnvironmentName $EnvironmentName -Path "C:\Path\To\Your\UpdatedFlow.zip"
Write-Host "Flow reimported successfully with updated connection references."

If you’re updating a flow that’s been exported, this command will re-import it into Power Automate with the updated connection references.


Step 7: Testing the Flow After Connection Update

After you update the connection reference, it’s important to test the flow to ensure that it’s working correctly with the new connection.

Test the Flow:

Start-FlowRun -EnvironmentName $EnvironmentName -FlowName $FlowId
Write-Host "Flow run started successfully."

Check the flow’s run history in the Power Automate portal to ensure that the flow is executing correctly with the new connection.


Step 8: Handle Errors and Rollback

If you encounter issues after updating the connection references, you may need to rollback to the previous version of the flow. You can do this by re-importing an older version or by manually reverting the connection references in the flow’s definition.

Rollback Example:

If you have the old flow definition saved, you can revert to it by re-importing the older version:

Import-Flow -EnvironmentName $EnvironmentName -Path "C:\Path\To\OldFlow.zip"
Write-Host "Flow rolled back to the previous version."

Leave a Reply

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