-
-
Save Silthus/62cc52469d05739dc2c7af292a00a5cb to your computer and use it in GitHub Desktop.
Export-VMTemplate
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
param ( | |
[parameter(Mandatory=$true)] | |
$VM, | |
[parameter(Mandatory=$true)] | |
[string] | |
$TemplateName, | |
[parameter(Mandatory=$false)] | |
$VMMServer, | |
[parameter(Mandatory=$false)] | |
$LibraryServer, | |
[switch] | |
$PassThru | |
) | |
. .\Menu.ps1 | |
$ErrorActionPreference = 'Stop' | |
if (-not $VMMServer) { | |
$VMMServer = [Microsoft.SystemCenter.VirtualMachineManager.Remoting.ServerConnection]::ActiveConnection | |
} elseif ($VMMServer -is [string]) { | |
$VMMServer = Get-SCVMMServer -ComputerName $VMMServer | |
} elseif ($VMMServer -isnot [Microsoft.SystemCenter.VirtualMachineManager.Remoting.ServerConnection]) { | |
throw 'VMMServer parameter must be a string or ServerConnection' | |
} | |
if ($VM -is [string]) { | |
$VM = Get-SCVirtualMachine -Name $VM | |
} elseif ($VM -isnot [Microsoft.SystemCenter.VirtualMachineManager.VM]) { | |
throw 'VM parameter must be a string or VM' | |
} | |
if (-not $LibraryServer) { | |
$LibraryServer = [Microsoft.SystemCenter.VirtualMachineManager.LibraryServer]($VMMServer | Get-SCLibraryServer) | |
} elseif ($LibraryServer -is [string]) { | |
$LibraryServer = Get-SCLibraryServer -ComputerName $LibraryServer | |
} elseif ($LibraryServer -isnot [Microsoft.SystemCenter.VirtualMachineManager.LibraryServer]) { | |
throw 'LibraryServer parameter must be a string or LibraryServer' | |
} | |
# Check VM Additions are installed | |
if (-not $VM.HasVMAdditions) { | |
throw 'VM must have VM Additions installed' | |
} | |
if (!(AskYesNo -Title "Local Admin Password correct?" -Message "Does the local Administrator account for $($VM.Name) have a blank password or for Linux root:root?")) { | |
throw 'Set the local Administrator account password to blank or root:root (on Linux) before creating a template.' | |
} | |
$WasRunning = $VM.Status -eq 'Running' | |
if ($VM.Status -ne 'PowerOff') { | |
Write-Host 'Shutting down VM...' | |
$VM | Stop-SCVirtualMachine | Out-Null | |
} | |
Write-Host 'Dismounting DVD drives...' | |
$VM.VirtualDVDDrives | | |
Where-Object { | |
$_.Connection -ne 'None' | |
} | | |
ForEach-Object { | |
$_ | Set-VirtualDVDDrive -NoMedia | Out-Null | |
} | |
$VMPath = $VM.VMHost.VMPaths | Select-Object -First 1 | |
$SharePath = Get-LibraryShare | | |
Where-Object { | |
$_.LibraryServer -eq $LibraryServer | |
} | | |
Select -First 1 -ExpandProperty Path | |
Write-Host 'Cloning VM...' | |
$CloneVM = New-SCVirtualMachine -VM $VM -VMHost $VM.VMHost -Name $TemplateName -Path $VMPath | |
Write-Host 'Creating template...' | |
$Template = New-SCVMTemplate -Name $TemplateName -VM $CloneVM -LibraryServer $LibraryServer -SharePath $SharePath | |
if ($WasRunning) { | |
Write-Host 'Restarting VM...' | |
$VM | Start-SCVirtualMachine | Out-Null | |
} | |
if ($PassThru) { | |
Write-Output $Template | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment