Skip to content

Commit

Permalink
new post
Browse files Browse the repository at this point in the history
  • Loading branch information
stowen-msft committed May 10, 2024
1 parent ff11601 commit cd76b28
Show file tree
Hide file tree
Showing 5 changed files with 202 additions and 0 deletions.
1 change: 1 addition & 0 deletions .github/workflows/jekyll.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ jobs:
run: bundle exec jekyll build --baseurl "${{ steps.pages.outputs.base_path }}"
env:
JEKYLL_ENV: production
exclude: [vendor]
- name: Upload artifact
# Automatically uploads an artifact from the './_site' directory by default
uses: actions/upload-pages-artifact@v3
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@

---
title: "How to query your Azure Devops Pipelines, using PowerShell"
date: "2024-05-08"
redirect_from : 2024/05/08/How-to-query-your-Azure-Devops-Pipelines-using-PowerShell
coverImage: \assets\images\2021\trackingStates.webp
categories:
- "scripting"
tags:

- "azuredevops"
- "devops"
- "pipelines"
excerpt: "In the digital realm of Azure DevOps, where pipelines flow as endlessly as the rivers of the fabled Babylon, we find ourselves in need of a divine intervention."
fileName: '2024-05-08-how-to-query-your-azure-devops-pipelines-using-powershell'
---
In the digital realm of Azure DevOps, where pipelines flow as endlessly as the rivers of the fabled Babylon, we find ourselves in need of a divine intervention.

![Header for this post, reads 'How To Make GitHub Button'](\assets\images\2021\trackingStates.webp)



How to query your Azure Devops Pipelines, using PowerShell

In the digital realm of Azure DevOps, where pipelines flow as endlessly as the rivers of the fabled Babylon, we find ourselves in need of a divine intervention.

Enter PowerShell, the Lord’s own programming language, ready to smite the overwhelming chaos of untracked appIDs and rogue variable values.

```powershell
# Arm yourself with the holy script
$AzureDevOpsPat = "p@ssw0rd" # Replace with your Personal Access Token
$headers = @{
'Authorization' = 'Bearer ' + $AzureDevOpsPat
}
$OrganizationName = "Heavenly-DevOps" # Replace with your Azure DevOps Organization Name
$ProjectName = "Creation" # Replace with your Azure DevOps Project Name
$FolderToSearch = "42" # The folder containign the pipelines we seek
#the variables which may contain the fields we seek
$VariablesToList = "ServicePrincipalUserName","service-principal-id"
$link = "https://dev.azure.com/{0}/{1}/_apis/pipelines?api-version=7.2-preview.1" `
-f $OrganizationName, $ProjectName
if ($data -eq $null){
$data = Invoke-RestMethod -Method 'GET' -Uri $link -Headers $headers
}
else{
"keeping last result"
}
$folderPipelines = $data.value | where folder -eq $FolderToSearch
foreach($a in $folderPipelines){
$values = $null
$thisPipeline = Invoke-RestMethod -Method 'GET' -Uri $a.url -Headers $headers
foreach($field in $VariablesToList){
$values += $thisPipeline.configuration.variables.$field.value
}
@{name=$thisPipeline.Name;SPId=[string]::Join(',', $values)}
}
```


### Why, you ask, would one embark on such a celestial quest?

Imagine, if you will, sifting through thousands of Azure DevOps Pipelines, each a potential Pandora’s box, just to see if any dare to use a particular appID or variable value.

It’s like finding a needle in a haystack, but fear not! With PowerShell, you’re not just any farmer looking at hay pieces by hand; instead, you’re a shepherd with a divine rod, parting the sea of data with ease and precision.

So, don your robes, grab your staff, and prepare to command the elements of Azure DevOps with the omnipotence of PowerShell.

For in this quest, it is not just about finding what is lost, but about mastering the cosmos of continuous integration and delivery.
122 changes: 122 additions & 0 deletions _posts/2024-05-09-troubleshooting-common-problems-in-azure-devops.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
---
title: "Troubleshooting common problems in Azure Devops"
date: "2024-05-09"
redirect_from : 2024/05/09/Troubleshooting-common-problems-in-Azure-Devops
coverImage: /assets/images/2024/AboveAllElseThyShallDoNoChangesOnFriday.webp
categories:
- "programming"
- "devops"
tags:
- "azuredevops"
- "devops"
- "pipelines"
excerpt: "I love orchestration and automation, believe me I do. But there is just something about understanding the vagaries and internal inconsistencies of these products that could break a lesser System Admin or Devops guru."
fileName: '2024-05-09-troubleshooting-common-problems-in-azure-devops'
---
I love orchestration and automation, believe me I do. But there is just something about understanding the vagaries and internal inconsistencies of these products that could break a lesser System Admin or Devops guru.

![Header for this post, reads 'How To Make GitHub Button'](../assets/images/2024/AboveAllElseThyShallDoNoChangesOnFriday.webp)

I love orchestration and automation, believe me I do. But there is just something about understanding the vagaries and internal inconsistencies of these products that could break a lesser System Admin or Devops guru.

For one thing, who wants to waste fifteen minutes reading the docs when they could take two whole days figuring it out instead?!

In this short post, I'll help you solve a few small problems you might encounter trying to use Azure PowerShell commands in your Azure Devops Pipeline.


**Topics Covered**
* Could not find the modules Az.Accounts

* ##[error] The term 'C:\__w\1\s' is not recognized as a name of a cmdlet, function

* 403 Forbidden error on using Azure PowerShell commands

* No output when running `AzurePowerShell@` commands in Azure Devops pipelines.

### If the module was recently installed, retry after restarting the Azure Pipelines task agent. ##[error]PowerShell exited with code '1'.

When you use the `AzurePowerShell` or other `Azure*` tasks in Azure Devops, you might expect that they will setup your modules for you. This is not in fact the case. What they *do* do for you is run `Initialize-Azure` for you before your command executes, which handles setting up your Azure credentials from your Service Connection, SPN, or MSI, and then runs `Connect-AzAccount` and `Set-AzContect`.

This means that before you can call *any* `Azure*` commands, you should first setup your PowerShell modules manually, using this command.

```yaml
- task: PowerShell@2
displayName: 'Update Az.Accounts Module'
inputs:
targetType: 'inline'
script: |
Get-PackageProvider -Name NuGet -ForceBootstrap
Install-Module -Name Az.Accounts -Verbose -Force -AllowClobber
Uninstall-AzureRm -Verbose
```
After this, the `AzurePowerShell` commands can work.

### The term 'C:\__w\1\s' is not recognized as a name of a cmdlet, functio

Let's say you setup your modules as above, and then go to run subsequent commands, and see this lovely error.

```
##[error]The term 'D:\a\1\s\myPowershellFile.ps1' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
```
This occurs because the `AzurePowerShell` commands have different parameters than the plain-jane `PowerShell` task!
Specifically, you'll need to **always specify `azurePowerShellVersion` and `azureSubscription` fields**. Fail to do this, and the default params will think you're using a script file and throw an error.
And that leads you directly into the next problem.
### No output when running an AzureDevops command
How about this! Your command executes without an error and **nothing happens**.
For instance, you might see this
```
2024-05-10T15:59:13.3300799Z VERBOSE: Command [Set-AzContext] succeeded.
2024-05-10T15:59:14.8584021Z ##[command]Disconnect-AzAccount -Scope CurrentUser -ErrorAction Stop
```
How lovely, it finishes executing `Set-AzContext` and then immediatly runs `Disconnect-AzAccount`. What's the deal!?
Well in a lovely turn of events, even more parameters are different in `AzurePowerShell`.
Specifically when using an inline script you must set your `ScriptType` to `'InlineScript'`, and then provide your actual script in `Inline: |` format.
The below example is known good and will work and save you much frustration.
```yaml
- task: AzurePowerShell@3
displayName: "Create Resource Group with version 3"
inputs:
azureSubscription: "putYourOwnServiceConnectionNameHere"
azurePowerShellVersion: latestVersion
ScriptType: 'InlineScript'
Inline: |
$dateTime = Get-Date
$formattedString = "FoxTest{0:MMddHHmm}v3" -f $dateTime
new-azresourcegroup -location eastus $formattedString -verbose
Write-output $formattedString
pwsh: true
```

The format of this command is noticibly different from a plain `PowerShell@2` command, so if you get no output from your `AzurePowerShell` commands, be sure to check this!

### I get a 403 Forbidden

Nothing like getting all the way here, to actually running your command and then you experience an error.

Well, remember my golden rule "Every new failure message gets you closer to success".

Actually that's my silver rule, the true golden rule is:

![1](../assets/images/2024/AboveAllElseThyShallDoNoChangesOnFriday.webp)

Well, if you're using a Service Connection to authenticate to Azure in your pipelines(and you should be), you may have [expected from the documents that your account would automatically be granted permissions to the relevant subscription](https://learn.microsoft.com/en-us/azure/devops/pipelines/release/azure-rm-endpoint?view=azure-devops#insufficient-privileges-to-complete-the-operation).

One thing I've found to check is to ensure your Service Connection account was actually granted permission to the target subscription.

In my case, my Azure operations were failing due to insufficient permissions, but once I manually applied Contributor permissions to my new Enterprise Application, the task completed on next operation!

![1](../assets/images/2024/newRgsCreated.png)

Binary file not shown.
Binary file added assets/images/2024/newRgsCreated.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit cd76b28

Please sign in to comment.