Functions/Set-ArtifactoryParameters.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
<#PSScriptInfo
 
.VERSION 1.0.0
 
.GUID
 
.AUTHOR Artsiom Krot
 
.PROJECTURI https://github.com/artyom-krot/PS.JfrogArtifactory
 
.RELEASENOTES
 
 
Script file name:
 
    Set-ArtifactoryParameters.ps1
 
.DESCRIPTION
 
    The script is an integral part of PS.JfrogArtifactory solution (https://github.com/artyom-krot/PS.JfrogArtifactory)
 
#>


function Set-ArtifactoryParameters {
    <#
    .SYNOPSIS
        PowerShell script for setting up jfrog artifactory credentials, that can be used accross the whole PS.JfrogArtifactory module.
    .DESCRIPTION
        The function defines credentials and establish connection for validating those credentials.
         
    .INPUTS
        -artifactoryUrl <string[]>
 
        -artifactoryUser <string[]>
         
        -artifactoryUserToken <string[]>
 
    .OUTPUTS
        Information message
 
    .NOTES
         
     
    .EXAMPLE
        Set-ArtifactoryParameters -artifactoryUrl $ArtifactoryServer -artifactoryUser $ArtifactoryUser -artifactoryUserToken $ArtifactoryToken
 
    #>


    [CmdletBinding(SupportsShouldProcess, ConfirmImpact='Low')]

    param(
        [parameter(Position = 0, Mandatory = $true)]
        [ValidateNotNullOrEmpty()]
        [string]
        $artifactoryUrl,

        [parameter(Position = 1, Mandatory = $true)]
        [ValidateNotNullOrEmpty()]
        [string]
        $artifactoryUser,

        [parameter(Position = 3, Mandatory = $false)]
        [ValidateNotNullOrEmpty()]
        [string]
        $artifactoryUserToken = (Read-Host -Prompt 'Password' -AsSecureString | Get-PlaintextFromSecureString),

        [parameter(Position = 4, Mandatory = $false)]
        [switch]
        $Force
    )
    
    if ($Force -or $PSCmdlet.ShouldProcess("Set new credentials for artifactoryUrl '$artifactoryUrl' and artifactoryUser '$artifactoryUser'?")) {
        $script:artifactoryUrl        = $artifactoryUrl
        $script:artifactoryUser       = $artifactoryUser
        $script:artifactoryUserToken  = $artifactoryUserToken
    }

    # Establish one-time connection to Jfro Artifactory to validate provided credentials
    $testConnection = Invoke-ArtifactoryRestApi -RestApiPath "/api/storageinfo" -Method Get -ErrorAction SilentlyContinue
    
    if ([string]::IsNullOrEmpty($testConnection)) {
        Write-Error ("Connection to the artifactory can't be estalished")
        Write-Warning "Validate connectivity or set new credentials for artifactoryUrl '$artifactoryUrl' and artifactoryUser '$artifactoryUser'"
    }
    else {
        Write-Verbose "Connection to the jfrog artifactory '$artifactoryUrl' has been validated successfully."
    }
}