none
How to change VM settings while importing VM

    Question

  • Hi all,

    I was hoping to be able to stand up new VMs with a minimum of fuss, but the Import-VM functionality seems woefully limited.

    I created Win2016Template, sysprepped it and then exported it.

    Now when I run Import-VM, the VHDs and the VM itself, retain the old settings, as I can't tell the import job that the "new"/imported VM should actually be called something else, Win2016DC01, and then name the VHDs and the VM itself correctly.

    I suppose I could fudge it with PowerShell one by one, but that's inconvenient if I want to stand up 10 VMs in one go..

    So, what is my best choice for importing/creating new VMs based on a template and ensuring they have the correct settings with a minimum of fuss? And by fuss I mean administrative work.


    Wednesday, February 1, 2017 11:47 AM

Answers

  • Import is doing exactly what it was designed to do.  It is even doing what the term 'import' means.

    I would simply take the sysprepped VHD and write a script to create multiple VMs from it, providing the name you want for each one. To make it easier yet, you could inject an unattend.xml into each one that could automatically configure a number of things for you. In fact, if you go to https://gallery.technet.microsoft.com/ you are likely to find scripts that others have written that may do something like what you are trying to do.  Pick one that comes close and modify it to your needs.


    . : | : . : | : . tim

    • Marked as answer by Panzerbjrn Monday, February 6, 2017 11:57 AM
    Wednesday, February 1, 2017 1:03 PM
  • Thanks, I gave up on importing, and scripted your suggestion.

    $Name = "WinDC01"
    $Path = "D:\VMs\"+$Name
    $SourceVHDFldr = "C:\Dropbox\VM-Templates"
    $SourceVHD = "Win2016-Template.vhdx"
    $DestinationVHDFldr = $Path+"\VHD"
    $DestinationVHD = $Path+"\VHD\"+$Name+".vhdx"
    $VMSwitch = Get-VMSwitch
    $SwitchName = $VMSwitch.Name
    $OldVHDName = $DestinationVHDFldr+"\"+$SourceVHD
    $NewVHDName = $DestinationVHD
    New-Item -ItemType "Directory" -Path $DestinationVHDFldr -Force
    Robocopy $SourceVHDFldr $DestinationVHDFldr $SourceVHD
    Rename-Item $OldVHDName -NewName $NewVHDName
    
    New-VM `
    -Name $Name `
    -BootDevice VHD `
    -ComputerName Localhost `
    -Generation 2 `
    -Path $Path `
    -SwitchName $SwitchName `
    -VHDPath $DestinationVHD

    That seems to be as close to what I wanted to do as I'm likely to get.

    • Marked as answer by Panzerbjrn Monday, February 6, 2017 11:57 AM
    Monday, February 6, 2017 11:57 AM

All replies

  • Import is doing exactly what it was designed to do.  It is even doing what the term 'import' means.

    I would simply take the sysprepped VHD and write a script to create multiple VMs from it, providing the name you want for each one. To make it easier yet, you could inject an unattend.xml into each one that could automatically configure a number of things for you. In fact, if you go to https://gallery.technet.microsoft.com/ you are likely to find scripts that others have written that may do something like what you are trying to do.  Pick one that comes close and modify it to your needs.


    . : | : . : | : . tim

    • Marked as answer by Panzerbjrn Monday, February 6, 2017 11:57 AM
    Wednesday, February 1, 2017 1:03 PM
  • Your option is to use PowerShell and script it.

    And your PowerShell option is to use the Compare-VM cmdlet, modify that output, and feed it into the Import VM cmdlet as a new VM configuration object.

    http://itproctology.blogspot.com/2012/08/handling-import-vm-errors-in-server.html


    Brian Ehlert
    http://ITProctology.blogspot.com
    Learn. Apply. Repeat.

    Wednesday, February 1, 2017 3:46 PM
    Moderator
  • Thanks, I gave up on importing, and scripted your suggestion.

    $Name = "WinDC01"
    $Path = "D:\VMs\"+$Name
    $SourceVHDFldr = "C:\Dropbox\VM-Templates"
    $SourceVHD = "Win2016-Template.vhdx"
    $DestinationVHDFldr = $Path+"\VHD"
    $DestinationVHD = $Path+"\VHD\"+$Name+".vhdx"
    $VMSwitch = Get-VMSwitch
    $SwitchName = $VMSwitch.Name
    $OldVHDName = $DestinationVHDFldr+"\"+$SourceVHD
    $NewVHDName = $DestinationVHD
    New-Item -ItemType "Directory" -Path $DestinationVHDFldr -Force
    Robocopy $SourceVHDFldr $DestinationVHDFldr $SourceVHD
    Rename-Item $OldVHDName -NewName $NewVHDName
    
    New-VM `
    -Name $Name `
    -BootDevice VHD `
    -ComputerName Localhost `
    -Generation 2 `
    -Path $Path `
    -SwitchName $SwitchName `
    -VHDPath $DestinationVHD

    That seems to be as close to what I wanted to do as I'm likely to get.

    • Marked as answer by Panzerbjrn Monday, February 6, 2017 11:57 AM
    Monday, February 6, 2017 11:57 AM