Functions/Public/Remove-Artifact.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
function Remove-Artifact {
    [CmdletBinding(SupportsShouldProcess, ConfirmImpact='High')]
    param(
        [Parameter(Mandatory)]
        [ValidateNotNullOrEmpty()]
        [string]
        $Path
        ,
        [Parameter(Mandatory, ValueFromPipeline, ParameterSetName = 'Pipeline')]
        [ValidateNotNullOrEmpty()]
        [string[]]
        $Item
        ,
        [Parameter(Mandatory, ParameterSetName = 'Interactive')]
        [ValidateNotNullOrEmpty()]
        [switch]
        $Interactive
    )
    
    begin {
        if (-not $PSBoundParameters.ContainsKey('Confirm')) {
            $ConfirmPreference = $PSCmdlet.SessionState.PSVariable.GetValue('ConfirmPreference')
        }
        if (-not $PSBoundParameters.ContainsKey('WhatIf')) {
            $WhatIfPreference = $PSCmdlet.SessionState.PSVariable.GetValue('WhatIfPreference')
        }
    }

    process {
        if ($PSCmdlet.ParameterSetName -ieq 'Pipeline') {
            foreach ($subitem in $Item) {
                try {
                    if ($Force -or $PSCmdlet.ShouldProcess("Remove artifact '/$Path/$subitem'?")) {
                        Invoke-ArtifactoryApi -Path "/$Path/$subitem" -Method Delete | Out-Null
                    }

                } catch [System.Net.WebException] {
                    $response = $_.Exception.Response
                    Write-Error ('[{0}] Failed to remove {1} due to status code {2} ({3}).' -f $MyInvocation.MyCommand.Name, $response.ResponseUri, [int]($response.StatusCode), $response.StatusDescription)
                }
            }

        } elseif ($PSCmdlet.ParameterSetName -ieq 'Interactive') {
            Get-Artifact -Path $Path | Out-GridView -Title 'Select artifacts' -PassThru | Remove-Artifact -Path $Path
        }
    }
}