首页 > 代码库 > Hyper-v powershell 基于差异盘创建虚拟机

Hyper-v powershell 基于差异盘创建虚拟机

$imageAll=Get-ChildItem “D:\Hyper-V\master image”    #the path of the parent vhd images  
#$imageAll[0].FullName    
$i=1    
#list all of the parent vhd images    
foreach ($image in $imageAll)    
        {          
           Write-Host "Type of OS" $i ($image)    
           $i++          
        }

$typeOS=read-host "Please Input the type of OS"  
$ParerentPath =$imageAll[$typeos-1].FullName

$vmName=read-host "Please Input the vm Name"  
$k=1    
while ($k -eq 1)  
        {                                          
            if ( Get-VM |? name -EQ $vmname )    
                   {    
                    echo "The vm existed!"    
                    $vmName=read-host "Please Input the vm Name"    
                    $k=1    
                                       }    
            else {$k=0}    
        }

$vhdpath = "D:\Hyper-V\Virtual Hard Disks\"+$vmName+".vhdx"

#$vhdsize = 300GB


New-VHD –ParentPath  $ParerentPath    –Path $vhdpath  -Differencing   #create diffencing vhd

$switch="inside"   #set vm  connecting to inside vswitch

#create new vm  
New-VM –Name $vmName -generation 2 –MemoryStartupBytes 1GB –VHDPath $vhdpath -SwitchName $switch

#setting the vm  
Set-VM $vmName  -DynamicMemory -MemoryMaximumBytes 4GB -AutomaticStartAction Nothing

Hyper-v powershell 基于差异盘创建虚拟机