Archive
[Code Snippet] PowerShell | Azure DevOps | Query Variable Group using API
In Azure DevOps, Variable groups store values and secrets that you might want to be passed into a YAML pipeline or make available across multiple pipelines.
Variable Group is a collection of variables with Name and Value pair.
Below is my variable Group ‘BillingSystemDictionary’ with 3 variables in my DevOps organization ‘RajeevPentyala0011’ and project ‘Demo’ (These details are required in PowerShell script).
In DevOps pipeline scenarios, you may need to fetch the variables under a variable group. Following is the PowerShell script which calls DevOps API and fetch variables by variable group name.
# Form API authentication Header
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("Authorization", "Bearer $(System.AccessToken)")
$headers.Add("Content-Type", "application/json")
# Pass Variable Group Name and read variables
$variableGroupName = "BillingSystemDictionary"
# 'projectid' pattern is https://dev.azure.com/{organization_name}/{projectname}
$projectid = "https://dev.azure.com/RajeevPentyala0011/Demo"
$variableGroupGetUrl = "$projectid/_apis/distributedtask/variablegroups?groupName=$variableGroupName*&queryOrder=IdDescending&api-version=6.0-preview.2"
$queryResponseObject = Invoke-RestMethod -Uri $variableGroupGetUrl -Method GET -Headers $headers
# $queryResponseObject will be of type pscustomobject
# To fetch the variable value of "isactive"
$variableName = "isactive"
$variableValue = $queryResponseObject.value.variables.$variableName.value
Write-Host "$variableName value is $variableValue"
🙂
Azure DevOps | YAML | Could not load type ‘System.Management.Automation.PSSnapIn’
I got the following exception while executing a PowerShell script in my Azure DevOps pipeline step.
Import-Module : Could not load type 'System.Management.Automation.PSSnapIn' from assembly 'System.Management.Automation, Version=7.2.5.500,
Reason:
- I used following ‘pwsh‘ task in my YAML file to execute the PowerShell script.
steps:
- pwsh: |
<My Script>
env:
SYSTEM_ACCESSTOKEN: $(System.AccessToken)
displayName: 'My Task'
condition: succeeded()
- For unversed,
pwsh
is the cross-platform edition of PowerShell built on .NET Core / .NET 5+; by contrast,Âpowershell
is the executable name of the legacy Windows PowerShell edition (v5.1-), built on the Windows-only .NET Framework (v4.8-). - The issue is ”System.Management.Automation’ is built on .Net Framework and ‘pwsh‘ can’t be used in this case.
Fix:
- Replace pwsh task with powershell task in YAML file.
steps:
- powershell: |
<My Script>
env:
SYSTEM_ACCESSTOKEN: $(System.AccessToken)
displayName: 'My Task'
condition: succeeded()
🙂
Azure DevOps (ADO) | Pipelines |Â Publish and Access Build Artifacts from Staging Directory
In this article, Lets see how to Publish and Access Build Artifacts using ADO Pipeline.
What are Build Artifacts:
- Build artifacts are the files generated by your build.
- In ADO Pipelines, We can use Build.ArtifactStagingDirectory Predefined Variable, on the agent where any artifacts are copied to.
Below are the steps to Publish and Download the Build Artefacts in a ADO Pipeline.
Steps to Publish the Build Artefacts:
- Lets see how to Publish a file to Build Artifacts folder, using following ‘Power Platform Export Solution’ Task.
- In the below Task, I am exporting a Solution from CRM Instance and publishing as $(Build.ArtifactStagingDirectory)\$(SolutionName)_managed.zip by using the ‘Solution Output File’ property.
- In the above the ‘Solution Output File’ property, $(SolutionName) is my custom defined ‘Pipeline Variable’ contains Solution name.
- ‘ALM_Base’ is my Dynamics Instance Solution name.
- And $(Build.ArtifactStagingDirectory) is Predefined Variable.
- Once the pipeline runs successfully, you can download the published artefacts (i.e., Solution folder) as follows.
Steps to Download the Build Artefacts:
- Go to Pipeline Runs and select the last run.
- Click on the below highlighted link.
- Select the folder you want to download.
- We also have an option to commit these files to Git using ‘Command Line Script’ task.
🙂
Azure DevOps (ADO) | Pipeline failure | Failed to connect to Dataverse
One of my ADO pipelines ‘Power Platform Publish Customizations’ task failed with “Failed to connect to Dataverse” error.
Reason:
- ‘Power Platform Publish Customizations’ task’s ‘Authentication type’ was selected as ‘Username/password’ which does not have MFA support.
- MFA (Multi Factor Authentication) was enabled on the Environment, which I was trying to connect from the Pipeline.
- Since the ‘Power Platform Publish Customizations’ task’s ‘Authentication type’ was selected as ‘Username/password’ which does not support MFA, pipeline could not connect to the Dataverse environment.
Fix:
- Create an ‘Application User’ by completing App Registration in Azure Active Directory and grant a Security Role.
- In the ADO pipeline’s ‘Power Platform Publish Customizations’ task, select ‘Authentication type’ as ‘Service Principal’.
- Make sure the ‘Service Connection’ configured properly with Azure App Registration details (i.e., Tenant ID, Application ID and Client Secret).
- My connection name is ‘SP_ExpAugust21’ and details are as follows.
- Save and run the pipeline and it should work now.
🙂