Start-IpsAzurePrepareAndPublishJob.ps1

<#
 .Synopsis
  Implement an image prepare job followed by an image export job.

 .Description
  Implement an image prepare job followed by an image export job. This function supports preparing and publishing an image on Azure platform.
#>

Function Start-IpsAzurePrepareAndPublishJob
{
    [CmdletBinding(DefaultParameterSetName = 'cmd')]
    Param(
        [Parameter(Mandatory = $true, ParameterSetName = 'file')]
        [Obsolete("Use command-line arguments as input of configuration data.")]
        [string]$ConfigJsonFile,
        [Parameter(Mandatory = $true, ParameterSetName = 'cmd')]
        [string]$CustomerId,
        [Parameter(Mandatory = $false, ParameterSetName = 'cmd')]
        [string]$CloudProvisioningType = "Pvs",
        [Parameter(Mandatory = $true, ParameterSetName = 'cmd')]
        [string]$CloudCwSecretId,
        [Parameter(Mandatory = $true, ParameterSetName = 'cmd')]
        [string]$CloudDiskName,
        [Parameter(Mandatory = $true, ParameterSetName = 'cmd')]
        [psobject[]]$XdReconfigure,
        [Parameter(Mandatory = $false, ParameterSetName = 'cmd')]
        [string]$Deployment,
        [Parameter(Mandatory = $true, ParameterSetName = 'cmd')]
        [string]$ResourceLocationId,
        [Parameter(Mandatory = $true, ParameterSetName = 'cmd')]
        [string]$AzureSubscriptionId,
        [Parameter(Mandatory = $false, ParameterSetName = 'cmd')]
        [string]$AzureLocation = "eastus",
        [Parameter(Mandatory = $true, ParameterSetName = 'cmd')]
        [string]$TargetResourceGroup,
        [Parameter(Mandatory = $true, ParameterSetName = 'cmd')]
        [string]$AzureVirtualNetworkResourceGroupName,
        [Parameter(Mandatory = $true, ParameterSetName = 'cmd')]
        [string]$AzureVirtualNetworkName,
        [Parameter(Mandatory = $true, ParameterSetName = 'cmd')]
        [string]$AzureVirtualNetworkSubnetName,
        [Parameter(Mandatory = $false, ParameterSetName = 'cmd')]
        [bool]$UsePublicIP = $false,
        [Parameter(Mandatory = $false, ParameterSetName = 'cmd')]
        [string]$AzureVmResourceGroup,
        [Parameter(Mandatory = $false, ParameterSetName = 'cmd')]
        [string]$AssetsId,
        [Parameter(Mandatory = $false, ParameterSetName = 'cmd')]
        [psobject]$Tags = [PSCustomObject]@{},
        [Parameter(Mandatory = $false, ParameterSetName = 'cmd')]
        [int]$Timeout = 20000,
        [Parameter(Mandatory = $false, ParameterSetName = 'cmd')]
        [string]$Prefix = "ce",
        [Parameter(Mandatory = $false, ParameterSetName = 'cmd')]
        [string]$CeVmSku = "Standard_D2s_v3",
        [Parameter(Mandatory = $false, ParameterSetName = 'cmd')]
        [bool]$CeDebug,
        [Parameter(Mandatory = $false, ParameterSetName = 'cmd')]
        [bool]$DomainUnjoin = $false,
        [Parameter(Mandatory = $false, ParameterSetName = 'cmd')]
        [bool]$InstallMisa = $true,
        [Parameter(Mandatory = $false, ParameterSetName = 'cmd')]
        [bool]$ForceMisa = $false,
        [Parameter(Mandatory = $false, ParameterSetName = 'cmd')]
        [string]$InstallPvs,
        [Parameter(Mandatory = $false, ParameterSetName = 'cmd')]
        [string]$SmbHost,
        [Parameter(Mandatory = $false, ParameterSetName = 'cmd')]
        [Obsolete("Specifying an alternate SMB port is not supported and this parameter will be ignored.")]
        [string]$SmbPort = $null,
        [Parameter(Mandatory = $true, ParameterSetName = 'cmd')]
        [string]$SmbShare,
        [Parameter(Mandatory = $false, ParameterSetName = 'cmd')]
        [string]$SmbPath,
        [Parameter(Mandatory = $true, ParameterSetName = 'cmd')]
        [string]$SmbDiskName,
        [Parameter(Mandatory = $false, ParameterSetName = 'cmd')]
        [string]$SmbDiskFormat = "VhdxDiskFormat",
        [Parameter(Mandatory = $true, ParameterSetName = 'cmd')]
        [string]$SmbCwId,
        [Parameter(Mandatory = $false, ParameterSetName = 'cmd')]
        [string[]]$Flags,
        [Parameter(Mandatory = $false, ParameterSetName = 'cmd')]
        [bool]$DryRun = $false,
        [Parameter(Mandatory = $false)]
        [string]$SecureClientId = '',
        [Parameter(Mandatory = $false)]
        [string]$SecureSecret = '',
        [Parameter(Mandatory = $false)]
        [string]$LogFileDir = "",
        [Parameter(Mandatory = $false)]
        [string]$LogFileName = 'AzurePrepareAndPublish.log',
        [Parameter(Mandatory = $false)]
        [switch]$OverwriteLog,
        [Parameter(Mandatory = $false)]
        [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
        if($VerbosePreference -eq 'Continue')
        {
            $Verbose = $True
        }
        else
        {
            $Verbose = $False
        }
        LogInit $LogFileDir $LogFileName $OverwriteLog $Verbose
        # Initialize config data
        if ($PSCmdlet.ParameterSetName -eq 'file') {
            LogIt "Loading config from $ConfigJsonFile"
            $configData = Get-Content -Raw -Path $ConfigJsonFile | ConvertFrom-Json
            LogIt "Config: $configData" $False
            $CustomerId = $configData.CustomerId
            $CloudProvisioningType = $configData.CloudProvisioningType
            $CloudCwSecretId = $configData.CloudCwSecretId
            $CloudDiskName = $configData.CloudDiskName
            $XdReconfigure = $configData.XdReconfigure
            $SmbHost = $configData.PublishSmb.Host
            $SmbShare = $configData.PublishSmb.Share
            $SmbPath = $configData.PublishSmb.Path
            $SmbDiskName = $configData.PublishSmb.DiskName
            $SmbDiskFormat = $configData.PublishSmb.DiskFormat
            if([String]::IsNullOrWhiteSpace($SmbDiskFormat)) { $SmbDiskFormat = "VhdxDiskFormat" }
            $SmbCwId = $configData.PublishSmb.CwId
            $Deployment = $configData.Deployment
            if($configData.psobject.Properties.name -contains "PrepareResourceLocationId") { $ResourceLocationId = $configData.PrepareResourceLocationId }
            else { $ResourceLocationId = $configData.ResourceLocationId }
            $AzureSubscriptionId = $configData.AzureSubscriptionId
            if([String]::IsNullOrWhiteSpace($configData.AzureLocation)) { $AzureLocation = 'eastus' }
            else { $AzureLocation = $configData.AzureLocation }
            $AzureVirtualNetworkResourceGroupName = $configData.AzureVirtualNetworkResourceGroupName
            $AzureVirtualNetworkName = $configData.AzureVirtualNetworkName
            $AzureVirtualNetworkSubnetName = $configData.AzureVirtualNetworkSubnetName
            $UsePublicIP = $configData.UsePublicIP
            $TargetResourceGroup = $configData.TargetResourceGroup
            $AzureVmResourceGroup = $configData.AzureVmResourceGroup
            $AssetsId = $configData.AssetsId
            if($configData.psobject.Properties.name -contains "Tags") { $Tags = $configData.Tags }
            if([String]::IsNullOrWhiteSpace($configData.Timeout)) { $Timeout = 20000 }
            else { $Timeout = [int]$configData.Timeout }
            if([String]::IsNullOrWhiteSpace($configData.Prefix)) { $Prefix = "ce" }
            else { $Prefix = $configData.Prefix }
            $CeDebug = $configData.CeDebug
            $DomainUnjoin = $configData.DomainUnjoin
            $InstallMisa = $configData.InstallMisa
            $ForceMisa = $configData.ForceMisa
            $InstallPvs = $configData.InstallPvs
            $Flags = $configData.Flags
        }

        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"
        }

        # Prepare
        try
        {
            LogIt "Starting prepareAndPublish workflow"
            #
            # Run the prepare workflow
            #
            Write-Host "***** Call Method: prepareAndPublish *****"
            $platformpreparePublishData = @{
                subscriptionId = $AzureSubscriptionId
                azureRegion = $AzureLocation
                targetDiskResourceGroupName = $TargetResourceGroup
                targetDiskName = $CloudDiskName
                VirtualNetworkResourceGroupName = $AzureVirtualNetworkResourceGroupName
                VirtualNetworkName = $AzureVirtualNetworkName
                VirtualNetworkSubnetName = $AzureVirtualNetworkSubnetName
                UsePublicIP = $UsePublicIP
                resourceGroup = $AzureVmResourceGroup
            }
            #Add default tags
            $Tags = Convert-ObjectToHashtable $Tags
            $Tags['ctx-user'] = ($env:UserName).ToLower()
            if ($SmbPath)
            {
                $smbDiskPath = "$SmbShare\$SmbPath"
            }
            else
            {
                $smbDiskPath = $SmbShare
            }
            $preparePublishData = @{
                platform = "Azure"
                provisioningType = $CloudProvisioningType
                platformCredentialId = $CloudCwSecretId
                resourceLocationId = $ResourceLocationId
                XdReconfigure = $XdReconfigure
                tags = $Tags
                timeoutInSeconds = $Timeout
                prefix = $Prefix
                CeVmSku = $CeVmSku
                ceDebug = $CeDebug
                outputStorageLocation = @{
                    type = "SMB"
                    credentialId = $SmbCwId
                    host = $SmbHost
                    sharePath = $smbDiskPath
                }
                outputImageFilename = $SmbDiskName
                outputImageFormat = $SmbDiskFormat
                provisionType = "Thin"
                overwriteTargetFile = $Force.IsPresent
            }
            if ($AssetsId)
            {
                $preparePublishData['assetsId'] = $AssetsId
            }
            if ($DomainUnjoin)
            {
                $preparePublishData['domainUnjoin'] = $DomainUnjoin
            }
            if ($CloudProvisioningType -eq "Pvs")
            {
                if ($InstallPvs)
                {
                    $preparePublishData['installPvs'] = $InstallPvs
                }
                $preparePublishData['installMisa'] = $false
                $preparePublishData['forceMisa'] = $false
            }
            if ($Flags)
            {
                $preparePublishData['flags'] = $Flags
            }
            # Convert the object to JSON to use in the POST body (Note: Default depth is 2 when serializing)
            $json = ($preparePublishData + $platformpreparePublishData) | ConvertTo-Json -Depth 10
            LogIt "$($preparePublishData["CloudProvisioningType"]) Prepare POST body $json" $False

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

            # Send the POST
            try
            {
                $response = Invoke-CCRestMethod 'Post' $Deployment "images/`$prepareAndPublish" $CustomerId $SecureClientId $SecureSecret $query $json
                $JobId = $response.id
                LogIt "Image prepareAndPublish started with id $JobId"
            }
            catch
            {
                $JobId = "Job failed to start"
                throw "Failed to start prepareAndPublish: $_"
            }
        }
        catch
        {
            LogFatal "Workflow failed: $_"
        }
        finally
        {
            #Check if it comes to the end of pipeline
            if ($PSCmdlet.MyInvocation.PipelinePosition  -lt $PSCmdlet.MyInvocation.PipelineLength)
            {
                $output = [PSCustomObject] @{
                    CustomerId = $CustomerId
                    Deployment = $Deployment
                    JobId = $JobId
                    LogFileDir = $LogFileDir
                    LogFileName = $LogFileName
                }
                Write-Output $output
            }
            else
            {
                Clear-XDCredentials
            }
        }
    }
}