Created
August 8, 2015 19:08
-
-
Save Novakov/c640d65f1bf4cc7d7b7a to your computer and use it in GitHub Desktop.
This file contains hidden or 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
[CmdletBinding()] | |
param( | |
[Parameter(Mandatory)] | |
[string]$VMName, | |
[Parameter(Mandatory)] | |
[string]$OutputFile | |
) | |
function Get-DriveType($drive) | |
{ | |
if($drive -is [Microsoft.HyperV.PowerShell.HardDiskDrive]) | |
{ | |
return "VHD" | |
} | |
if($drive -is [Microsoft.HyperV.PowerShell.DvdDrive]) | |
{ | |
return "ISO" | |
} | |
return "NONE" | |
} | |
function Get-Drives([Microsoft.HyperV.PowerShell.Drive[]]$drives) | |
{ | |
$drives | ForEach-Object { | |
$num = $_.DiskNumber + 0 | |
@" | |
<drive$num> | |
<pathname type="string">$($_.Path)</pathname> | |
<type type="string">$(Get-DriveType($_))</type> | |
</drive$num> | |
"@ | |
} | |
} | |
function Get-IdeControllers($vm) | |
{ | |
Get-VMIdeController -VM $vm | ForEach-Object { | |
@" | |
<controller$($_.ControllerNumber)> | |
$(Get-Drives($_.Drives)) | |
</controller$($_.ControllerNumber)> | |
"@ | |
} | |
} | |
function Get-ScsiControllers($vm) | |
{ | |
Get-VMScsiController -VM $vm | ForEach-Object { | |
@" | |
<controller$($_.ControllerNumber)> | |
$(Get-Drives($_.Drives)) | |
</controller$($_.ControllerNumber)> | |
"@ | |
} | |
} | |
function Get-Controllers($vm) | |
{ | |
if($vm.Generation -eq 1) | |
{ | |
Get-IdeControllers($vm) | |
} | |
else | |
{ | |
"<scsi ChannelInstanceGuid=`"x`">$(Get-ScsiControllers($vm))</scsi>" | |
} | |
} | |
$vm = Get-VM -Name $VMName | |
$adapter = Get-VMNetworkAdapter -VM $vm | select -First 1 | |
if($vm.Generation -eq 1) | |
{ | |
$bios = Get-VMBios -VM $vm | |
$startupOrder = $bios.StartupOrder | |
} | |
else | |
{ | |
$firmware = Get-VMFirmware -VM $vm | |
$startupOrder = $firmware.BootOrder | |
} | |
$xml = [xml]@" | |
<?xml version="1.0" ?> | |
<configuration> | |
<properties> | |
<subtype type="integer">$($vm.Generation - 1)</subtype> | |
<name type="string">$($vm.Name)</name> | |
</properties> | |
<settings> | |
<processors> | |
<count type="integer">$($vm.ProcessorCount)</count> | |
</processors> | |
<memory> | |
<bank> | |
<dynamic_memory_enabled type="bool">$($vm.DynamicMemoryEnabled)</dynamic_memory_enabled> | |
<limit type="integer">$($vm.MemoryMaximum / 1MB)</limit> | |
<reservation type="integer">$($vm.MemoryMinimum / 1MB)</reservation> | |
<size type="integer">$($vm.MemoryStartup / 1MB)</size> | |
</bank> | |
</memory> | |
</settings> | |
<AltSwitchName type="string">$($adapter.SwitchName)</AltSwitchName> | |
<boot> | |
<device0 type="string">Optical</device0> | |
</boot> | |
<secure_boot_enabled type="bool">False</secure_boot_enabled> <!-- TODO --> | |
<notes type="string">$($vm.Notes)</notes> | |
<vm-controllers> | |
$(Get-Controllers($vm)) | |
</vm-controllers> | |
</configuration> | |
"@ | |
$xml.Save($OutputFile) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How-to use it ?