Functions/Public/content-management-service/Import-vRAPackage.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
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
function Import-vRAPackage {
<#
    .SYNOPSIS
    Imports a vRA Content Package

    .DESCRIPTION
    Imports a vRA Content Package

    .PARAMETER File
    The content package file

    .PARAMETER DontValidatePackage
    Skip Package Validation. Not recommended by the API documentation

    .INPUTS
    System.String
    System.Switch

    .OUTPUTS
    System.Management.Automation.PSObject

    .EXAMPLE
    Import-vRAPackage -File C:\Packages\Package100.zip

    .EXAMPLE
    Get-ChildItem -Path C:\Packages\Package100.zip| Import-vRAPackage -Confirm:$false

#>

[CmdletBinding(SupportsShouldProcess,ConfirmImpact="High")][OutputType('System.Management.Automation.PSObject')]

    Param (

        [Parameter(Mandatory=$true,ValueFromPipeline=$true,ValueFromPipelinebyPropertyName=$true)]
        [ValidateNotNullOrEmpty()]
        [String[]]$File,

        [Parameter(Mandatory=$false)]
        [Switch]$DontValidatePackage

    )

    Begin {

        xRequires -Version 7.0

        # --- Set Set Line Feed
        $LF = "`r`n"
   
    }

    Process {

        foreach ($FilePath in $File){

            try {
                # --- Validate the Content Package
                if (!$PSBoundParameters.ContainsKey('DontValidatePackage')){

                    $Test = Test-vRAPackage -File $FilePath

                    switch ($Test.operationStatus) {

                        'FAILED' {

                            $Test.operationResults
                            throw "Content Package failed validation test. You should remedy the issue with the Content Package before importing - A failed import may potentially leave the system in an inconsistent state"
                        }
                        'WARNING' {

                            $Test.operationResults
                            Write-Warning "Content Package $FilePath contains a warning. Please check the Operation Results for details"
                        }
                        'SUCCESS' {

                            Write-Verbose "Content Package $FileInfo has been successfully validated"
                        }
                        Default {

                            throw "Unable to validate Content Package $FilePath"
                        }
                    }
                }
                else {

                    Write-Verbose "Skipping Content Package validation"
                }

                # --- Resolve the file path
                $FileInfo = [System.IO.FileInfo](Resolve-Path $FilePath).Path

                # --- Create the multi-part form
                $Boundary = [guid]::NewGuid().ToString()
                $FileBin = [System.IO.File]::ReadAllBytes($FileInfo.FullName)
                $Encoding = [System.Text.Encoding]::GetEncoding("iso-8859-1")
                $EncodedFile = $Encoding.GetString($FileBin)

                $Form = (
                    "--$($Boundary)",
                    "Content-Disposition: form-data; name=`"file`"; filename=`"$($FileInfo.Name)`"",
                    "Content-Type:application/octet-stream$($LF)",
                    $EncodedFile,
                    "--$($Boundary)--$($LF)"
                ) -join $LF

                $URI = "/content-management-service/api/packages/"

                # --- Set custom headers for the request
                $Headers = @{
                
                    "Authorization" = "Bearer $($Global:vRAConnection.Token)";
                    "Accept" = "Application/json"
                    "Accept-Encoding" = "gzip,deflate,sdch";
                    "Content-Type" = "multipart/form-data; boundary=$($Boundary)"
                }

                if ($PSCmdlet.ShouldProcess($FileInfo.FullName)){

                    # --- Run vRA REST request
                    Invoke-vRARestMethod -Method POST -Uri $URI -Body $Form -Headers $Headers -Verbose:$VerbosePreference

                }

            }
            catch [Exception]{

                throw
            }
        }
    }

    End {

    }
}