AzureDevOps.Account/Public/Add-AzureDevOpsAccount.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
function Add-AzureDevOpsAccount
{
    <#
    .SYNOPSIS
    Add your Azure DevOps account to interact with the Azure DevOps REST API.
 
    .DESCRIPTION
    Saves your account details in a global variable named $AzureDevOpsAccount for the other cmdlets to use.
 
    .PARAMETER OrganisationName
    Your Azure DevOps organisation name
 
    .PARAMETER ProjectName
    Your Azure DevOps project name
 
    .PARAMETER UserName
    Your Azure DevOps user name
 
    .PARAMETER Token
    Your Azure DevOps personal access token
 
    .EXAMPLE
    Add-AzureDevOpsAccount -OrganisationName "contoso" -ProjectName "adventureapp" -UserName "mehmet.seckin@contoso.com" -Token "your-personal-access-token"
 
    #>

    param
    (
       [string]$OrganisationName = "",
       [string]$ProjectName = "",
       [string]$UserName = "",
       [string]$Token = ""
    )

    $base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $UserName,$Token)));
    $baseApiUrl = "https://dev.azure.com/$($OrganisationName)/$($ProjectName)/_apis";
    $azureDevOpsAccountObject = [PSCustomObject]@{
        OrganisationName = $OrganisationName
        ProjectName = $ProjectName
        UserName = $UserName
        PersonalAccessToken = $Token
        BaseApiUrl = $baseApiUrl
        AuthorizationHeader = $base64AuthInfo
    }

    Set-Variable -Name "AzureDevOpsAccount" -Value $azureDevOpsAccountObject -Scope Global;
}