SHARE
TWEET

VM Configuration Duplication

a guest Jul 12th, 2019 74 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. $originalhypervhost = "hyper-v source"
  2. $destinationhypervhost = "hyper-v destination"
  3.  
  4. $VMList = Get-VM  -ComputerName $originalhypervhost a-*
  5.  
  6. foreach ($vm in $VMList) {
  7.  
  8.     if($vm.Name -notmatch ".*(aspecificname|anothername).*") {
  9.  
  10.         $newVMName = $vm.name -replace "^a-","b-"
  11.         $newVMStartupMemory = $vm.MemoryStartup
  12.         # Creating VM
  13.         New-VM -Name $newVMName -MemoryStartupBytes $newVMStartupMemory -SwitchName vEthernetSwitch -Generation 2 -ComputerName $destinationhypervhost
  14.  
  15.         #Updating processor count and enabling compatibility
  16.         $newVMProcessorCount = $vm.ProcessorCount
  17.         Set-VMProcessor -VMName $newVMName -Count $newVMProcessorCount -CompatibilityForMigrationEnabled $true -ComputerName $destinationhypervhost
  18.  
  19.         #If dynamic memory is enabled, update memory settings
  20.         if ($vm.DynamicMemoryEnabled -eq $true) {
  21.             $newVMMaxMemory = $vm.MemoryMaximum
  22.             $newVMMinMemory = $vm.MemoryMinimum
  23.             Set-VMMemory -DynamicMemoryEnabled $true -MinimumBytes $newVMMinMemory -MaximumBytes $newVMMaxMemory -VMName $newVMName -ComputerName $destinationhypervhost
  24.         }
  25.  
  26.         #setup disks
  27.         foreach ($disk in $VM.harddrives) {
  28.             $diskInfo = $disk.path | get-vhd -ComputerName $originalhypervhost
  29.             $diskPath = $disk.Path -replace "\\a-","\b-"
  30.  
  31.             New-VHD -Path $diskPath -SizeBytes $diskInfo.Size -Dynamic -ComputerName $destinationhypervhost
  32.             Add-VMHardDiskDrive -VMName $newVMName -Path $diskPath -ComputerName $destinationhypervhost
  33.         }
  34.  
  35.         #setup dvd drive
  36.         Add-VMDvdDrive -VMName $newVMName -ComputerName $destinationhypervhost
  37.         Set-VMDvdDrive -VMName $newVMName -Path "C:\iso\WindowsServer2019-VirtIO.ISO" -ComputerName $destinationhypervhost
  38.         if($newVMName -match ".*(listofnames).*") {
  39.             Set-VMDvdDrive -VMName $newVMName -Path "C:\iso\ubuntu-18.04.1-live-server-amd64.ISO" -ComputerName $destinationhypervhost
  40.         }
  41.         if($newVMName -match ".*(listofnames).*") {
  42.             Set-VMDvdDrive -VMName $newVMName -Path "C:\iso\CentOS-7-x86_64-Minimal-1810.ISO" -ComputerName $destinationhypervhost
  43.         }
  44.         $dvd = Get-VMDvdDrive -VMName $newVMName -ComputerName $destinationhypervhost
  45.         Set-VMFirmware  -VMName $newVMName -FirstBootDevice $dvd -ComputerName $destinationhypervhost
  46.  
  47.         #Setup the vlan
  48.         $newVMVlan = ($vm | Get-VMNetworkAdapterVlan).AccessVlanId
  49.         Set-VMNetworkAdapterVlan $newVMName -Access -VlanId $newVMVlan  -ComputerName $destinationhypervhost
  50.     }
  51. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top