Skip to content

Get-Template

VMware: Retrieves virtual machine templates

#Requires -Version 5.1
#Requires -Modules VMware.VimAutomation.Core
[CmdletBinding(DefaultParameterSetName = "byName")]
Param(
    [Parameter(Mandatory = $true, ParameterSetName = "byID")]
    [Parameter(Mandatory = $true, ParameterSetName = "byName")]
    [Parameter(Mandatory = $true, ParameterSetName = "byDatastore")]
    [string]$VIServer,
    [Parameter(Mandatory = $true, ParameterSetName = "byID")]
    [Parameter(Mandatory = $true, ParameterSetName = "byName")]
    [Parameter(Mandatory = $true, ParameterSetName = "byDatastore")]
    [pscredential]$VICredential,
    [Parameter(Mandatory = $true, ParameterSetName = "byID")]
    [string]$TemplateID,
    [Parameter(ParameterSetName = "byName")]
    [string]$TemplateName,
    [Parameter(Mandatory = $true, ParameterSetName = "byDatastore")]
    [string]$DatastoreName,
    [Parameter(ParameterSetName = "byName")]
    [Parameter(ParameterSetName = "byDatastore")]
    [switch]$NoRecursion
)
Process {
    try {
        $vmServer = Connect-VIServer -Server $VIServer -Credential $VICredential -ErrorAction Stop
        if ($PSCmdlet.ParameterSetName -eq "byID") {
            $result = Get-Template -Server $vmServer -Id $TemplateID -ErrorAction Stop | Select-Object *
        }
        elseif ($PSCmdlet.ParameterSetName -eq "byName") {
            if ([System.String]::IsNullOrWhiteSpace($TemplateName)) { $result = Get-Template -Server $vmServer -NoRecursion:$NoRecursion -ErrorAction Stop | Select-Object * }
            else { $result = Get-Template -Server $vmServer -Name $TemplateName -ErrorAction Stop | Select-Object * }
        }
        else {
            $datastore = Get-Datastore -Server $vmServer -Name $DatastoreName -ErrorAction Stop
            $result = Get-Template -Server $vmServer -Datastore $datastore -NoRecursion:$NoRecursion -ErrorAction Stop | Select-Object *
        }
        if ($null -ne $result) { foreach ($item in $result) { $item | Add-Member -NotePropertyName Timestamp -NotePropertyValue (Get-Date -Format "yyyy-MM-dd HH:mm:ss") -PassThru -Force } }
    }
    catch { throw }
    finally { if ($null -ne $vmServer) { Disconnect-VIServer -Server $vmServer -Force -Confirm:$false -ErrorAction SilentlyContinue } }
}

IP address or DNS name of the vSphere server

PSCredential object for authenticating with the server

ID of the template

Name of the template; retrieves all if empty

Name of the datastore where templates are stored

Off

Disable recursive behavior

An interactive directory of PowerShell scripts.