ewreew 发表于 2017-3-15 09:49:49

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

                      $imageAll=Get-ChildItem “D:\Hyper-V\master image”    #the path of the parent vhd images   
#$imageAll.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 vmconnecting 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
                  

页: [1]
查看完整版本: Hyper-v powershell 基于差异盘创建虚拟机