Citrix.Image.Uploader/Copy-DiskToGCP.ps1

<#

.SYNOPSIS
Copy a disk to Google Cloud.

.DESCRIPTION
Copy a disk to a Google Cloud image.

.PARAMETER FileName
Specifies the filename of the disk to be copied. The file can be either local or on a share.

.PARAMETER ImageName
Specifies the name of the Google Cloud image to copy the disk to.

.PARAMETER BucketName
Specifies the name of an existing S3 bucket to use for temporarily storing an uploaded copy of the disk. If unspecified a S3 bucket with a unique name is created for this purpose and deleted upon completion of the image upload.

.PARAMETER LogFile
Specifies the path to the file to log to. ".\Upload.log" is the default.

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

.PARAMETER Force
If the destination of the copy already exists then delete it before doing the copy.

.PARAMETER ServiceAccountKeyFile
Specifies the name of a file containing Google Cloud service principal credentials.

.PARAMETER Labels
Specifies labels to label the image with.

.INPUTS
None.

.OUTPUTS
None.

.EXAMPLE

Copy-DiskToGCP -FileName C:\demo\disk.vhd -ImageName demo -ServiceAccountKeyFile demo-project-af94dadb30a1.json -Labels @{Organization = "acme"; user = "roadrunner"}

.EXAMPLE

Copy-DiskToGCP -FileName C:\demo\disk.vhd -ImageName demo -BucketName acme -ServiceAccountKeyFile demo-project-af94dadb30a1.json

#>


Function Copy-DiskToGCP
{
    [CmdletBinding()]
    Param(
        [Parameter(Mandatory = $True)]
        [string] $FileName,

        [Parameter(Mandatory = $True)]
        [string] $ImageName,

        [Parameter()]
        [string] $BucketName,

        [Parameter()]
        [string] $ServiceAccountKeyFile,

        [Parameter()]
        [string] $LogFile,

        [Parameter()]
        [switch] $OverwriteLog,

        [Parameter()]
        [hashtable] $Labels,

        [Parameter()]
        [switch] $Force
    )

    Begin
    {
        InitUploadLog $LogFile $OverwriteLog

        VersionCheck
    }

    Process
    {
        try
        {
            if ($Force)
            {
                CleanUpGcpDisk $ServiceAccountKeyFile $ImageName
            }
            $InformationPreference = "Continue"
            try
            {
                Copy-ToGcpDisk -File $FileName -BucketName $BucketName -ImageName $ImageName -ServiceAccountKeyFile $ServiceAccountKeyFile -LogFileName $Global:UploadLogFile -Labels $Labels
            }
            catch
            {
                ThrowError ([UploaderError]::new("Failed to copy the disk to Google Cloud", $_.Exception))
            }
        }
        catch [UploaderError]
        {
            LogIfSslError $_
            $PSCmdlet.ThrowTerminatingError($_)
        }
        catch
        {
            Log $_
            LogIfSslError $_
            $PSCmdlet.ThrowTerminatingError($_)
        }
    }
}