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')] [string]$ApiVersion = '7.1-preview.7' ) begin { Write-Verbose "GetBuild : 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)"; try { $ErrorActionPreference = 'Stop'; $Error.Clear(); # # Are we connected # if ($Global:azDevOpsConnected) { if ($PSCmdlet.ParameterSetName -eq 'ProjectId') { $Project = Get-AdoProject -ProjectId $ProjectId -Verbose:$VerbosePreference; } $Uri = $Global:azDevOpsOrg + "$($Project.Id)/_apis/build/builds?api-version=$($ApiVersion)"; if ($BuildId) { $Uri = $Global:azDevOpsOrg + "$($Project.Id)/_apis/build/builds/$($BuildId)?api-version=$($ApiVersion)"; return (Invoke-AdoEndpoint -Uri ([System.Uri]::new($Uri)) -Method Get -Headers $Global:azDevOpsHeader -Verbose:$VerbosePreference); } else { return (Invoke-AdoEndpoint -Uri ([System.Uri]::new($Uri)) -Method Get -Headers $Global:azDevOpsHeader -Verbose:$VerbosePreference).Value; } } } catch { throw $_; } } } # .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')] [string]$ApiVersion = '7.1-preview.2' ) process { Write-Verbose "GetBuildLog : Process Record"; if ($PSCmdlet.ParameterSetName -eq 'Project') { Write-Verbose " ProjectID : $($Project.Id)"; Write-Verbose " BuildId : $($Build.Id)"; } else { Write-Verbose " ProjectID : $($ProjectId)"; Write-Verbose " BuildId : $($BuildId)"; } Write-Verbose " LogId : $($LogId)"; Write-Verbose " ApiVersion : $($ApiVersion)"; try { $ErrorActionPreference = 'Stop'; $Error.Clear(); # # Are we connected # if ($Global:azDevOpsConnected) { if ($PSCmdlet.ParameterSetName -eq 'ProjectId') { $Project = Get-AdoProject -ProjectId $ProjectId -Verbose:$VerbosePreference; $Build = Get-AdoBuild -ProjectId $Project.id -BuildId $BuildId -Verbose:$VerbosePreference; } $Uri = $Global:azDevOpsOrg + "$($Project.Id)/_apis/build/builds/$($Build.Id)/logs?api-version=$($ApiVersion)"; if ($LogId) { $Uri = $Global:azDevOpsOrg + "$($Project.Id)/_apis/build/builds/$($Build.Id)/logs/$($LogId)?api-version=$($ApiVersion)"; return (Invoke-AdoEndpoint -Uri ([System.Uri]::new($Uri)) -Method Get -Headers $Global:azDevOpsHeader -Verbose:$VerbosePreference); } else { return (Invoke-AdoEndpoint -Uri ([System.Uri]::new($Uri)) -Method Get -Headers $Global:azDevOpsHeader -Verbose:$VerbosePreference).Value; } } } catch { throw $_; } } } # .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')] [string]$ApiVersion = '7.1-preview.7' ) process { Write-Verbose "RemoveBuild : Process Record"; if ($PSCmdlet.ParameterSetName -eq 'Project') { Write-Verbose " ProjectID : $($Project.Id)"; Write-Verbose " BuildId : $($Build.Id)"; } else { Write-Verbose " ProjectID : $($ProjectId)"; Write-Verbose " BuildId : $($BuildId)"; } Write-Verbose " ApiVersion : $($ApiVersion)"; try { $ErrorActionPreference = 'Stop'; $Error.Clear(); # # Are we connected # if ($Global:azDevOpsConnected) { if ($PSCmdlet.ParameterSetName -eq 'ProjectId') { $Project = Get-AdoProject -ProjectId $ProjectId -Verbose:$VerbosePreference; $Build = Get-AdoBuild -ProjectId $Project.Id -BuildId $BuildId -Verbose:$VerbosePreference; } if (!($Build.deleted)) { $Uri = $Global:azDevOpsOrg + "$($Project.Id)/_apis/build/builds/$($Build.Id)?api-version=$($ApiVersion)"; if ($PSCmdlet.ShouldProcess("Delete", "Remove Build $($Build.Id) from $($Project.name) Azure Devops Projects")) { $Result = (Invoke-AdoEndpoint -Uri ([System.Uri]::new($Uri)) -Method Delete -Headers $Global:azDevOpsHeader -Verbose:$VerbosePreference); } if (!($Result)) { return "Build : $($Build.id) removed from Project : $($Project.name)" } } else { return "Build : $($Build.id) was deleted on $(Get-Date ($Build.deletedDate)) by $($Build.deletedBy.displayName)" } } } catch { 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')] [string]$ApiVersion = '7.1-preview.7' ) process { Write-Verbose "StartBuild : Process Record"; if ($PSCmdlet.ParameterSetName -eq 'Project') { Write-Verbose " ProjectID : $($Project.Id)"; Write-Verbose " DefinitionId : $($Definition.Id)"; } else { Write-Verbose " ProjectID : $($ProjectId)"; Write-Verbose " DefinitionId : $($DefinitionId)"; } Write-Verbose " Variables : $([string]::Join(',',$Variables.Keys))"; Write-Verbose " Wait : $($Wait)"; Write-Verbose " ApiVersion : $($ApiVersion)"; try { $ErrorActionPreference = 'Stop'; $Error.Clear(); # # Are we connected # if ($Global:azDevOpsConnected) { if ($PSCmdlet.ParameterSetName -eq 'ProjectId') { $Project = Get-AdoProject -ProjectId $ProjectId -Verbose:$VerbosePreference; $Definition = Get-AdoBuildDefinition -ProjectId $Project.Id -DefinitionId $DefinitionId -Verbose:$VerbosePreference; } $Uri = $Global:azDevOpsOrg + "$($Project.Id)/_apis/build/builds?api-version=$($ApiVersion)"; # # Check that variables exist in defintion # 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, $MyObject ) ); } } $Body = New-Object -TypeName psobject; Add-Member -InputObject $Body -MemberType NoteProperty -Name definition -Value $Definition; $Parameters = New-Object -TypeName psobject; foreach ($item in $Variables) { Add-Member -InputObject $parameters -MemberType NoteProperty -Name $item.Keys -Value $item[$item.Keys][0] }; Add-Member -InputObject $Body -MemberType NoteProperty -Name parameters -Value ($Parameters | ConvertTo-Json -Compress); if ($PSCmdlet.ShouldProcess("Start", "Qeue Build $($Build.Id) from $($Project.name) Azure Devops Projects")) { $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 { Get-AdoBuild -Project $Project -BuildId $Result.id | out-null; } until ((Get-AdoBuild -Project $Project -BuildId $Result.id -Verbose:$VerbosePreference).status -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')] [string]$ApiVersion = '7.1-preview.7' ) process { Write-Verbose "GetBuildDefinition : Process Record"; if ($PSCmdlet.ParameterSetName -eq 'Project') { Write-Verbose " ProjectId : $($Project.Id)"; } else { Write-Verbose " ProjectId : $($ProjectId)"; } Write-Verbose " DefinitionId : $($DefinitionId)"; Write-Verbose " Revision : $($Revision)"; Write-Verbose " ApiVersion : $($ApiVersion)"; try { $ErrorActionPreference = 'Stop'; $Error.Clear(); # # Are we connected # if ($Global:azDevOpsConnected) { if ($PSCmdlet.ParameterSetName -eq 'ProjectId') { $Project = Get-AdoProject -ProjectId $ProjectId -Verbose:$VerbosePreference; } $Uri = $Global:azDevOpsOrg + "$($Project.Id)/_apis/build/definitions?api-version=$($ApiVersion)"; if ($DefinitionId) { $Uri = $Global:azDevOpsOrg + "$($Project.Id)/_apis/build/definitions/$($DefinitionId)"; if ($Revision) { $Uri = $Uri + "?revision=$($Revision)&api-version=$($ApiVersion)"; } else { $Uri = $Uri + "?api-version=$($ApiVersion)" } return (Invoke-AdoEndpoint -Uri ([System.Uri]::new($Uri)) -Method Get -Headers $Global:azDevOpsHeader -Verbose:$VerbosePreference); } else { return (Invoke-AdoEndpoint -Uri ([System.Uri]::new($Uri)) -Method Get -Headers $Global:azDevOpsHeader -Verbose:$VerbosePreference).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')] [string]$ApiVersion = '7.1-preview.2' ) process { Write-Verbose "GetBuildFolder : Process Record"; if ($PSCmdlet.ParameterSetName -eq 'Project') { Write-Verbose " ProjectId : $($Project.Id)"; } else { Write-Verbose " ProjectId : $($ProjectId)"; } Write-Verbose " Path : $($Path)"; Write-Verbose " ApiVersion : $($ApiVersion)"; try { $ErrorActionPreference = 'Stop'; $Error.Clear(); # # Are we connected # if ($Global:azDevOpsConnected) { if ($PSCmdlet.ParameterSetName -eq 'ProjectId') { $Project = Get-AdoProject -ProjectId $ProjectId -Verbose:$VerbosePreference; } $Uri = $Global:azDevOpsOrg + "$($Project.Id)/_apis/build/folders?api-version=$($ApiVersion)"; if ($Path) { $Uri = $Uri + "&path=$($Path.Replace('\','/'))" } return (Invoke-AdoEndpoint -Uri ([System.Uri]::new($Uri)) -Method Get -Headers $Global:azDevOpsHeader -Verbose:$VerbosePreference).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')] [string]$ApiVersion = '7.1-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 : $($Path)"; Write-Verbose " Description : $($Path)"; Write-Verbose " ApiVersion : $($ApiVersion)"; try { $ErrorActionPreference = 'Stop'; $Error.Clear(); # # Are we connected # if ($Global:azDevOpsConnected) { if ($PSCmdlet.ParameterSetName -eq 'ProjectId') { $Project = Get-AdoProject -ProjectId $ProjectId -Verbose:$VerbosePreference; } $Body = New-Object -TypeName psobject if ($Name) { Add-Member -InputObject $Body -MemberType NoteProperty -Name Path -Value $Name.Replace('\', '/') }; if ($Description) { Add-Member -InputObject $Body -MemberType NoteProperty -Name Description -Value $Description }; $Uri = $Global:azDevOpsOrg + "$($Project.Id)/_apis/build/folders?path=$($Name.Replace('\','/'))&api-version=$($ApiVersion)"; 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')] [string]$ApiVersion = '7.1-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 : $($Path)"; Write-Verbose " ApiVersion : $($ApiVersion)"; try { $ErrorActionPreference = 'Stop'; $Error.Clear(); # # Are we connected # if ($Global:azDevOpsConnected) { if ($PSCmdlet.ParameterSetName -eq 'ProjectId') { $Project = Get-AdoProject -ProjectId $ProjectId -Verbose:$VerbosePreference; } $Folder = Get-AdoBuildFolder -Project $Project -Path $Name -Verbose:$VerbosePreference; $Uri = $Global:azDevOpsOrg + "$($Project.Id)/_apis/build/folders/?api-version=$($ApiVersion)&path=$($Folder.Path)" 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; $Project | Get-AdoBuildFolder -Verbose:$VerbosePreference; } } } 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]' |