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

Dynamics 365 CE S2S : Automate App Registration in Azure AD using PowerShell

Application user creation in Dynamics 365 CE requires an app registration in Azure AD, which is a lengthy process. You can automate it using PowerShell.

Launch PowerShell as Administrator in your local machine and run this script.

# Install AzureAD PS module in local m/c
Install-Module AzureAD

#Connect to Azure AD – Launches popup to enter username/password
Connect-AzureAD

#Get CDS object (shown as Dynamics CRM in Azure Portal)
$AzureMgmtPrincipal = Get-AzureADServicePrincipal -All $true | Where-Object {$_.DisplayName -eq "Common Data Service"}

#Create API permission to CDS
$AzureMgmtAccess = New-Object -TypeName "Microsoft.Open.AzureAD.Model.RequiredResourceAccess"
$AzureMgmtAccess.ResourceAppId = $AzureMgmtPrincipal.AppId
$AzureSvcMgmt = New-Object -TypeName "microsoft.open.azuread.model.resourceAccess" -ArgumentList $AzureMgmtPrincipal.Oauth2Permissions.Id, "Scope"
$AzureMgmtAccess.ResourceAccess = $AzureSvcMgmt

#Create App Registration – Update DisplayName value as needed
$App = New-AzureADApplication -DisplayName "Integration App2" -RequiredResourceAccess @($AzureMgmtAccess)

#Create App Secret
$AppSecret = New-AzureADApplicationPasswordCredential -ObjectId $App.ObjectId -CustomKeyIdentifier "Access Key" -EndDate (get-date).AddYears(1)

#Create managed application in local directory
$AppSPN = New-AzureADServicePrincipal -AppId $App.AppId -Tags @("WindowsAzureActiveDirectoryIntegratedApp")

Once the script runs successfully, you can fetch the Client ID and Secret from the script variables $App.AppId and $AppSecret respectively.

 

 

Hope this saves your time!!

Cheers