Dynamics 365 CI/CD : Turn ON Power Automate (MS Flow) using PowerShell after Solution Import

Often, Power Automate (MS Flow) components goes into OFF mode, post solution import. Had to manually turn it on in this case, as post deployment step.

Especially, if you automated solution deployment setup (CD) in Azure DevOps using Pipelines, this PowerShell script would save your time.

Just a create a Powershell task and put it after solution import step.

$Username=’$(Username)’
$Passwrod=’$(Passwrod)’
$EnvironmentName=’$(EnvironmentName)’

try
{
$modulePowerAppAdministrator=’Microsoft.PowerApps.Administration.PowerShell’
$modulePowerAppAdministrator=’Microsoft.PowerApps.PowerShell’

if(!(Get-Module -ListAvailable -Name $modulePowerAppAdministrator))
{
Write-Host “Installing PowerAppsModule.”
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
Install-PackageProvider -Name NuGet -RequiredVersion 2.8.5.201 -Force
Install-Module -Name Microsoft.PowerApps.Administration.PowerShell -Force -Verbose -Scope CurrentUser
Install-Module -Name Microsoft.PowerApps.PowerShell -AllowClobber -Force -Verbose -Scope CurrentUser
Write-Host “Installed Successfully PowerAppsModule.”
}
else
{
Write-Host “Module already installed”
}

}
catch
{

Write-host $_.Exception.Message
}

try{
Write-Host “Connecting Target Instance of PowerApps.”
$pass = ConvertTo-SecureString $Passwrod -AsPlainText -Force
Add-PowerAppsAccount -Username $Username -Password $pass
Write-Host “Successfully Target Instance connected.”
}
catch
{
Write-host $_.Exception.Message
}

try{
Write-Host “Connecting Environment.”

$en=”*$EnvironmentName*”
$environmentName=Get-PowerAppEnvironment $en | Select -ExpandProperty EnvironmentName
Write-Host “Connected Environment.” $environmentName

Get-FlowEnvironment $environmentName
}
catch
{
Write-host $_.Exception.Message
}

$listOfMsFlow=”;
$listOfMsFlow=Get-AdminFlow -EnvironmentName $environmentName |where-object {!($_.Enabled -eq “True”) } | Select FlowName
$count=1;
foreach ($flow in $listOfMsFlow)
{

try{

Write-Host “Flow Name : ” $flow.FlowName $count

Enable-AdminFlow -EnvironmentName $environmentName -FlowName $flow.FlowName
#Disable-AdminFlow -EnvironmentName $environmentName -FlowName $flow.FlowName
# Write-Host “Turn Off.” $flow
$count=$count+1;
}
catch
{
Write-Host “Failed to Turn On.” $flow.FlowName
Write-host $_.Exception.Message
}

}

PS : On very first deployment of flows into new instance, you probably have to set up the connections.

 

Cheers