2014-02-16 19:31:33 +00:00
|
|
|
Param(
|
2014-02-16 19:42:20 +00:00
|
|
|
[Parameter(Mandatory=$true)]
|
|
|
|
[string]$vm_xml_config,
|
|
|
|
[Parameter(Mandatory=$true)]
|
2014-07-17 09:50:51 +00:00
|
|
|
[string]$image_path,
|
2014-02-27 07:47:28 +00:00
|
|
|
|
2015-01-14 14:59:01 +00:00
|
|
|
[string]$switchname=$null,
|
|
|
|
[string]$memory=$null,
|
|
|
|
[string]$maxmemory=$null,
|
|
|
|
[string]$cpus=$null,
|
2016-07-26 12:41:01 +00:00
|
|
|
[string]$vmname=$null,
|
2016-07-26 12:56:20 +00:00
|
|
|
[string]$auto_start_action=$null,
|
|
|
|
[string]$auto_stop_action=$null
|
2014-02-16 19:31:33 +00:00
|
|
|
)
|
|
|
|
|
2014-02-15 23:29:16 +00:00
|
|
|
# Include the following modules
|
2014-02-16 19:31:33 +00:00
|
|
|
$Dir = Split-Path $script:MyInvocation.MyCommand.Path
|
2014-02-16 19:42:20 +00:00
|
|
|
. ([System.IO.Path]::Combine($Dir, "utils\write_messages.ps1"))
|
2014-02-15 23:29:16 +00:00
|
|
|
|
2014-02-16 19:31:33 +00:00
|
|
|
[xml]$vmconfig = Get-Content -Path $vm_xml_config
|
2014-02-15 23:29:16 +00:00
|
|
|
|
2014-08-09 17:30:10 +00:00
|
|
|
$generation = [int]($vmconfig.configuration.properties.subtype.'#text')+1
|
2014-02-15 23:29:16 +00:00
|
|
|
|
2015-01-14 14:59:01 +00:00
|
|
|
if (!$vmname) {
|
|
|
|
# Get the name of the vm
|
|
|
|
$vm_name = $vmconfig.configuration.properties.name.'#text'
|
|
|
|
}else {
|
|
|
|
$vm_name = $vmname
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$cpus) {
|
|
|
|
# Get the name of the vm
|
|
|
|
$processors = $vmconfig.configuration.settings.processors.count.'#text'
|
|
|
|
}else {
|
|
|
|
$processors = $cpus
|
|
|
|
}
|
|
|
|
|
2014-02-16 19:31:33 +00:00
|
|
|
function GetUniqueName($name) {
|
2014-02-15 23:29:16 +00:00
|
|
|
Get-VM | ForEach-Object -Process {
|
2014-02-16 19:31:33 +00:00
|
|
|
if ($name -eq $_.Name) {
|
|
|
|
$name = $name + "_1"
|
|
|
|
}
|
2014-02-15 23:29:16 +00:00
|
|
|
}
|
|
|
|
return $name
|
2014-02-16 19:31:33 +00:00
|
|
|
}
|
2014-02-15 23:29:16 +00:00
|
|
|
|
2014-02-16 19:31:33 +00:00
|
|
|
do {
|
2014-02-15 23:29:16 +00:00
|
|
|
$name = $vm_name
|
2014-02-16 19:31:33 +00:00
|
|
|
$vm_name = GetUniqueName $name
|
|
|
|
} while ($vm_name -ne $name)
|
|
|
|
|
2015-01-14 14:59:01 +00:00
|
|
|
if (!$memory) {
|
|
|
|
$xmlmemory = (Select-Xml -xml $vmconfig -XPath "//memory").node.Bank
|
|
|
|
if ($xmlmemory.dynamic_memory_enabled."#text" -eq "True") {
|
|
|
|
$dynamicmemory = $True
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$dynamicmemory = $False
|
|
|
|
}
|
|
|
|
# Memory values need to be in bytes
|
|
|
|
$MemoryMaximumBytes = ($xmlmemory.limit."#text" -as [int]) * 1MB
|
|
|
|
$MemoryStartupBytes = ($xmlmemory.size."#text" -as [int]) * 1MB
|
|
|
|
$MemoryMinimumBytes = ($xmlmemory.reservation."#text" -as [int]) * 1MB
|
2014-02-16 19:31:33 +00:00
|
|
|
}
|
|
|
|
else {
|
2015-01-14 14:59:01 +00:00
|
|
|
if (!$maxmemory){
|
|
|
|
$dynamicmemory = $False
|
|
|
|
$MemoryMaximumBytes = ($memory -as [int]) * 1MB
|
|
|
|
$MemoryStartupBytes = ($memory -as [int]) * 1MB
|
|
|
|
$MemoryMinimumBytes = ($memory -as [int]) * 1MB
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$dynamicmemory = $True
|
|
|
|
$MemoryMaximumBytes = ($maxmemory -as [int]) * 1MB
|
|
|
|
$MemoryStartupBytes = ($memory -as [int]) * 1MB
|
|
|
|
$MemoryMinimumBytes = ($memory -as [int]) * 1MB
|
|
|
|
}
|
2014-02-16 19:31:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-02-27 07:47:28 +00:00
|
|
|
if (!$switchname) {
|
|
|
|
# Get the name of the virtual switch
|
|
|
|
$switchname = (Select-Xml -xml $vmconfig -XPath "//AltSwitchName").node."#text"
|
|
|
|
}
|
2014-02-16 19:31:33 +00:00
|
|
|
|
2015-10-08 00:25:09 +00:00
|
|
|
if ($generation -eq 1) {
|
|
|
|
# Determine boot device
|
|
|
|
Switch ((Select-Xml -xml $vmconfig -XPath "//boot").node.device0."#text") {
|
|
|
|
"Floppy" { $bootdevice = "Floppy" }
|
|
|
|
"HardDrive" { $bootdevice = "IDE" }
|
|
|
|
"Optical" { $bootdevice = "CD" }
|
|
|
|
"Network" { $bootdevice = "LegacyNetworkAdapter" }
|
|
|
|
"Default" { $bootdevice = "IDE" }
|
|
|
|
} #switch
|
|
|
|
} else {
|
|
|
|
# Determine boot device
|
|
|
|
Switch ((Select-Xml -xml $vmconfig -XPath "//boot").node.device0."#text") {
|
|
|
|
"HardDrive" { $bootdevice = "VHD" }
|
|
|
|
"Optical" { $bootdevice = "CD" }
|
|
|
|
"Network" { $bootdevice = "NetworkAdapter" }
|
|
|
|
"Default" { $bootdevice = "VHD" }
|
|
|
|
} #switch
|
|
|
|
}
|
2014-02-16 19:31:33 +00:00
|
|
|
|
2015-01-17 20:31:37 +00:00
|
|
|
# Determine secure boot options
|
|
|
|
$secure_boot_enabled = (Select-Xml -xml $vmconfig -XPath "//secure_boot_enabled").Node."#text"
|
|
|
|
|
2014-02-16 19:31:33 +00:00
|
|
|
# Define a hash map of parameter values for New-VM
|
|
|
|
|
|
|
|
$vm_params = @{
|
|
|
|
Name = $vm_name
|
|
|
|
NoVHD = $True
|
|
|
|
MemoryStartupBytes = $MemoryStartupBytes
|
|
|
|
SwitchName = $switchname
|
|
|
|
BootDevice = $bootdevice
|
|
|
|
ErrorAction = "Stop"
|
|
|
|
}
|
|
|
|
|
2016-03-05 07:42:17 +00:00
|
|
|
# Generation parameter was added in ps v4
|
|
|
|
if((get-command New-VM).Parameters.Keys.Contains("generation")) {
|
|
|
|
$vm_params.Generation = $generation
|
|
|
|
}
|
|
|
|
|
2014-02-16 19:31:33 +00:00
|
|
|
# Create the VM using the values in the hash map
|
|
|
|
|
|
|
|
$vm = New-VM @vm_params
|
|
|
|
|
|
|
|
$notes = (Select-Xml -xml $vmconfig -XPath "//notes").node.'#text'
|
|
|
|
|
|
|
|
# Set-VM parameters to configure new VM with old values
|
|
|
|
|
|
|
|
$more_vm_params = @{
|
|
|
|
ProcessorCount = $processors
|
2014-02-27 05:29:55 +00:00
|
|
|
MemoryStartupBytes = $MemoryStartupBytes
|
2014-02-16 19:31:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
If ($dynamicmemory) {
|
|
|
|
$more_vm_params.Add("DynamicMemory",$True)
|
2014-02-27 05:29:55 +00:00
|
|
|
$more_vm_params.Add("MemoryMinimumBytes",$MemoryMinimumBytes)
|
|
|
|
$more_vm_params.Add("MemoryMaximumBytes", $MemoryMaximumBytes)
|
|
|
|
} else {
|
2014-02-15 23:29:16 +00:00
|
|
|
$more_vm_params.Add("StaticMemory",$True)
|
2014-02-16 19:31:33 +00:00
|
|
|
}
|
2014-02-15 23:29:16 +00:00
|
|
|
|
2014-02-16 19:31:33 +00:00
|
|
|
if ($notes) {
|
2014-02-15 23:29:16 +00:00
|
|
|
$more_vm_params.Add("Notes",$notes)
|
2014-02-16 19:31:33 +00:00
|
|
|
}
|
2014-02-15 23:29:16 +00:00
|
|
|
|
2016-07-26 12:41:01 +00:00
|
|
|
if ($auto_start_action) {
|
2016-07-26 12:56:20 +00:00
|
|
|
$more_vm_params.Add("AutomaticStartAction",$auto_start_action)
|
2016-07-26 12:41:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($auto_stop_action) {
|
2016-07-26 12:56:20 +00:00
|
|
|
$more_vm_params.Add("AutomaticStopAction",$auto_stop_action)
|
2016-07-26 12:41:01 +00:00
|
|
|
}
|
|
|
|
|
2014-02-16 19:31:33 +00:00
|
|
|
# Set the values on the VM
|
|
|
|
$vm | Set-VM @more_vm_params -Passthru
|
2014-02-15 23:29:16 +00:00
|
|
|
|
2014-02-16 19:31:33 +00:00
|
|
|
# Add drives to the virtual machine
|
|
|
|
$controllers = Select-Xml -xml $vmconfig -xpath "//*[starts-with(name(.),'controller')]"
|
2014-02-27 05:29:55 +00:00
|
|
|
|
2015-03-30 13:00:16 +00:00
|
|
|
# Only set EFI secure boot for Gen 2 machines, not gen 1
|
|
|
|
if ($generation -ne 1) {
|
2016-07-26 12:56:20 +00:00
|
|
|
# Set EFI secure boot
|
|
|
|
if ($secure_boot_enabled -eq "True") {
|
|
|
|
Set-VMFirmware -VM $vm -EnableSecureBoot On
|
|
|
|
} else {
|
|
|
|
Set-VMFirmware -VM $vm -EnableSecureBoot Off
|
|
|
|
}
|
2015-01-17 20:31:37 +00:00
|
|
|
}
|
|
|
|
|
2014-02-16 19:31:33 +00:00
|
|
|
# A regular expression pattern to pull the number from controllers
|
|
|
|
[regex]$rx="\d"
|
2014-02-15 23:29:16 +00:00
|
|
|
|
2014-02-16 19:31:33 +00:00
|
|
|
foreach ($controller in $controllers) {
|
2014-02-15 23:29:16 +00:00
|
|
|
$node = $controller.Node
|
2014-02-27 05:29:55 +00:00
|
|
|
|
|
|
|
# Check for SCSI
|
|
|
|
if ($node.ParentNode.ChannelInstanceGuid) {
|
|
|
|
$ControllerType = "SCSI"
|
|
|
|
} else {
|
|
|
|
$ControllerType = "IDE"
|
|
|
|
}
|
2014-02-15 23:29:16 +00:00
|
|
|
|
|
|
|
$drives = $node.ChildNodes | where {$_.pathname."#text"}
|
|
|
|
foreach ($drive in $drives) {
|
2014-02-27 05:29:55 +00:00
|
|
|
#if drive type is ISO then set DVD Drive accordingly
|
2014-02-15 23:29:16 +00:00
|
|
|
$driveType = $drive.type."#text"
|
|
|
|
|
2014-02-27 05:29:55 +00:00
|
|
|
$addDriveParam = @{
|
|
|
|
ControllerNumber = $rx.Match($controller.node.name).value
|
2014-07-17 09:50:51 +00:00
|
|
|
Path = $image_path
|
2014-02-27 05:29:55 +00:00
|
|
|
}
|
|
|
|
|
2014-02-15 23:29:16 +00:00
|
|
|
if ($drive.pool_id."#text") {
|
2014-02-16 19:31:33 +00:00
|
|
|
$ResourcePoolName = $drive.pool_id."#text"
|
2014-02-27 05:29:55 +00:00
|
|
|
$addDriveParam.Add("ResourcePoolname",$ResourcePoolName)
|
2014-02-15 23:29:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($drivetype -eq 'VHD') {
|
|
|
|
$addDriveParam.add("ControllerType",$ControllerType)
|
2014-02-27 05:29:55 +00:00
|
|
|
$vm | Add-VMHardDiskDrive @AddDriveparam
|
2014-02-15 23:29:16 +00:00
|
|
|
}
|
|
|
|
}
|
2014-02-16 19:31:33 +00:00
|
|
|
}
|
2014-02-15 23:29:16 +00:00
|
|
|
|
2014-02-16 19:31:33 +00:00
|
|
|
$vm_id = (Get-VM $vm_name).id.guid
|
|
|
|
$resultHash = @{
|
2014-02-15 23:29:16 +00:00
|
|
|
name = $vm_name
|
2014-02-27 05:29:55 +00:00
|
|
|
id = $vm_id
|
2014-02-15 23:29:16 +00:00
|
|
|
}
|
2014-02-27 05:29:55 +00:00
|
|
|
|
2014-02-16 19:31:33 +00:00
|
|
|
$result = ConvertTo-Json $resultHash
|
|
|
|
Write-Output-Message $result
|