Skip to content

Instantly share code, notes, and snippets.

@Novakov
Created August 8, 2015 19:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save Novakov/c640d65f1bf4cc7d7b7a to your computer and use it in GitHub Desktop.
Save Novakov/c640d65f1bf4cc7d7b7a to your computer and use it in GitHub Desktop.
[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)
@ForNeVeR
Copy link

Hello, @Novakov. What's the license of this script? Could I use it as part of my contribution to GPL-licensed https://github.com/pyranja/vbox-centos6-hyperv?

@ForNeVeR
Copy link

Unfortunately, it doesn't work for me. The script complains about not being able to convert Microsoft.HyperV.PowerShell.HardDiskDrive to Microsoft.HyperV.PowerShell.Drive at line 44.

@cmosh
Copy link

cmosh commented Jul 29, 2016

Hi @ForNeVeR, the script only works with SCSI controllers. Please remove all IDE controllers it should work then.

@aharkness
Copy link

@cmosh, I also encounter the drive conversion issue as @ForNeVeR, but in the SCSI section. When I initially set up my VM, I was able to run this against it. Only after I deleted, then imported that VM, and THEN ran this script against the VM did I actually run into this problem.

Get-Drives : Cannot process argument transformation on parameter 'drives'. Cannot convert the "HardDiskDrive (Name = 'Hard Drive on SCSI controller number 0 at location 0', VMName = 
ubuntu1604tcbase') [Id = 'Microsoft:540EFF01-0895-458E-806D-C8FD06101DF2\FA75C42E-2490-4D4C-98F1-7C7D82455EE4\0\0\D', VMId = '540eff01-0895-458e-806d-c8fd06101df2']" value of type 
"Microsoft.HyperV.PowerShell.HardDiskDrive" to type "Microsoft.HyperV.PowerShell.Drive".
At <mypath>\generate_vm_xml_config.ps1:56 char:16
+          $(Get-Drives($_.Drives))
+                      ~~~~~~~~~~~
    + CategoryInfo          : InvalidData: (:) [Get-Drives], ParameterBindingArgumentTransformationException
    + FullyQualifiedErrorId : ParameterArgumentTransformationError,Get-Drives

I'm going to do some more troubleshooting, will let you know if I figure it out.

@kiquenet
Copy link

kiquenet commented May 3, 2021

How-to use it ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment