Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- $originalhypervhost = "hyper-v source"
- $destinationhypervhost = "hyper-v destination"
- $VMList = Get-VM -ComputerName $originalhypervhost a-*
- foreach ($vm in $VMList) {
- if($vm.Name -notmatch ".*(aspecificname|anothername).*") {
- $newVMName = $vm.name -replace "^a-","b-"
- $newVMStartupMemory = $vm.MemoryStartup
- # Creating VM
- New-VM -Name $newVMName -MemoryStartupBytes $newVMStartupMemory -SwitchName vEthernetSwitch -Generation 2 -ComputerName $destinationhypervhost
- #Updating processor count and enabling compatibility
- $newVMProcessorCount = $vm.ProcessorCount
- Set-VMProcessor -VMName $newVMName -Count $newVMProcessorCount -CompatibilityForMigrationEnabled $true -ComputerName $destinationhypervhost
- #If dynamic memory is enabled, update memory settings
- if ($vm.DynamicMemoryEnabled -eq $true) {
- $newVMMaxMemory = $vm.MemoryMaximum
- $newVMMinMemory = $vm.MemoryMinimum
- Set-VMMemory -DynamicMemoryEnabled $true -MinimumBytes $newVMMinMemory -MaximumBytes $newVMMaxMemory -VMName $newVMName -ComputerName $destinationhypervhost
- }
- #setup disks
- foreach ($disk in $VM.harddrives) {
- $diskInfo = $disk.path | get-vhd -ComputerName $originalhypervhost
- $diskPath = $disk.Path -replace "\\a-","\b-"
- New-VHD -Path $diskPath -SizeBytes $diskInfo.Size -Dynamic -ComputerName $destinationhypervhost
- Add-VMHardDiskDrive -VMName $newVMName -Path $diskPath -ComputerName $destinationhypervhost
- }
- #setup dvd drive
- Add-VMDvdDrive -VMName $newVMName -ComputerName $destinationhypervhost
- Set-VMDvdDrive -VMName $newVMName -Path "C:\iso\WindowsServer2019-VirtIO.ISO" -ComputerName $destinationhypervhost
- if($newVMName -match ".*(listofnames).*") {
- Set-VMDvdDrive -VMName $newVMName -Path "C:\iso\ubuntu-18.04.1-live-server-amd64.ISO" -ComputerName $destinationhypervhost
- }
- if($newVMName -match ".*(listofnames).*") {
- Set-VMDvdDrive -VMName $newVMName -Path "C:\iso\CentOS-7-x86_64-Minimal-1810.ISO" -ComputerName $destinationhypervhost
- }
- $dvd = Get-VMDvdDrive -VMName $newVMName -ComputerName $destinationhypervhost
- Set-VMFirmware -VMName $newVMName -FirstBootDevice $dvd -ComputerName $destinationhypervhost
- #Setup the vlan
- $newVMVlan = ($vm | Get-VMNetworkAdapterVlan).AccessVlanId
- Set-VMNetworkAdapterVlan $newVMName -Access -VlanId $newVMVlan -ComputerName $destinationhypervhost
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement