Skip to content

Instantly share code, notes, and snippets.

@rfennell
rfennell / Add-CapabilityToAgent.ps1
Last active January 29, 2021 09:27
A bit of PowerShell to add capabilities to a Azure DevOps Pipeline Agent programmatically
##-----------------------------------------------------------------------
## <copyright file="Add-CapabilityToAgent.ps1">(c) Tichard Fennell </copyright>
##-----------------------------------------------------------------------
# Adds capacility tags a Azure DevOps Pipeline agent
# Run in the agent folder
Param
(
[parameter(Mandatory=$true,HelpMessage="Personal Access Token with rights to manage agents")]
$patToken,
@rfennell
rfennell / Get-AzureDevOpsEnvApprovel.ps1
Last active May 22, 2021 15:48
A code fragment that can be used in an Azure DevOps PowerShell task to find the approver of the current stage of a multi stage YAML pipeline
try {
# this block is in its own try/catch as it uses undocumented API calls, so will fail safe
# we need to stage ID which is only available via a REST call
$uri = "$(System.CollectionUri)/$(System.TeamProject)/_apis/build/builds/$(Build.BuildID)/timeline?api-version=6.0"
$response = Get-WebClient.DownloadString($uri) | ConvertFrom-Json
$stage = $response.records | where-object -property name -eq "$(System.StageName)"
# now we can get the approval, we have to use a new webclient instance else you get a 400 Bad Request error
$uri = "$(System.CollectionUri)_apis/Contribution/HierarchyQuery/project/$(System.TeamProject)?api-version=6.1-preview.1"
$payload = "{`"contributionIds`":[`"ms.vss-build-web.checks-panel-data-provider`"],`"dataProviderContext`":{`"properties`":{`"buildId`":`"$(Build.BuildID)`",`"stageIds`":`"$($stage.id)`",`"checkListItemType`":1,`"sourcePage`":{`"url`":`"$(System.CollectionUri)/$(System.TeamProject)/_build/results?buildId=$(Build.BuildID)&view=results`",`"routeId`":`"ms.vss-bui
@rfennell
rfennell / Move-GitTag.ps1
Created July 8, 2021 09:26
Move a Git tag to the most recent commit
param
(
$tagname,
$trunk = 'main'
)
& git push origin :refs/tags/$tagname
& git tag -fa $tagname -m "Current $tagname release"
& git push origin $trunk --tags
@rfennell
rfennell / Update-AppConfig.ps1
Created February 13, 2018 09:08
PowerShell script to update an app.config file based on a parameters.xml
function Update-AppConfig
{
[cmdletbinding()]
param
(
[parameter(Mandatory = $true, HelpMessage = "Name of app.exe.config file")]
[string]$AppConfigFile,
[parameter(Mandatory = $false, HelpMessage = "Name of parameters.xml file")]
[string]$ParametersFile = "parameters.xml"
@rfennell
rfennell / add_issue_to_project
Created October 15, 2021 20:24
A GitHub Actions workflow to add any newly created issues to a Project within a GitHub user (pro or free) account as opposed to a GitHub Enterprise Organisation.in the main sample https://docs.github.com/en/issues/trying-out-the-new-projects-experience/automating-projects
name: Add Issue to project
on:
issues:
types: [opened]
jobs:
track_issue:
runs-on: ubuntu-latest
steps:
- name: Get project data
@rfennell
rfennell / Azure-DevOps-Variables-Sample-yml
Created May 5, 2022 08:47
Example of how to pass variables between Azure DevOps stages
pool:
vmImage: windows-latest
variables:
- name: system.debug
value: true
stages:
- stage: Stage1
jobs:
@rfennell
rfennell / Unregister-Agent.ps1
Created December 20, 2019 17:09
Finds an Azure Build agent, takes it off line then removes it registration
param
(
[parameter(Mandatory = $true, HelpMessage = "Azure DevOps PAT token")]
$pat,
[parameter(Mandatory = $true, HelpMessage = "URL of Azure DevOps instance e.g. https://dev.aure.com/myinstance")]
$url,
[parameter(Mandatory = $true, HelpMessage = "Azure DevOps Agent Pool name")]
$pool ,
[parameter(Mandatory = $true, HelpMessage = "Based name for agent to search for e.g MyAgent as part of B1MyAgent-123")]
$agentBaseName ,
@rfennell
rfennell / blog2md.js
Created June 25, 2022 10:58
A quick edit to the blog2md (https://github.com/palaniraja/blog2md) tools to place exported posts from a multi site WordPress Server in folders based on the sub site name
'use strict';
/***
Usage: blog2md b|w <BLOGGER/WordPress BACKUP XML> <OUTPUT DIR>
*/
const fs = require('fs');
const os = require('os');
@rfennell
rfennell / fixalias.ps1
Created July 1, 2022 10:17
A script to update a Hugo content file to provide an alias that matches a Wordpress permalink
$basepath = "C:\tmp\bmBlog\content"
$files = Get-ChildItem -path $basepath -Filter "*.md" -Recurse | % { $_.FullName}
foreach ($file in $files) {
$f = get-item $file
$content = (Get-Content -path $f.FullName)
foreach ($l in $content) {
@rfennell
rfennell / Get-AgentPoolForNamedAgent.ps1
Created September 6, 2022 09:20
A sample script that shows how to find the Azure DevOps agent pool for a named agent. Also shows the call required to delete the agent
function Get-AgentPoolForNamedAgent {
param
(
$pat,
$url,
$agentName
)
$patToken = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($pat)"))