#Requires -Version 4.0 <# .SYNOPSIS Quickly creates some linked clones .DESCRIPTION Quickly creates one or more linked clone virtual machines on a VMWare vSphere vCenter platform, mostly to be used for development / test purposes .PARAMETER ServerName Specifies the IP address or the DNS name of the vSphere server to which you want to connect. You can also specify a server by providing its IPv6 address enclosed in square brackets, for example [fe80::56ff:feb0:74bd]. .PARAMETER Credential Specifies a PSCredential object that contains credentials for authenticating with the server. .PARAMETER VMName Specifies the name of the virtual machine which will be worked on. .PARAMETER SnapshotName Specifies the virtual machine's shapshot name. THe snapshot must exist. .PARAMETER Server Specifies a VIServer object to use. You can create a VIServer object using VMWare's Connect-VIServer cmdlet. .PARAMETER Snapshot Specifies a Snapshot object to use. Can be created using VMWare's Get-Snapshot cmdlet. .PARAMETER LinkedClone Specifies the linked clone(s) which will be created. Can be a string which will be used as vm name, an array containing strings, a hashtable containing a key named 'Name' [String] and optionally a key named 'Spec' [hashtable] or an array containing hashtables. Example: @( @{ 'Name'='adc01' 'Spec'=@{'MemoryGB'=4;'NumCpu'=2;'CoresPerSocket'=1} }; @{ 'Name'='adc02' 'Spec'=@{'MemoryGB'=4;'NumCpu'=2;'CoresPerSocket'=1} }; ) The Spec-key takes every parameter which VMWare's Set-VM cmdlet also takes, but be very careful using the -VM and -VMHost parameters as they might have strange consequences. .NOTES Author: kurator | lagerhaus128.ch Revision 0: Initial revision .EXAMPLE Create-LinkedClone -ServerName 172.16.0.10 -Credential $cred -VMName 'mg01' -SnapshotName '20170607 raw linked clone base' -LinkedClone 'mg02' Creates a single linked clone without modifying the vm's specs. .EXAMPLE Create-LinkedClone -ServerName 172.16.0.10 -Credential $cred -VMName 'mg01' -SnapshotName '20170607 raw linked clone base' -LinkedClone @('mg03','mg04') Creates a two linked clone without modifying the vm's specs. .EXAMPLE Create-LinkedClone -ServerName 172.16.0.10 -Credential $cred -VMName 'mg01' -SnapshotName '20170607 raw linked clone base' -LinkedClone @{'Name'='ap01';'Spec'=@{'MemoryGB'=8}} Creates a single linked clone and modifies the vm's memory size to 8GB. .EXAMPLE Create-LinkedClone -ServerName 172.16.0.10 -Credential $cred -VMName 'mg01' -SnapshotName '20170607 raw linked clone base' -LinkedClone @(@{'Name'='adc01';'Spec'=@{'MemoryGB'=4;'NumCpu'=2;'CoresPerSocket'=1}};@{'Name'='adc02';'Spec'=@{'MemoryGB'=4;'NumCpu'=2;'CoresPerSocket'=1}};@{'Name'='ap02';'Spec'=@{'MemoryGB'=10;'NumCpu'=2;'CoresPerSocket'=2}}) Creates a two linked clone and modifies the vm's specs. #> Function Create-LinkedClone { [CmdletBinding()] param( [Parameter(ParameterSetName="Strings",Mandatory="true")][String]$ServerName, [Parameter(ParameterSetName="Strings",Mandatory="true")][PSCredential]$Credential, [Parameter(ParameterSetName="Strings",Mandatory="true")][String]$VMName, [Parameter(ParameterSetName="Strings",Mandatory="true")][String]$SnapshotName, [Parameter(ParameterSetName="Objects",Mandatory="true")][VMware.VimAutomation.ViCore.Impl.V1.VIServerImpl]$Server, [Parameter(ParameterSetName="Objects",Mandatory="true")][VMware.VimAutomation.ViCore.Impl.V1.VM.SnapshotImpl]$Snapshot, [Parameter(Mandatory="true")]$LinkedClone ) Try { Import-Module VMware.VimAutomation.Core if($PSCmdlet.ParameterSetName -eq 'Strings') { $Server = Connect-VIServer -Server $ServerName -NotDefault -Credential $Credential $Snapshot = (Get-Snapshot -Name $SnapshotName -Server $Server -VM (Get-VM -Name $VMName -Server $Server)) } switch($LinkedClone.GetType()) { ('').GetType() {$LinkedClone = @(@{'Name' = $LinkedClone})} (@{}).GetType() {$LinkedClone = @($LinkedClone)} } $LinkedClone | ForEach-Object { if($_.GetType() -eq ('').GetType()) { $_ = @{'Name' = $_} } $VMNew = New-VM -Name $_.Name -VM $Snapshot.VM -LinkedClone -ReferenceSnapshot $Snapshot -ResourcePool $Snapshot.VM.ResourcePool -Server $Server -Location $VM.Folder if($_.Spec) { $Spec = $_.Spec $Spec.Keys | ForEach-Object { Invoke-Expression -Command ('Set-VM -VM $VMNew -Confirm:$false -{0} {1}' -f $_, $Spec.$_ ) } Remove-Variable Spec } } Remove-Variable VMNew if($PSCmdlet.ParameterSetName -eq 'Strings') { Disconnect-VIServer -Server $Server -Confirm:$false Remove-Variable Snapshot,Server } } Catch { Write-Error $_.Exception.Message } } Export-ModuleMember -Function Create-LinkedClone