Module/DevOps/New-BCSDevOpsProject.ps1
<#
.SYNOPSIS Create a new Azure DevOps Project .DESCRIPTION Setup a new Azure DevOps Project .PARAMETER organisation DevOps organisation where the project will be created. .PARAMETER projectName Name of the project to create .PARAMETER licenseFile path to licensfile .PARAMETER password DevOps build container default password (SecureString) .PARAMETER sourcePat Url to template repository if different from the default repository .EXAMPLE New-BCSDevOpsProject -$organisation 'MyOrg' -projectName 'MyProject' -SourcePat 'MyPersonalAccessToken' -password (Get-BCSSecureString -InputString "MyPassword") -licensfile (Get-BCSSecureString -InputString "MyLicenseFilePath") -sourcePat -sourcePat (Get-BCSSecureString -InputString "MyDevOpsPat") .NOTES Author: Mathias Stjernfelt Website: http://www.brightcom.se #> function New-BCSDevOpsProject { [CmdletBinding(SupportsShouldProcess)] Param ( [Parameter(Mandatory = $false)] [string]$organisation = 'BrightComSolutions', [Parameter(Mandatory = $true)] [string]$projectName, [Parameter(Mandatory = $true)] [Securestring]$licenseFile, [Parameter(Mandatory = $true)] [securestring]$password, [Parameter(Mandatory = $false)] $variableGroupName = "BusinessCentral", [Parameter(Mandatory = $true)] [securestring]$sourcePat ) try { $fullOrgUrl = "https://dev.azure.com/$organisation" $sourcePatPlainText = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto([System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($sourcePat)) $sourcePatPlainText | az devops login --org $fullOrgUrl Write-host "Logged in to '$fullOrgUrl'" $project = az devops project show --project $projectName --org $fullOrgUrl | convertfrom-json if ([Object]::ReferenceEquals($project, $null)) { if ($PSCmdlet.ShouldProcess($projectName)) { $project = az devops project create --name $projectName --org $fullOrgUrl | convertfrom-json Write-host ("Project {0} was successfully creted in organisation {1}." -f $project.name, $organisation) } } else { Write-host ("Project {0} already exists in organisation {1}, aborting." -f $project.name, $organisation) -ForegroundColor Red exit; } Write-host ("Setting up Variable group {0} " -f $variableGroupName) $licensenFilePlainText = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto([System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($licenseFile)) $group = az pipelines variable-group create --name $variableGroupName --project $projectName --org $fullOrgUrl --variable "LicenseFile=$licensenFilePlainText" | ConvertFrom-json Write-host " - Adding secret LicenseFile variable" $variable = az pipelines variable-group variable update --group-id $group.id --name "LicenseFile" --project $projectName --org $fullOrgUrl --value $licensenFilePlainText --secret true Write-host " - Adding secret Password variable" $passwordPlainText = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto([System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($password)) $variable = az pipelines variable-group variable create --group-id $group.id --name "Password" --project $projectName --org $fullOrgUrl --value $passwordPlainText --secret true } catch { az DevOps logout throw "An error occured: $_.Exception"; } finally { az DevOps logout } } Export-ModuleMember -Function New-BCSDevOpsProject |