copy VM's at the command line of Core Server 2008 R2

Assume I have a VM created running on Core Server 2008 R2 that is configured the way I like and I would now like to clone the configuration via some type of copy function at the command line of the console of the Core Server.  The idea would be that the original would be a template VM configuration complete with blank VHD and that every time I wanted to create a new VM I would have a way of copying all the relevant files/folders and doing some renaming and I would have a new VM ready to go.  I know I could do this monkeying around with the HyperV GUI but the point here is whether it's possible through a command line/series of command lines copying function on the console of the core server.
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Syed Mutahir AliTechnology ConsultantCommented:
As for the template vm, this is what I do and suggest :

I use quickvhd tool to quickly create a fixed size (you can create dynamic size vhds too) vhd, install Operating system, updates, programs etc and then sysprep it and shutdown. I then copy / move the vhd to a local storage or external storage for future use.

Install Powershell on your server core or hyper-v server :

First you would have to install .NET and then powershell (Follow the technet forum link)

Download the Powershell Management Library for Hyper-V :

Then follow this article to add the Powershell Mgmt Library and start using Powershell for your Hyper-V mgmt tasks :

Following are the commands from the Powershell Library download at codeplex:

Finding a VM
Get-VM, Choose-VM  , Get-VMHost

Connecting  to a VM

Discovering and  manipulating Machine states
Get-VMState , Set-VMState ,  Convert-VmState,  
Ping-VM , Test-VMHeartBeat, Shutdown-VM ,  Start-VM,  Stop-VM, Suspend-VM
Get-VMKVP, Add-KVP, Remove-KVP,  Get-VMJPEG

Backing up, exporting and snapshotting VMs
Export-VM  , Import-VM, Get-VMSnapshot, Choose-VMSnapshot , Apply-VMSnapshot ,  New-VMSnapshot ,Remove-VMSnapshot, Rename-VMSnapShot, Update-VMSnapshot,  Get-VMSnapshotTree, Get-VmBackupScript

Adding and removing  VMs, configuring motherboard settings.
New-VM , Remove-VM  ,  Set-VM , Get-VMCPUCount, Set-VMCPUCount, Get-VMMemory, Set-VMMemory,  Set-VMSerialPort

Manipulating Disk controllers, drives and  disk images
Add-VMSCSIController ,  Remove-VMSCSIcontroller
Get-VMDriveByController , Add-VMDRIVE ,  Remove-VMdrive
Get-VMDiskByDrive, Add-VMDISK , Set-VMDisk, Get-VMDisk
Get-VMFloppyDisk  , Add-VMFloppyDisk

Manipluating Network  Interface Cards
Get-VMNic , List-VMNic , Choose-VMNIC, Add-VMNIC,   Remove-VMNIC , Set-VMNICAddress , Set-VMNICConnection , Get-VMNicport ,  
Get-VMnicSwitch, Choose-VMSwitch, New-VMSwitchPort,  Get-VMByMACaddress, Choose-VMExternalEthernet,
New-VMExternalSwitch,  New-VMInternalSwitch,New-VmPrivateSwitch

Working with VHD  files
Get-VHDDefaultPath, Get-VHDInfo, New-VHD, Compact-VHD,  Test-VHD,Convert-VHD,Merge-VHD,Mount-VHD, Unmount-VHD

So, once you have all setup with powershell etc, you can then create a new vm by "New-VM" command and then copying the file (VHD) over to the location.


You can use the script from the following location (I have pasted the script too) :

function New-HyperVVM {
	param	(
			[string]$Hypervhost = "localhost",
			[string]$Vm = "VM Courtesy of PowerShell",
			[string]$location = "C:\MyVirtualMachines\$vm"
	$wmiClassString = "\\" + $Hypervhost + "\root\virtualization:Msvm_VirtualSystemGlobalSettingData"
	$wmiclass = [WMIClass]$wmiClassString
	$newVSGlobalSettingData = $wmiClass.CreateInstance()
	$newVSGlobalSettingData.psbase.Properties.Item("ExternalDataRoot").value = $location
	$newVSGlobalSettingData.psbase.Properties.Item("ElementName").value = $Vm
	$VSManagementService = gwmi MSVM_VirtualSystemManagementService -namespace "root\virtualization" -ComputerName $Hypervhost
	$GlobalSettings  = $newVSGlobalSettingData.psbase.GetText(1)
	$VSManagementService.DefineVirtualSystem($GlobalSettings, $ResourceSettings)

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Syed Mutahir AliTechnology ConsultantCommented:
Summarizing :

On Server core with hyper-v :

a) Install .Net and Powershell
b) Install / Import Hyper-v Powershell Library (codeplex link in my previous post)
c) Start using the available commands to create new, copy, move virtual machines and many others
d) you can use the script to create new vms with it.
e) Browse around the poshcode website to get new or different scripts.

Hope this helps.

lineonecorpAuthor Commented:
Thanks for all the info.  I may use it at other times. However, this time around I already have a VM/VHD exactly the way I like them. So how do I 'copy' them from the command line.  I guess I'm asking what I have to copy from what folders and what additional steps I have to do activate. For instance, I have a folder that seems to have virtual machines in it that's nested quite deep and at the end of the trail is an xml file with a long hexadecimal name which I'm assuming is a specific virtual machine file.  Can you provide me with any info on the default folder configuration for VMs/VHDs associated with them and then how I would copy/rename to create new ones - if that's possible.
lineonecorpAuthor Commented:
Still working my way through this.  I'll keep you updated.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Virtual Server

From novice to tech pro — start learning today.