Skip to content

feat: Added new functions asked in issue 82 and updated some small changes like helpuri #83

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# Don't check in the Output dir
Output/
site/
baswijdenes/
test.ps1
.vs
2 changes: 1 addition & 1 deletion AzureDevOpsPowerShell/AzureDevOpsPowerShell.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@
} # End of PrivateData hashtable

# HelpInfo URI of this module
# HelpInfoURI = ''
HelpInfoURI = 'https://github.com/WeAreInSpark/AzureDevOpsPowerShellAPI'

# Default prefix for commands exported from this module. Override the default prefix using Import-Module -Prefix.
# DefaultCommandPrefix = ''
Expand Down
10 changes: 5 additions & 5 deletions AzureDevOpsPowerShell/AzureDevOpsPowerShell.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
$public = @(Get-ChildItem -Path (Join-Path -Path $PSScriptRoot -ChildPath 'Public/*.ps1') -Recurse -ErrorAction Stop)
$private = @(Get-ChildItem -Path (Join-Path -Path $PSScriptRoot -ChildPath 'Private/*.ps1') -Recurse -ErrorAction Stop)
foreach ($import in @($private + $public)) {
try {
. $import.FullName
} catch {
throw "Unable to dot source [$($import.FullName)]"
}
try {
. $import.FullName
} catch {
throw "Unable to dot source [$($import.FullName)]"
}
}

Export-ModuleMember -Function $public.Basename
3 changes: 3 additions & 0 deletions AzureDevOpsPowerShell/Private/Invoke-AzDoRestMethod.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ function Invoke-AzDoRestMethod {
}

if ($QueryParameters) {
if ($QueryParameters -notlike "?*") {
$QueryParameters = "?$QueryParameters"
}
$params.Uri = "$($Uri)?$($QueryParameters)&api-version=$($Version)"
} else {
$params.Uri = "$($Uri)?api-version=$($Version)"
Expand Down
2 changes: 1 addition & 1 deletion AzureDevOpsPowerShell/Private/New-AzDoAuthHeader.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ function New-AzDoAuthHeader {
)
if ($PSCmdlet.ShouldProcess("Creating new authentication header")) {
Write-Verbose "Function: New-AzDoAuthHeader"
if ($Pat -eq '') {
if ([string]::IsNullOrEmpty($Pat)) {
# validate if user is logged in to Azure PowerShell
Write-Verbose "Using Access Token"
try {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
function Get-AzDoProjectProperties {
<#
.SYNOPSIS
Retrieves properties of specified Azure DevOps projects.

.DESCRIPTION
The Get-AzDoProjectProperties function retrieves properties of specified Azure DevOps projects within a given collection URI. It supports pipeline input for project names and collection URI.

.EXAMPLE
$Params = @{
CollectionUri = "https://dev.azure.com/organization"
ProjectName = "Project1"
}
Get-AzDoProjectProperties @Params

This example retrieves properties of the project named "Project1" in the specified Azure DevOps organization.

.EXAMPLE
$Params = @{
CollectionUri = "https://dev.azure.com/organization"
}
"Project1", "Project2" | Get-AzDoProjectProperties @Params

This example retrieves properties of multiple projects ("Project1" and "Project2") in the specified Azure DevOps organization.

.NOTES
This function requires the Validate-CollectionUri and Invoke-AzDoRestMethod helper functions to be defined in the scope.

.LINK
https://learn.microsoft.com/en-us/rest/api/azure/devops/core/projects/get-project-properties?view=azure-devops-rest-7.1&tabs=HTTP
#>
[CmdletBinding(SupportsShouldProcess)]
param (
# Collection Uri of the organization
[Parameter(Mandatory, ValueFromPipelineByPropertyName)]
[ValidateScript({ Validate-CollectionUri -CollectionUri $_ })]
[string]
$CollectionUri,

# Project where the Repos are contained
[Parameter(ValueFromPipelineByPropertyName, ValueFromPipeline)]
[string]
$ProjectName
)

begin {
Write-Verbose "Starting function: Get-AzDoProjectProperties"
}

process {
# somehow it will not work on project name, but will work like this:
$ProjectId = (Get-AzDoProject -CollectionUri $CollectionUri -ProjectName $ProjectName).ProjectID
$params = @{
uri = "$CollectionUri/_apis/projects/$ProjectId/Properties"
version = "7.2-preview.1"
method = 'GET'
}

if ($PSCmdlet.ShouldProcess($CollectionUri, "Get project $ProjectName properties")) {
$result = Invoke-AzDoRestMethod @params
} else {
Write-Verbose "Calling Invoke-AzDoRestMethod with $($params| ConvertTo-Json -Depth 10)"
}

if ($result) {
$HashTable = @{
CollectionURI = $CollectionUri
ProjectName = $ProjectName
}
foreach ($property in $result.value) {
$HashTable[$property.Name] = $property.Value
}
[PSCustomObject]$HashTable
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,35 @@ function Get-AzDoEnvironment {
Creates a Build Validation policy on a branch
.DESCRIPTION
Creates a Build Validation policy on a branch

.EXAMPLE
$params = @{
$Params = @{
CollectionUri = "https://dev.azure.com/contoso"
Name = "Policy 1"
RepoName = "Repo 1"
ProjectName = "Project 1"
Id = 1
ProjectName = "Project 1"
}
Set-AzDoBranchPolicyBuildValidation @params
Get-AzDoEnvironment @Params

This example creates a policy with splatting parameters
This example retrieves all environments in the specified project ("Project 1") in Azure DevOps.

.EXAMPLE
$env:SYSTEM_ACCESSTOKEN = '***'
New-AzDoPipeline -CollectionUri "https://dev.azure.com/contoso" -ProjectName "Project 1" -Name "Pipeline 1" -RepoName "Repo 1" -Path "main.yml"
| Set-AzDoBranchPolicyBuildValidation
$Params = @{
CollectionUri = "https://dev.azure.com/contoso"
ProjectName = "Project 1"
EnvironmentName = "Environment 1"
}
Get-AzDoEnvironment @Params

This example retrieves details of the environment named "Environment 1" in the specified project ("Project 1") in Azure DevOps.

.EXAMPLE
$Params = @{
CollectionUri = "https://dev.azure.com/contoso"
ProjectName = "Project 1"
EnvironmentName = @("Environment 1", "Environment 2")
}
Get-AzDoEnvironment @Params

This example creates a new Azure Pipeline and sets this pipeline as Build Validation policy on the main branch
This example retrieves details of multiple environments ("Environment 1" and "Environment 2") in the specified project ("Project 1") in Azure DevOps.

.OUTPUTS
[PSCustomObject]@{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
function Remove-AzDoEnvironment {
<#
.SYNOPSIS
Remove Environment from Azure DevOps.

.DESCRIPTION
This function removes an environment from Azure DevOps.

.EXAMPLE
Remove-AzDoEnvironment -CollectionUri "https://dev.azure.com/contoso" -ProjectName "Project 1" -EnvironmentName "Environment 1"

This example removes the environment named "Environment 1" from the specified project in Azure DevOps.

.EXAMPLE
Remove-AzDoEnvironment -CollectionUri "https://dev.azure.com/contoso" -ProjectName "Project 1" -EnvironmentName "Environment 1", "Environment 2"

This example removes multiple environments ("Environment 1" and "Environment 2") from the specified project in Azure DevOps.

.EXAMPLE
Remove-AzDoEnvironment -CollectionUri "https://dev.azure.com/contoso" -ProjectName "Project 1" -EnvironmentName 1

This example removes the environment with the ID 1 from the specified project in Azure DevOps.

.EXAMPLE
Remove-AzDoEnvironment -CollectionUri "https://dev.azure.com/contoso" -ProjectName "Project 1" -EnvironmentName 1, 2

This example removes multiple environments with IDs 1 and 2 from the specified project in Azure DevOps.

.EXAMPLE
Remove-AzDoEnvironment -CollectionUri "https://dev.azure.com/contoso" -ProjectName "Project 1" -EnvironmentName "Environment 1", 2

This example removes a mix of environments by name ("Environment 1") and ID (2) from the specified project in Azure DevOps.

.LINK
https://learn.microsoft.com/en-us/rest/api/azure/devops/environments/environments/delete?view=azure-devops-rest-7.2
#>
[CmdletBinding(SupportsShouldProcess, ConfirmImpact = 'High')]
[OutputType([System.Collections.ArrayList])]
param (
# Collection URI. e.g. https://dev.azure.com/contoso.
# Azure Pipelines has a predefined variable for this.
[Parameter(Mandatory, ValueFromPipelineByPropertyName)]
[ValidateScript({ Validate-CollectionUri -CollectionUri $_ })]
[string]
$CollectionUri,

# Name of the project.
[Parameter(Mandatory, ValueFromPipelineByPropertyName, ValueFromPipeline)]
[string]
$ProjectName,

# Id or name of the environment.
# this is a string because a name can be used as well and will do a Get-AzDoEnvironment to get the ID.
[Parameter(Mandatory, ValueFromPipelineByPropertyName, ValueFromPipeline)]
[string[]]
$EnvironmentName
)

begin {
$result = @()
Write-Verbose "Starting function: Remove-AzDoEnvironment"
}

Process {

foreach ($Environment in $EnvironmentName) {
if ($Environment -ne [int]) {
$EnvironmentId = (Get-AzDoEnvironment -CollectionUri $CollectionUri -ProjectName $ProjectName -EnvironmentName $Environment).EnvironmentId

} else {
$EnvironmentId = $Environment
}

$params = @{
uri = "$CollectionUri/$ProjectName/_apis/pipelines/environments/$EnvironmentId"
version = "7.2-preview.1"
method = 'DELETE'
}

if ($PSCmdlet.ShouldProcess($CollectionUri, "Remove environment id: $($PSStyle.Bold)$EnvironmentId$($PSStyle.Reset)")) {
$result += Invoke-AzDoRestMethod @params
} else {
Write-Verbose "Calling Invoke-AzDoRestMethod with $($params| ConvertTo-Json -Depth 10)"
}
}
if ($result) {
$result | ForEach-Object {
[PSCustomObject]@{
CollectionUri = $CollectionUri
ProjectName = $ProjectName
Response = $_
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
function Get-AzDoPipelineBranchControl {
<#
.SYNOPSIS
Retrieves branch policy configurations of specified Azure DevOps projects.

.DESCRIPTION
The Get-AzDoPipelineBranchControl function retrieves branch policy configurations of specified Azure DevOps projects within a given collection URI.
It supports pipeline input for project names and collection URI.

.EXAMPLE
$Params = @{
CollectionUri = "https://dev.azure.com/organization"
ProjectName = "Project1"
}
Get-AzDoPipelineBranchControl @Params

This example retrieves branch policy configurations for the project named "Project1" in the specified Azure DevOps organization.

.EXAMPLE
$Params = @{
CollectionUri = "https://dev.azure.com/organization"
}
"Project1", "Project2" | Get-AzDoPipelineBranchControl @Params

This example retrieves branch policy configurations for multiple projects ("Project1" and "Project2") in the specified Azure DevOps organization.

.EXAMPLE
$Params = @{
CollectionUri = "https://dev.azure.com/organization"
ProjectName = "Project1"
RepositoryId = "12345"
RefName = "refs/heads/main"
PolicyType = "Build"
}
Get-AzDoPipelineBranchControl @Params

This example retrieves branch policy configurations for the "main" branch in the repository with ID "12345" in the project "Project1" in the specified Azure DevOps organization, filtered by the "Build" policy type.

.NOTES
This function requires the Validate-CollectionUri and Invoke-AzDoRestMethod helper functions to be defined in the scope.

.LINK
https://learn.microsoft.com/en-us/rest/api/azure/devops/git/policy-configurations/list?view=azure-devops-rest-5.0#policyconfiguration
#>
[CmdletBinding(SupportsShouldProcess)]
param (
# Collection URI of the organization
[Parameter(Mandatory, ValueFromPipelineByPropertyName)]
[ValidateScript({ Validate-CollectionUri -CollectionUri $_ })]
[string]
$CollectionUri,

# Project where the policies are defined
[Parameter(ValueFromPipelineByPropertyName, ValueFromPipeline)]
[string]
$ProjectName,

# Repository ID (optional)
[Parameter(ValueFromPipelineByPropertyName)]
[string]
$RepositoryId,

# Ref name (branch) to filter policies (optional)
[Parameter(ValueFromPipelineByPropertyName)]
[string]
$RefName,

# Policy type to filter results (optional)
[Parameter(ValueFromPipelineByPropertyName)]
[string]
$PolicyType
)

begin {
Write-Verbose "Starting function: Get-AzDoPipelineBranchControl"
}

process {
# somehow it will not work on project name, but will work like this:
$ProjectId = (Get-AzDoProject -CollectionUri $CollectionUri -ProjectName $ProjectName).ProjectID

$queryParams = @()
if ($RepositoryId) {
$queryParams += "repositoryId=$RepositoryId"
}
if ($RefName) {
$queryParams += "refName=$RefName"
}
if ($PolicyType) {
$queryParams += "policyType=$PolicyType"
}
$queryString = $queryParams -join "&"

$params = @{
Uri = "$CollectionUri/$ProjectId/_apis/git/policy/configurations"
Version = "5.0-preview.1"
Method = 'GET'
}
if (-not([string]::IsNullOrEmpty(($queryString)))) {
$params.QueryParameters = $queryString
}
if ($PSCmdlet.ShouldProcess($CollectionUri, "Get branch policies for project $ProjectName")) {
$response = Invoke-AzDoRestMethod @params
} else {
Write-Verbose "Calling Invoke-AzDoRestMethod with $($params | ConvertTo-Json -Depth 10)"
}
if ($response) {
$List = [System.Collections.Generic.List[Object]]::new()
foreach ($property in $response.Value) {
$Property | Add-Member -MemberType NoteProperty -Name "ProjectName" -Value $ProjectName
$Property | Add-Member -MemberType NoteProperty -Name "CollectionURI" -Value $CollectionUri
if ($RepositoryId) {
$Property | Add-Member -MemberType NoteProperty -Name "RepositoryId" -Value $RepositoryId
}
if ($RefName) {
$Property | Add-Member -MemberType NoteProperty -Name "RefName" -Value $RefName
}
if ($PolicyType) {
$Property | Add-Member -MemberType NoteProperty -Name "PolicyType" -Value $PolicyType
}
$List.Add($Property)
}
$List
}
}
}
Loading