public/New-VolumeSnapshot.ps1

function New-ZStackVolumeSnapshot {
    # .SYNOPSIS
    # Creates a snapshot of a single volume.
    # .DESCRIPTION
    # ZStack PowerCLI (ZStack.PowerCLI 1.1.5) uses CLI 1.1.5, commit 6fcf509ff540d0098f54c1c33609dbe9f572c837.
    # Connection configuration namespace: zstack-powershell.
    # .LINK
    # https://github.com/terraform-zstack-modules/zstack-cli-go/blob/main/docs/powershell.md
    [CmdletBinding(SupportsShouldProcess)]
    param(
        [Parameter(Mandatory, ValueFromPipeline)] [ValidateNotNullOrEmpty()] [object[]] $Volume,
        [Parameter(Mandatory)] [ValidateNotNullOrEmpty()] [string] $Name,
        [string] $Description,
        [ValidatePattern('\A[a-fA-F0-9]{32}\z')] [string] $ResourceId,
        [ValidateRange(1, 3600)] [int] $TimeoutSeconds = 300,
        [object] $Server
    )
    begin { $count = 0 }
    process {
        foreach ($item in $Volume) {
            try {
                $count++
                if ($ResourceId -and $count -gt 1) { throw (New-CloudError -Operation NewVolumeSnapshot -ExitCode 2 -Target $ResourceId) }
                $parent = Get-CloudResourceSelection $item @('Volume', 'HardDisk') $Server NewVolumeSnapshot
                $id = if ($ResourceId) { $ResourceId } else { [guid]::NewGuid().ToString('N') }
                $arguments = @('snapshot', 'create', $parent.Id, "--name=$Name", '--resource-uuid', $id)
                if ($PSBoundParameters.ContainsKey('Description')) { $arguments += "--description=$Description" }
                $target = @{ Connection = $parent.Connection; Id = $id; Name = "$Name on $($parent.Id)" }
                Invoke-CloudDeviceWrite $PSCmdlet $target $arguments create-snapshot NewVolumeSnapshot 'Create volume snapshot' $TimeoutSeconds VolumeSnapshot
            } catch { $PSCmdlet.WriteError($_) }
        }
    }
}