Merge pull request #210 from lamw/master
Added support for VMTX Template support for VMC-based Content Library
This commit is contained in:
@@ -579,3 +579,121 @@ Function Copy-ContentLibrary {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Function New-VMTX {
|
||||||
|
<#
|
||||||
|
.NOTES
|
||||||
|
===========================================================================
|
||||||
|
Created by: William Lam
|
||||||
|
Organization: VMware
|
||||||
|
Blog: www.virtuallyghetto.com
|
||||||
|
Twitter: @lamw
|
||||||
|
===========================================================================
|
||||||
|
.DESCRIPTION
|
||||||
|
This function clones a VM to VM Template in Content Library (currently only supported on VMC)
|
||||||
|
.PARAMETER SourceVMName
|
||||||
|
The name of the source VM to clone
|
||||||
|
.PARAMETER VMTXName
|
||||||
|
The name of the VM Template in Content Library
|
||||||
|
.PARAMETER Description
|
||||||
|
Description of the VM template
|
||||||
|
.PARAMETER LibaryName
|
||||||
|
The name of the Content Library to clone to
|
||||||
|
.PARAMETER FolderName
|
||||||
|
The name of vSphere Folder (Defaults to Workloads for VMC)
|
||||||
|
.PARAMETER ResourcePoolName
|
||||||
|
The name of the vSphere Resource Pool (Defaults to Compute-ResourcePools for VMC)
|
||||||
|
.EXAMPLE
|
||||||
|
New-VMTX -SourceVMName "Windows10-BaseInstall" -VMTXName "Windows10-VMTX-Template" -LibraryName "VMC-CL-01"
|
||||||
|
#>
|
||||||
|
param(
|
||||||
|
[Parameter(Mandatory=$true)][String]$SourceVMName,
|
||||||
|
[Parameter(Mandatory=$true)][String]$VMTXName,
|
||||||
|
[Parameter(Mandatory=$false)][String]$Description,
|
||||||
|
[Parameter(Mandatory=$true)][String]$LibraryName,
|
||||||
|
[Parameter(Mandatory=$false)][String]$FolderName="Workloads",
|
||||||
|
[Parameter(Mandatory=$false)][String]$ResourcePoolName="Compute-ResourcePool"
|
||||||
|
)
|
||||||
|
|
||||||
|
$vmtxService = Get-CisService -Name "com.vmware.vcenter.vm_template.library_items"
|
||||||
|
|
||||||
|
$sourceVMId = ((Get-VM -Name $SourceVMName).ExtensionData.MoRef).Value
|
||||||
|
$libraryId = ((Get-ContentLibrary -LibraryName $LibraryName).Id).Value
|
||||||
|
$folderId = ((Get-Folder -Name $FolderName).ExtensionData.MoRef).Value
|
||||||
|
$rpId = ((Get-ResourcePool -Name $ResourcePoolName).ExtensionData.MoRef).Value
|
||||||
|
|
||||||
|
$vmtxCreateSpec = $vmtxService.Help.create.spec.Create()
|
||||||
|
$vmtxCreateSpec.source_vm = $sourceVMId
|
||||||
|
$vmtxCreateSpec.name = $VMTXName
|
||||||
|
$vmtxCreateSpec.description = $Description
|
||||||
|
$vmtxCreateSpec.library = $libraryId
|
||||||
|
$vmtxCreateSpec.placement.folder = $folderId
|
||||||
|
$vmtxCreateSpec.placement.resource_pool = $rpId
|
||||||
|
|
||||||
|
Write-Host "`nCreating new VMTX Template from $SourceVMName in Content Library $LibraryName ..."
|
||||||
|
$result = $vmtxService.create($vmtxCreateSpec)
|
||||||
|
}
|
||||||
|
|
||||||
|
Function New-VMFromVMTX {
|
||||||
|
<#
|
||||||
|
.NOTES
|
||||||
|
===========================================================================
|
||||||
|
Created by: William Lam
|
||||||
|
Organization: VMware
|
||||||
|
Blog: www.virtuallyghetto.com
|
||||||
|
Twitter: @lamw
|
||||||
|
===========================================================================
|
||||||
|
.DESCRIPTION
|
||||||
|
This function deploys a new VM from Template in Content Library (currently only supported in VMC)
|
||||||
|
.PARAMETER VMTXName
|
||||||
|
The name of the VM Template in Content Library to deploy from
|
||||||
|
.PARAMETER NewVMName
|
||||||
|
The name of the new VM to deploy
|
||||||
|
.PARAMETER FolderName
|
||||||
|
The name of vSphere Folder (Defaults to Workloads for VMC)
|
||||||
|
.PARAMETER ResourcePoolName
|
||||||
|
The name of the vSphere Resource Pool (Defaults to Compute-ResourcePools for VMC)
|
||||||
|
.PARAMETER NumCpu
|
||||||
|
The number of vCPU to configure for the new VM
|
||||||
|
.PARAMETER MemoryMb
|
||||||
|
The amount of memory (MB) to configure for the new VM
|
||||||
|
.PARAMETER PowerOn
|
||||||
|
To power on the VM after deploy
|
||||||
|
.EXAMPLE
|
||||||
|
New-VMFromVMTX -NewVMName "FooFoo" -VMTXName "FooBar" -PowerOn $true -NumCpu 4 -MemoryMB 2048
|
||||||
|
#>
|
||||||
|
param(
|
||||||
|
[Parameter(Mandatory=$true)][String]$VMTXName,
|
||||||
|
[Parameter(Mandatory=$true)][String]$NewVMName,
|
||||||
|
[Parameter(Mandatory=$false)][String]$FolderName="Workloads",
|
||||||
|
[Parameter(Mandatory=$false)][String]$ResourcePoolName="Compute-ResourcePool",
|
||||||
|
[Parameter(Mandatory=$false)][String]$DatastoreName="WorkloadDatastore",
|
||||||
|
[Parameter(Mandatory=$false)][Int]$NumCpu,
|
||||||
|
[Parameter(Mandatory=$false)][Int]$MemoryMB,
|
||||||
|
[Parameter(Mandatory=$false)][Boolean]$PowerOn=$false
|
||||||
|
)
|
||||||
|
|
||||||
|
$vmtxService = Get-CisService -Name "com.vmware.vcenter.vm_template.library_items"
|
||||||
|
$vmtxId = (Get-ContentLibraryItem -Name $VMTXName).Id
|
||||||
|
$folderId = ((Get-Folder -Name $FolderName).ExtensionData.MoRef).Value
|
||||||
|
$rpId = ((Get-ResourcePool -Name $ResourcePoolName).ExtensionData.MoRef).Value
|
||||||
|
$datastoreId = ((Get-Datastore -Name $DatastoreName).ExtensionData.MoRef).Value
|
||||||
|
|
||||||
|
$vmtxDeploySpec = $vmtxService.Help.deploy.spec.Create()
|
||||||
|
$vmtxDeploySpec.name = $NewVMName
|
||||||
|
$vmtxDeploySpec.powered_on = $PowerOn
|
||||||
|
$vmtxDeploySpec.placement.folder = $folderId
|
||||||
|
$vmtxDeploySpec.placement.resource_pool = $rpId
|
||||||
|
$vmtxDeploySpec.vm_home_storage.datastore = $datastoreId
|
||||||
|
$vmtxDeploySpec.disk_storage.datastore = $datastoreId
|
||||||
|
|
||||||
|
if($NumCpu) {
|
||||||
|
$vmtxDeploySpec.hardware_customization.cpu_update.num_cpus = $NumCpu
|
||||||
|
}
|
||||||
|
if($MemoryMB) {
|
||||||
|
$vmtxDeploySpec.hardware_customization.memory_update.memory = $MemoryMB
|
||||||
|
}
|
||||||
|
|
||||||
|
Write-Host "`nDeploying new VM $NewVMName from VMTX Template $VMTXName ..."
|
||||||
|
$results = $vmtxService.deploy($vmtxId,$vmtxDeploySpec)
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user