Functions/Public/Export-vRAContentPackage.psm1

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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
function Export-vRAContentPackage {
<#
    .SYNOPSIS
    Export a vRA Content Package
    
    .DESCRIPTION
    Export a vRA Content Package
    
    .PARAMETER Id
    Specify the ID of a Content Package

    .PARAMETER Name
    Specify the Name of a Content Package

    .PARAMETER Path
    The resulting path. If this parameter is not passed the action will be exported to
    the current working directory.

    .INPUTS
    System.String

    .OUTPUTS
    System.IO.FileInfo
    
    .EXAMPLE
    Export-vRAContentPackage -Id "b2d72c5d-775b-400c-8d79-b2483e321bae" -Path C:\Packages\ContentPackage01.zip

    .EXAMPLE
    Export-vRAContentPackage -Name "ContentPackage01" -Path C:\Packages\ContentPackage01.zip

    .EXAMPLE
    Get-vRAContentPackage | Export-vRAContentPackage

    .EXAMPLE
    Get-vRAContentPackage -Name "ContentPackage01" | Export-vRAContentPackage -Path C:\Packages\ContentPackage01.zip

#>

[CmdletBinding(DefaultParameterSetName="ById")][OutputType('System.IO.FileInfo')]

    Param (

        [Parameter(Mandatory=$true,ValueFromPipelineByPropertyName=$true,ParameterSetName="ById")]
        [ValidateNotNullOrEmpty()]
        [String[]]$Id,         

        [Parameter(Mandatory=$true,ParameterSetName="ByName")]
        [ValidateNotNullOrEmpty()]
        [String[]]$Name,
        
        [Parameter(Mandatory=$false)]
        [ValidateNotNullOrEmpty()]
        [String]$Path

    )

    Begin {

        # --- Test for vRA API version
        xRequires -Version 7.0

        function internalWorkings ($InternalContentPackage, $InternalId, $InternalPath) {
            
            $Headers = @{

                "Authorization" = "Bearer $($Global:vRAConnection.Token)";
                "Accept"="application/zip";
                "Content-Type" = "Application/zip";

            }
            
            $FileName = "$($InternalContentPackage.Name).zip"

            if (!$InternalPath) {

                Write-Verbose -Message "Path parameter not passed, exporting to current directory."
                $FullPath = "$($(Get-Location).Path)\$($FileName)"

            }
            else {

                Write-Verbose -Message "Path parameter passed."
                
                if ($InternalPath.EndsWith("\")) {

                    Write-Verbose -Message "Ends with"

                    $InternalPath = $InternalPath.TrimEnd("\")

                }
                
                $FullPath = "$($InternalPath)\$($FileName)"
            }

            # --- Run vRA REST Request
            $URI = "/content-management-service/api/packages/$($InternalId)"

            Invoke-vRARestMethod -Method GET -Headers $Headers -URI $URI -OutFile $FullPath -Verbose:$VerbosePreference

            # --- Output the result
            Get-ChildItem -Path $FullPath
        }
    }

    Process {

        try {    

            switch ($PsCmdlet.ParameterSetName) {
            
                'ByName' {

                    foreach ($ContentPackageName in $Name) {

                        $ContentPackage = Get-vRAContentPackage -Name $ContentPackageName
                        $Id = $ContentPackage.Id

                        internalWorkings -InternalContentPackage $ContentPackage -InternalId $Id -InternalPath $Path                   
                    }
                }
                'ById' {

                    foreach ($ContentPackageId in $Id){

                        $ContentPackage = Get-vRAContentPackage -Id $ContentPackageId

                        internalWorkings -InternalContentPackage $ContentPackage -InternalId $ContentPackageId -InternalPath $Path
                    }
                }
            }
        }
        catch [Exception]{

            throw
        }
    }
}