public/Get-VM.ps1

function Get-ZStackVM {
    # .SYNOPSIS
    # Returns virtual machines by name or ID from the selected connection.
    # .DESCRIPTION
    # ZStack PowerCLI (ZStack.PowerCLI 1.1.4) uses CLI 1.1.4, commit dca13b1d898c80c0f7189b5adb4a5537685ad4a6.
    # Connection configuration namespace: zstack-powershell.
    # .LINK
    # https://github.com/terraform-zstack-modules/zstack-cli-go/blob/main/docs/powershell.md
    [CmdletBinding(DefaultParameterSetName = 'Name')]
    param(
        [Parameter(Position = 0, ParameterSetName = 'Name')] [ValidateNotNullOrEmpty()] [string[]] $Name,
        [Parameter(Mandatory, ParameterSetName = 'Id')] [ValidateNotNullOrEmpty()] [ValidatePattern('\A[A-Za-z0-9-]+\z')] [string[]] $Id,
        [object] $Server,
        [switch] $Detail
    )

    if ($Detail -and ($PSCmdlet.ParameterSetName -ne 'Id' -or $Id.Count -ne 1)) {
        throw (New-CloudError -Operation DescribeVM -ExitCode 2)
    }
    $connection = Get-CloudConnection $Server
    if ($Detail) {
        Read-CloudVMDetail $connection $Id[0]
        return
    }
    if ($PSCmdlet.ParameterSetName -eq 'Id' -and $Id.Count -eq 1) {
        Convert-CloudVM (Read-CloudVMInventory $connection -Id $Id[0]) $connection
        return
    }
    $patterns = @($Name | Where-Object { $null -ne $_ } | ForEach-Object {
        if ($_ -eq '') { throw 'Name must not be empty.' }
        [System.Management.Automation.WildcardPattern]::new($_, 'IgnoreCase')
    })
    $rows = Read-CloudVMInventory $connection
    if ($PSCmdlet.ParameterSetName -eq 'Name' -and $patterns.Count -eq 0) {
        Convert-CloudVM $rows $connection
        return
    }
    $matches = foreach ($inventory in $rows) {
        if ($PSCmdlet.ParameterSetName -eq 'Id' -and $Id -cnotcontains $inventory['uuid']) { continue }
        if ($patterns.Count -gt 0) {
            $matched = $false
            foreach ($pattern in $patterns) {
                if ($pattern.IsMatch([string] $inventory['name'])) { $matched = $true; break }
            }
            if (-not $matched) { continue }
        }
        $inventory
    }
    Convert-CloudVM $matches $connection
}