次の方法で共有


Recreate a VM .. over and over again

For the kind of work I do, I have to create same kind of VM’s very frequently.. sometimes multiple times in a day. So I thought will automate the process and wrote a sample script… hope this helps.

 

Background

I am using Differencing Disks – the master HDD is a Windows 7 Trial , sysprep-ed and marked as Read-Only VHD. Locations are hardcoded. All I am doing is checking if the VM exists, stop and delete everything associated with the VM, then proceed to creating the differencing disk and then the VM. If the VM doesn’t exist, just create the VHD, VM and start the VM.

     

    1: $vm = (Get-VM -ComputerName TheHulk -Name MyWin7Trial).Name 
    2:  
    3: IF ($vm -eq "MyWin7Trial"){
    4: Stop-VM -Name MyWin7Trial -ComputerName TheHulk -Force -TurnOff
    5:  
    6: # delete existing VM
    7: Remove-VM -Name MyWin7Trial -ComputerName TheHulk -Force
    8: # Delete existing Differencing disk
    9: Remove-Item D:\RunningVHD\Win7Trial\* -recurse
   10: "Deleted VM"
   11: }
   12: # Create new VHDX
   13: New-VHD -ParentPath E:\MasterHDD\Win7MasterHDD.vhdx -Path D:\RunningVHD\Win7Trial\MyWinTrial.vhdx  -Differencing
   14: # Create VM
   15: New-VM -VHDPath D:\RunningVHD\Win7Trial\MyWinTrial.vhdx -ComputerName TheHulk -MemoryStartupBytes 4294967296 -Name MyWin7Trial -Path D:\RunningVHD\Win7Trial\
   16: Start-VM -Name MyWin7Trial -ComputerName TheHulk

   

This is a very simple sample… make it as complicated as you need it!

 

Cheers Smile,

Priyo Lahiri