build/build.psm1
Write-Verbose 'Importing from [D:\CODE\projects\Azure-Devops-PowerShell-Module\AzDevOps\AzDevOps\build\builds]' # .GetBuild function Get-Build { [CmdletBinding( HelpURI = 'https://github.com/Azure-Devops-PowerShell-Module/build/blob/master/docs/Get-AdoBuild.md#get-adobuild', PositionalBinding = $true )] [OutputType([Object])] param ( [Parameter(ValueFromPipeline, Mandatory = $false, ParameterSetName = 'Project')] [object]$Project, [Parameter(Mandatory = $false, ParameterSetName = 'ProjectId')] [Guid]$ProjectId, [Parameter(Mandatory = $false, ParameterSetName = 'Project')] [Parameter(Mandatory = $false, ParameterSetName = 'ProjectId')] [int]$BuildId, [Parameter(Mandatory = $false)] [ValidateSet('5.1', '7.1-preview.7', '7.2-preview.7')] [string]$ApiVersion = '7.2-preview.7' ) begin { Write-Verbose "Get-AdoBuild: Begin Processing" if ($PSCmdlet.ParameterSetName -eq 'Project') { Write-Verbose "ProjectId: $($Project.id)" } else { Write-Verbose "ProjectId: $($ProjectId)" } Write-Verbose "BuildId: $($BuildId)" Write-Verbose "ApiVersion: $($ApiVersion)" } process { try { $ErrorActionPreference = 'Stop' $Error.Clear() if (-not $Global:azDevOpsConnected) { throw "Not connected to Azure DevOps. Please connect using Connect-AzDevOps." } $BaseUri = if ($PSCmdlet.ParameterSetName -eq 'Project') { "$($Global:azDevOpsOrg)$($Project.Id)/_apis/build/builds" } else { "$($Global:azDevOpsOrg)$($ProjectId)/_apis/build/builds" } Write-Verbose "BaseUri: $BaseUri" $Uri = if ($BuildId) { "$($BaseUri)/$($BuildId)?api-version=$($ApiVersion)" } else { "$($BaseUri)?api-version=$($ApiVersion)" } Write-Verbose "Uri: $($Uri)" $Response = Invoke-AdoEndpoint -Uri ([System.Uri]::new($Uri)) -Method Get -Headers $Global:azDevOpsHeader -Verbose:$VerbosePreference if ($BuildId) { return $Response } else { return $Response.Value } } catch { Write-Error "Error retrieving build(s): $_" throw $_ } } end { Write-Verbose "Get-AdoBuild: End Processing" } } # .GetBuildLog function Get-BuildLog { [CmdletBinding( HelpURI = 'https://github.com/Azure-Devops-PowerShell-Module/build/blob/master/docs/Get-AdoBuildLog.md#get-adobuildlog', PositionalBinding = $true )] [OutputType([Object])] param ( [Parameter(ValueFromPipeline, Mandatory = $true, ParameterSetName = 'Project')] [object]$Project, [Parameter(ValueFromPipeline, Mandatory = $true, ParameterSetName = 'Project')] [object]$Build, [Parameter(Mandatory = $true, ParameterSetName = 'ProjectId')] [Guid]$ProjectId, [Parameter(Mandatory = $true, ParameterSetName = 'ProjectId')] [int]$BuildId, [Parameter(Mandatory = $false, ParameterSetName = 'Project')] [Parameter(Mandatory = $false, ParameterSetName = 'ProjectId')] [int]$LogId, [Parameter(Mandatory = $false)] [ValidateSet('5.1', '7.1-preview.2','7.2-preview.2')] [string]$ApiVersion = '7.2-preview.2' ) begin { Write-Verbose "Get-BuildLog: Begin Processing" if ($PSCmdlet.ParameterSetName -eq 'Project') { Write-Verbose "Project URL: $($Project.url)" Write-Verbose "Build ID: $($Build.Id)" } else { Write-Verbose "ProjectId: $($ProjectId)" Write-Verbose "BuildId: $($BuildId)" } Write-Verbose "LogId: $($LogId)" Write-Verbose "ApiVersion: $($ApiVersion)" } process { try { $ErrorActionPreference = 'Stop' $Error.Clear() if (-not $Global:azDevOpsConnected) { throw "Not connected to Azure DevOps. Please connect using Connect-AzDevOps." } if ($PSCmdlet.ParameterSetName -eq 'Project') { $BaseUri = "$($Global:azDevOpsOrg)$($Project.Id)/_apis/build/builds/$($Build.Id)/logs" if ($LogId) { $Uri = "$($BaseUri)/$($LogId)?api-version=$($ApiVersion)" } else { $Uri = "$($BaseUri)?api-version=$($ApiVersion)" } } else { $BaseUri = "$($Global:azDevOpsOrg)$($ProjectId)/_apis/build/builds/$($BuildId)/logs" if ($LogId) { $Uri = "$($BaseUri)/$($LogId)?api-version=$($ApiVersion)" } else { $Uri = "$($BaseUri)?api-version=$($ApiVersion)" } } Write-Verbose "BaseUri: $($BaseUri)" Write-Verbose "Uri: $($Uri)" $Response = Invoke-AdoEndpoint -Uri ([System.Uri]::new($Uri)) -Method Get -Headers $Global:azDevOpsHeader -Verbose:$VerbosePreference if ($LogId) { return $Response } else { return $Response.Value } } catch { Write-Error "Error retrieving build log(s): $_" throw $_ } } end { Write-Verbose "Get-BuildLog: End Processing" } } # .RemoveBuild function Remove-Build { [CmdletBinding(SupportsShouldProcess, ConfirmImpact = 'High', HelpURI = 'https://github.com/Azure-Devops-PowerShell-Module/build/blob/master/docs/Remove-AdoBuild.md#remove-adobuild', PositionalBinding = $true)] [OutputType([string])] param ( [Parameter(ValueFromPipeline, Mandatory = $false, ParameterSetName = 'Project')] [object]$Project, [Parameter(Mandatory = $false, ParameterSetName = 'ProjectId')] [Guid]$ProjectId, [Parameter(Mandatory = $true, ParameterSetName = 'Project')] [Parameter(Mandatory = $true, ParameterSetName = 'ProjectId')] [int]$BuildId, [Parameter(Mandatory = $false)] [ValidateSet('5.1', '7.1-preview.7','7.2-preview.2')] [string]$ApiVersion = '7.2-preview.2' ) process { Write-Verbose "Remove-Build: Process Record" if ($PSCmdlet.ParameterSetName -eq 'Project') { Write-Verbose "Project URL: $($Project.url)" Write-Verbose "Build ID: $($BuildId)" } else { Write-Verbose "ProjectId: $($ProjectId)" Write-Verbose "BuildId: $($BuildId)" } Write-Verbose "ApiVersion: $($ApiVersion)" try { $ErrorActionPreference = 'Stop' $Error.Clear() if (-not $Global:azDevOpsConnected) { throw "Not connected to Azure DevOps. Please connect using Connect-AzDevOps." } $BaseUri = if ($PSCmdlet.ParameterSetName -eq 'Project') { "$($Global:azDevOpsOrg)$($Project.Id)/_apis/build/builds/$($BuildId)" } else { "$($Global:azDevOpsOrg)$($ProjectId)/_apis/build/builds/$($BuildId)" } $Uri = "$($BaseUri)?api-version=$($ApiVersion)" Write-Verbose "BaseUri: $($BaseUri)" Write-Verbose "Uri: $($Uri)" if ($PSCmdlet.ShouldProcess("Delete", "Remove Build $($BuildId) from Azure DevOps Project")) { $Result = Invoke-AdoEndpoint -Uri ([System.Uri]::new($Uri)) -Method Delete -Headers $Global:azDevOpsHeader -Verbose:$VerbosePreference if (-not $Result) { if ($PSCmdlet.ParameterSetName -eq 'Project') { return "Build: $($BuildId) removed from Project: $($Project.name)" } else { return "Build: $($BuildId) removed from Project ID: $($ProjectId)" } } } } catch { Write-Error "Error removing build: $_" throw $_ } } } # .StartBuild function Start-Build { [CmdletBinding(SupportsShouldProcess, ConfirmImpact = 'Low', HelpURI = 'https://github.com/Azure-Devops-PowerShell-Module/build/blob/master/docs/Start-AdoBuild.md#start-adobuild', PositionalBinding = $true)] [OutputType([Object])] param ( [Parameter(ValueFromPipeline, Mandatory = $true, ParameterSetName = 'Project')] [object]$Project, [Parameter(ValueFromPipeline, Mandatory = $true, ParameterSetName = 'Project')] [object]$Definition, [Parameter(Mandatory = $true, ParameterSetName = 'ProjectId')] [Guid]$ProjectId, [Parameter(Mandatory = $true, ParameterSetName = 'ProjectId')] [int]$DefinitionId, [Parameter(Mandatory = $false, ParameterSetName = 'ProjectId')] [Parameter(Mandatory = $false, ParameterSetName = 'Project')] [hashtable[]]$Variables, [Parameter(Mandatory = $false, ParameterSetName = 'ProjectId')] [Parameter(Mandatory = $false, ParameterSetName = 'Project')] [switch]$Wait, [Parameter(Mandatory = $false)] [ValidateSet('5.1', '7.1-preview.7','7.2-preview.2')] [string]$ApiVersion = '7.2-preview.2' ) process { Write-Verbose "StartBuild: Process Record" if ($PSCmdlet.ParameterSetName -eq 'Project') { Write-Verbose "Project URL: $($Project.url)" Write-Verbose "Definition ID: $($Definition.Id)" } else { Write-Verbose "ProjectId: $($ProjectId)" Write-Verbose "DefinitionId: $($DefinitionId)" } if ($Variables) { Write-Verbose "Variables: $([string]::Join(',', $Variables.Keys))" } Write-Verbose "Wait: $($Wait)" Write-Verbose "ApiVersion: $($ApiVersion)" try { $ErrorActionPreference = 'Stop' $Error.Clear() if (-not $Global:azDevOpsConnected) { throw "Not connected to Azure DevOps. Please connect using Connect-AzDevOps." } $BaseUri = if ($PSCmdlet.ParameterSetName -eq 'Project') { "$($Global:azDevOpsOrg)$($Project.Id)/_apis/build/builds" } else { "$($Global:azDevOpsOrg)$($ProjectId)/_apis/build/builds" } $Uri = "$($BaseUri)?api-version=$($ApiVersion)" Write-Verbose "BaseUri: $($BaseUri)" Write-Verbose "Uri: $($Uri)" if ($Variables) { # Check that variables exist in definition foreach ($key in $Variables.keys) { if (!($Definition.variables | Get-Member -MemberType NoteProperty | Select-Object -ExpandProperty Name).Contains($key)) { $PSCmdlet.ThrowTerminatingError( [System.Management.Automation.ErrorRecord]::new( ([System.Management.Automation.ItemNotFoundException] "One or more variables not found in Build Definition"), 'Projects.Functions', [System.Management.Automation.ErrorCategory]::OpenError, $MyInvocation ) ) } } } $Body = New-Object -TypeName psobject -Property @{ definition = (New-Object -TypeName psobject -Property @{ id = $Definition.id }) } if ($Variables) { $Parameters = [PSCustomObject]@{} foreach ($item in $Variables) { $Parameters | Add-Member -MemberType NoteProperty -Name $item.Keys -Value $item[$item.Keys][0] } $Body | Add-Member -MemberType NoteProperty -Name parameters -Value ($Parameters | ConvertTo-Json -Compress) } if ($PSCmdlet.ShouldProcess("Start", "Queue Build $($Definition.Id) from $($Project.name) Azure DevOps Projects")) { Write-Verbose "Body: $($Body |ConvertTo-Json -Depth 99 -Compress)" $Result = Invoke-AdoEndpoint -Method POST -Uri ([System.Uri]::new($Uri)) -Headers $Global:azDevOpsHeader -ContentType 'application/json' -Body ($Body | ConvertTo-Json -Compress -Depth 10) -Verbose:$VerbosePreference if ($Wait) { do { Start-Sleep -Seconds 5 $BuildStatus = (Get-AdoBuild -Project $Project -BuildId $Result.id -Verbose:$VerbosePreference).status } until ($BuildStatus -eq 'completed') } return Get-AdoBuild -Project $Project -BuildId $Result.id -Verbose:$VerbosePreference } } catch { throw $_ } } } Write-Verbose 'Importing from [D:\CODE\projects\Azure-Devops-PowerShell-Module\AzDevOps\AzDevOps\build\definitions]' # .GetBuildDefinition function Get-BuildDefinition { [CmdletBinding( HelpURI = 'https://github.com/Azure-Devops-PowerShell-Module/build/blob/master/docs/Get-AdoBuildDefinition.md#get-Adobuilddefinition', PositionalBinding = $true )] [OutputType([Object])] param ( [Parameter(ValueFromPipeline, Mandatory = $false, ParameterSetName = 'Project')] [object]$Project, [Parameter(Mandatory = $false, ParameterSetName = 'ProjectId')] [Guid]$ProjectId, [Parameter(Mandatory = $false, ParameterSetName = 'Project')] [Parameter(Mandatory = $false, ParameterSetName = 'ProjectId')] [int]$DefinitionId, [Parameter(Mandatory = $false, ParameterSetName = 'Project')] [Parameter(Mandatory = $false, ParameterSetName = 'ProjectId')] [int]$Revision, [Parameter(Mandatory = $false)] [ValidateSet('5.1', '7.1-preview.7','7.2-preview.7')] [string]$ApiVersion = '7.2-preview.7' ) process { Write-Verbose "Get-BuildDefinition: Process Record" if ($PSCmdlet.ParameterSetName -eq 'Project') { Write-Verbose "Project URL: $($Project.url)" } else { Write-Verbose "ProjectId: $($ProjectId)" } Write-Verbose "DefinitionId: $($DefinitionId)" Write-Verbose "Revision: $($Revision)" Write-Verbose "ApiVersion: $($ApiVersion)" try { $ErrorActionPreference = 'Stop' $Error.Clear() if (-not $Global:azDevOpsConnected) { throw "Not connected to Azure DevOps. Please connect using Connect-AzDevOps." } $BaseUri = if ($PSCmdlet.ParameterSetName -eq 'Project') { "$($Global:azDevOpsOrg)$($Project.Id)/_apis/build/definitions" } else { "$($Global:azDevOpsOrg)$($ProjectId)/_apis/build/definitions" } if ($DefinitionId) { $Uri = "$($BaseUri)/$($DefinitionId)" if ($Revision) { $Uri += "?revision=$($Revision)&api-version=$($ApiVersion)" } else { $Uri += "?api-version=$($ApiVersion)" } } else { $Uri = "$($BaseUri)?api-version=$($ApiVersion)" } Write-Verbose "BaseUri: $($BaseUri)" Write-Verbose "Uri: $($Uri)" $Response = Invoke-AdoEndpoint -Uri ([System.Uri]::new($Uri)) -Method Get -Headers $Global:azDevOpsHeader -Verbose:$VerbosePreference if ($DefinitionId) { return $Response } else { return $Response.Value } } catch { throw $_ } } } Write-Verbose 'Importing from [D:\CODE\projects\Azure-Devops-PowerShell-Module\AzDevOps\AzDevOps\build\folders]' # .GetBuildFolder function Get-BuildFolder { [CmdletBinding( HelpURI = 'https://github.com/Azure-Devops-PowerShell-Module/build/blob/master/docs/Get-AdoBuildFolder.md#get-adobuildfolder', PositionalBinding = $true )] [OutputType([Object])] param ( [Parameter(ValueFromPipeline, Mandatory = $false, ParameterSetName = 'Project')] [object]$Project, [Parameter(Mandatory = $false, ParameterSetName = 'ProjectId')] [Guid]$ProjectId, [Parameter(Mandatory = $false, ParameterSetName = 'Project')] [Parameter(Mandatory = $false, ParameterSetName = 'ProjectId')] [string]$Path, [Parameter(Mandatory = $false)] [ValidateSet('5.1-preview.2', '7.1-preview.2','7.2-preview.2')] [string]$ApiVersion = '7.2-preview.2' ) process { Write-Verbose "Get-BuildFolder: Process Record" if ($PSCmdlet.ParameterSetName -eq 'Project') { Write-Verbose "Project URL: $($Project.url)" } else { Write-Verbose "ProjectId: $($ProjectId)" } Write-Verbose "Path: $($Path)" Write-Verbose "ApiVersion: $($ApiVersion)" try { $ErrorActionPreference = 'Stop' $Error.Clear() if (-not $Global:azDevOpsConnected) { throw "Not connected to Azure DevOps. Please connect using Connect-AzDevOps." } $BaseUri = if ($PSCmdlet.ParameterSetName -eq 'Project') { "$($Global:azDevOpsOrg)$($Project.Id)/_apis/build/folders" } else { "$($Global:azDevOpsOrg)$($ProjectId)/_apis/build/folders" } $Uri = "$($BaseUri)?api-version=$($ApiVersion)" if ($Path) { $Uri += "&path=$($Path.Replace('\', '/'))" } Write-Verbose "BaseUri: $($BaseUri)" Write-Verbose "Uri: $($Uri)" $Response = Invoke-AdoEndpoint -Uri ([System.Uri]::new($Uri)) -Method Get -Headers $Global:azDevOpsHeader -Verbose:$VerbosePreference return $Response.Value } catch { throw $_ } } } # .NewBuildFolder function New-BuildFolder { [CmdletBinding(SupportsShouldProcess, ConfirmImpact = 'Low', HelpURI = 'https://github.com/Azure-Devops-PowerShell-Module/build/blob/master/docs/New-AdoBuildFolder.md#new-adobuildfolder', PositionalBinding = $true)] [OutputType([Object])] param ( [Parameter(ValueFromPipeline, Mandatory = $false, ParameterSetName = 'Project')] [object]$Project, [Parameter(Mandatory = $false, ParameterSetName = 'ProjectId')] [Guid]$ProjectId, [Parameter(Mandatory = $true, ParameterSetName = 'Project')] [Parameter(Mandatory = $true, ParameterSetName = 'ProjectId')] [string]$Name, [Parameter(Mandatory = $false, ParameterSetName = 'Project')] [Parameter(Mandatory = $false, ParameterSetName = 'ProjectId')] [string]$Description, [Parameter(Mandatory = $false)] [ValidateSet('5.1-preview.2', '7.1-preview.2','7.2-preview.2')] [string]$ApiVersion = '7.2-preview.2' ) process { Write-Verbose "NewBuildFolder: Process Record" if ($PSCmdlet.ParameterSetName -eq 'Project') { Write-Verbose "ProjectId: $($Project.Id)" } else { Write-Verbose "ProjectId: $($ProjectId)" } Write-Verbose "Name: $($Name)" Write-Verbose "Description: $($Description)" Write-Verbose "ApiVersion: $($ApiVersion)" try { $ErrorActionPreference = 'Stop' $Error.Clear() if (-not $Global:azDevOpsConnected) { throw "Not connected to Azure DevOps. Please connect using Connect-AzDevOps." } $BaseUri = if ($PSCmdlet.ParameterSetName -eq 'Project') { "$($Global:azDevOpsOrg)$($Project.Id)/_apis/build/folders" } else { "$($Global:azDevOpsOrg)$($ProjectId)/_apis/build/folders" } $Uri = "$($BaseUri)?path=$($Name.Replace('\', '/'))&api-version=$($ApiVersion)" Write-Verbose "BaseUri: $($BaseUri)" Write-Verbose "Uri: $($Uri)" $Body = [PSCustomObject]@{} if ($Name) { $Body | Add-Member -MemberType NoteProperty -Name Path -Value $($Name.Replace('\', '/')) } if ($Description) { $Body | Add-Member -MemberType NoteProperty -Name Description -Value $($Description) } if ($PSCmdlet.ShouldProcess("New", "Create Folder $($Name) in $($Project.name) Azure DevOps Projects")) { return Invoke-AdoEndpoint -Uri ([System.Uri]::new($Uri)) -Method PUT -Headers $Global:azDevOpsHeader -Body ($Body | ConvertTo-Json) -ContentType 'application/json' -Verbose:$VerbosePreference } } catch { throw $_ } } } # .RemoveBuildFolder function Remove-BuildFolder { [CmdletBinding(SupportsShouldProcess, ConfirmImpact = 'High', HelpURI = 'https://github.com/Azure-Devops-PowerShell-Module/build/blob/master/docs/Remove-AdoBuildFolder.md#remove-adobuildfolder', PositionalBinding = $true)] [OutputType([string])] param ( [Parameter(ValueFromPipeline, Mandatory = $false, ParameterSetName = 'Project')] [object]$Project, [Parameter(Mandatory = $false, ParameterSetName = 'ProjectId')] [Guid]$ProjectId, [Parameter(Mandatory = $true, ParameterSetName = 'Project')] [Parameter(Mandatory = $true, ParameterSetName = 'ProjectId')] [string]$Name, [Parameter(Mandatory = $false)] [ValidateSet('5.0-preview.2', '7.1-preview.2', '7.2-preview.2')] [string]$ApiVersion = '7.2-preview.2' ) process { Write-Verbose "RemoveBuildFolder: Process Record" if ($PSCmdlet.ParameterSetName -eq 'Project') { Write-Verbose "ProjectId: $($Project.Id)" } else { Write-Verbose "ProjectId: $($ProjectId)" } Write-Verbose "Name: $($Name)" Write-Verbose "ApiVersion: $($ApiVersion)" try { $ErrorActionPreference = 'Stop' $Error.Clear() if (-not $Global:azDevOpsConnected) { throw "Not connected to Azure DevOps. Please connect using Connect-AzDevOps." } $BaseUri = if ($PSCmdlet.ParameterSetName -eq 'Project') { "$($Global:azDevOpsOrg)$($Project.Id)/_apis/build/folders" } else { "$($Global:azDevOpsOrg)$($ProjectId)/_apis/build/folders" } $Uri = "$($BaseUri)?api-version=$($ApiVersion)&path=$($Name.Replace('\', '/'))" Write-Verbose "BaseUri: $($BaseUri)" Write-Verbose "Uri: $($Uri)" if ($PSCmdlet.ShouldProcess("Delete", "Remove Folder $($Name) from $($Project.name) Azure DevOps Projects")) { Invoke-AdoEndpoint -Uri ([System.Uri]::new($Uri)) -Method DELETE -Headers $Global:azDevOpsHeader -Verbose:$VerbosePreference Write-Output "Folder $($Name) removed from project $($Project.name)" } } catch { throw $_ } } } Write-Verbose 'Importing from [D:\CODE\projects\Azure-Devops-PowerShell-Module\AzDevOps\AzDevOps\build\build.psd1]' Write-Verbose 'Importing from [D:\CODE\projects\Azure-Devops-PowerShell-Module\AzDevOps\AzDevOps\build\build.psm1]' |