Start-IpsAwsExportJob.ps1

<#
.SYNOPSIS
Start an Image Portability Service job to export an image from AWS.

.DESCRIPTION
Starts an Image Portability Service job to export an image from AWS to a virtual disk file on a SMB fileshare. The image to export must be in the form of an EC2 EBS volume.

.PARAMETER CustomerId
Specifies the customer id of the Citrix customer running this command.

.PARAMETER SecureClientId
Specifies the client id of the Citrix customer's API client.

.PARAMETER SecureSecret
Specifies the client secret of the Citrix customer's API client.

.PARAMETER SmbLocation
Specifies the SMB share folder to export the image to.

.PARAMETER SmbDiskName
Specifies the file name of the disk file that will be exported to the SMB server.

.PARAMETER SmbDiskFormat
Specifies the file format of the disk file that will be exported to the SMB server. Possible values are VhdDiskFormat and VhdxDiskFormat. The default value is VhdxDiskFormat.

.PARAMETER SmbCwId
Specifies the credential wallet id for the credentials used to access the SMB server.

.PARAMETER HashDisk
Hash the disk before exporting it and return the hash in the job results.

.PARAMETER ResourceLocationId
Specifies the UUID of the resource location of the Citrix Connector Appliance in AWS.

.PARAMETER AwsCwSecretId
Specifies the credential wallet id for the credentials used to access AWS.

.PARAMETER AwsVolumeId
Specifies the id of the EC2 EBS volume to be exported.

.PARAMETER AwsRegion
Specifies the AWS region where the export will take place.

.PARAMETER AwsSubnetId
Specifies the subnet id in AWS where the export appliance will be created.

.PARAMETER AwsSecurityGroupIds
Specifies an array of security groups to attach to the network interface of the export appliance VM.

.PARAMETER UsePublicIP
If specified, the export appliance VM will be given a public IP.

.PARAMETER Timeout
Specifies an optional time limit for the export operation. If the export does not complete in less than this time it will fail with a timeout error. The default value is 7200.

.PARAMETER Prefix
Specifies an optional prefix which will be prepended to the name of assets created by the export operation. The default value is 'ce'.

.PARAMETER DryRun
If specified, a test run is performed and any problems with the parameters specified are reported. No actual changes are made.

.PARAMETER Deployment
Specifies the service address to send the job request to. It defaults to api.layering.cloud.com. This can be used if necessary to send the request to a geo specific deployment such as api.eu.layering.cloud.com.

.PARAMETER LogFileDir
Specifies the path to the file to log to. The local directory is the default.

.PARAMETER LogFileName
Specifies the name of the file to log to.

.PARAMETER OverwriteLog
If specified the log file is overwritten otherwise it is appended to.

.PARAMETER Force
If specified then any existing export file is overwritten otherwise the operation will fail if the file specified to export to exists.

.INPUTS
None.

.OUTPUTS
PSCustomObject. A job description which can be consumed by the Wait-IpsJob cmdlet.

.EXAMPLE
PS> $ExportParams = @{
        CustomerId = 'a7f4wb1example'
        SecureClientId = '7fed2a1e-1495-46b7-8fd3-5644764af395'
        SecureSecret = '9T.3Q~MGlnB6NNgpNUUWrcquVzODrdGK~eXampLe'
        SmbLocation = "smb://example.com/share/folder"
        SmbDiskName = 'marketing-image'
        SmbDiskFormat = 'VhdDiskFormat'
        SmbCwId = 'smbserver-creds'
        ResourceLocationId = '47251663-6710-4f76-854a-2385e3fe002d'
        AwsCwSecretId = 'aws-creds'
        AwsVolumeId = 'vol-0aa1d07ceb4c41301'
        AwsRegion = 'us-east-2'
        AwsSubnetId = 'subnet-d7880448508711ed0'
        Prefix = 'acme'
        DryRun = $False
        Timeout = 7200
        LogFileName = '.\PrismExport.log'
    }
PS> Start-IpsAwsExportJob @ExportParams -Force -OverwriteLog -Verbose | Wait-IpsJob
#>


Function Start-IpsAwsExportJob
{
    [CmdletBinding(DefaultParameterSetName = 'cmd')]
    [Alias("Start-IpsAwsPublishJob")]
    Param(
        [Parameter(Mandatory = $true)]
        [string]$CustomerId,
        [Parameter(Mandatory = $true, ParameterSetName = 'obsoletecmd')]
        [Obsolete("Replaced by SmbLocation.")]
        [string]$SmbHost,
        [Parameter(Mandatory = $false, ParameterSetName = 'obsoletecmd')]
        [Obsolete("Replaced by SmbLocation.")]
        [string]$SmbShare,
        [Parameter(Mandatory = $false, ParameterSetName = 'obsoletecmd')]
        [Obsolete("Replaced by SmbLocation.")]
        [string]$SmbPath,
        [Parameter(Mandatory = $true, ParameterSetName = 'cmd')]
        [Uri]$SmbLocation,
        [Parameter(Mandatory = $true)]
        [string]$SmbDiskName,
        [Parameter()]
        [string]$SmbDiskFormat = "VhdxDiskFormat",
        [Parameter(Mandatory = $true)]
        [string]$SmbCwId,
        [Parameter(Mandatory = $false)]
        [switch]$HashDisk,
        [Parameter()]
        [string]$Deployment,
        [Parameter(Mandatory = $true)]
        [string]$ResourceLocationId,
        [Parameter(Mandatory = $true)]
        [Alias("CloudCwSecretId")]
        [string]$AwsCwSecretId,
        [Parameter()]
        [string]$AssetsId,
        [Parameter()]
        [int]$Timeout = 7200,
        [Parameter()]
        [bool]$UsePublicIP = $false,
        [Parameter()]
        [string]$Prefix,
        [Parameter(Mandatory = $true)]
        [string]$AwsRegion,
        [Parameter(Mandatory = $true)]
        [string]$AwsSubnetId,
        [Parameter()]
        [string[]]$AwsSecurityGroupIds,
        [Parameter(Mandatory = $true)]
        [string]$AwsVolumeId,
        [Parameter()]
        [HashTable]$JobDebug = @{},
        [Parameter()]
        [bool]$DryRun = $false,
        [Parameter()]
        [string]$SecureClientId,
        [Parameter()]
        [string]$SecureSecret,
        [Parameter()]
        [string]$LogFileDir,
        [Parameter()]
        [string]$LogFileName = 'ExportAwsToSmb.log',
        [Parameter()]
        [switch]$OverwriteLog,
        [Parameter()]
        [switch]$Force
    )

    Begin
    {
        Add-PSSnapin Citrix.*
    }
    Process
    {
        # Initialize Logger
        # Set parameter 'Verbose' by internal parameter 'VerbosePreference', since the option -Verbose is occupied by powershell cmdlet
        $Verbose = $VerbosePreference -eq 'Continue'
        LogInit $MyInvocation $LogFileDir $LogFileName $OverwriteLog $Verbose

        VersionCheck $Deployment $CustomerId

        try
        {
            # Authenticate to Citrix Cloud
            $parameters = AuthToCitrixCloud $CustomerId $SecureClientId $SecureSecret
            if ([string]::IsNullOrWhiteSpace($SecureClientId) -Or [string]::IsNullOrWhiteSpace($SecureSecret))
            {
                $SecureClientId = $parameters.ApiKey
                $SecureSecret = $parameters.SecretKey
            }
        }
        catch
        {
            LogFatal "Failed to authenticate to Citrix Cloud"
        }

        # Export
        try
        {
            LogIt "Starting export workflow"
            #
            # Run the export workflow
            #
            Write-Host "***** Call Method: ExportImageJob overwrite: $($Force.IsPresent) *****"
            $parameters = GetVariablesAsHash
            $platformData = Format-AwsData -Parameters $parameters -Verbose $Verbose
            $exportData = Format-ExportData -Platform "Aws" -Parameters $parameters -Verbose $Verbose

            # Convert the object to JSON to use in the POST body (Note: Default depth is 2 when serializing)
            $json = ($exportData + $platformData) | ConvertTo-Json -Depth 10
            LogIt "Export POST body $json" $Verbose

            $query = @{
                "async" = $true
                "dryRun" = $DryRun
            }

            try
            {
                $response = Invoke-CCRestMethod -method 'Post' -deployment $Deployment -serviceRoute "images/`$export" -customerId $CustomerId -secureClientId $SecureClientId -secureSecret $SecureSecret -query $query -json $json
                $JobId = $response.id
                LogIt "Image export started with id $JobId"
            }
            catch
            {
                $JobId = "Job failed to start"
                LogFatal "Failed to start export: $_"
            }
        }
        catch
        {
            LogFatalException -Message "Workflow failed" -Exception $_.Exception
        }
        finally
        {
            $output = [PSCustomObject]@{
                CustomerId = $CustomerId
                Deployment = $Deployment
                JobId = $JobId
                LogFileDir = $LogFileDir
                LogFileName = $LogFileName
                SmbLocation = $SmbLocation
                SmbDiskName = $SmbDiskName
                SmbDiskFormat = $SmbDiskFormat
                SmbCwId = $SmbCwId
            }
            Write-Output $output

            # Clear credentials at end of pipeline
            if ($PSCmdlet.MyInvocation.PipelinePosition -eq $PSCmdlet.MyInvocation.PipelineLength)
            {
                Clear-XDCredentials
            }
        }
    }
}