首页 > 系统相关 >Powershell 创建Hyper-v 虚拟机

Powershell 创建Hyper-v 虚拟机

时间:2023-02-28 18:03:39浏览次数:41  
标签:VHDPath Hyper 虚拟机 VMPath VM VHD Path VMName Powershell

function CreateVM{

Param(

    [Parameter(Mandatory=$true)]

    [string]$VMName,

    #can create a VM using an existing VHD template,if not create the new one.$VHDPath = Full path contains file name and extension.

    $VHDPath

)


# VM Path

$VMPath = "C:\hyper-v\" + $VMName

# VM VHD Path


# Create VM --Memory 4GB --Generation 2 --boot up CD

New-VM -Name $VMName -MemoryStartupBytes 4GB -Path $VMPath  -Generation 2 -SwitchName "Default Switch"  -BootDevice CD


# judge VM path exist --inexistence,Create

if(!(Test-Path $VMPath))

{

    New-Item -Path $VMPath -ItemType Directory

}


# use exist template VHD Create VM

if($VHDPath)

{

    Copy-Item -Path $VHDPath -Destination $VMPath

    Add-VMHardDiskDrive -VMName $VMName  -Path $VHDPath  -ControllerType SCSI

}

#if VHD inexistence, New VHD Create VM

else

{

    $VHD = New-VHD -Path $VMHDPath -Dynamic -SizeBytes 100GB

    Add-VMHardDiskDrive -VMName $VMName -Path $VHD.Path -ControllerType SCSI

    # Set ISO

    Set-VMDvdDrive -VMName $VMName -Path E:\ISO\en_windows_server_2019_updated_jun_2021_x64_dvd_a2a2f782.iso

}


# Set VM Processor Count and Disable AutomaticCheckpoints

Set-VMProcessor -VMName $VMName -Count 12 -ExposeVirtualizationExtensions $true -AutomaticCheckpointsEnabled $false


Start-VM $VMName

}



CreateVM -VMName H2019-DC01

Name       State CPUUsage(%) MemoryAssigned(M) Uptime   Status Version

----       ----- ----------- ----------------- ------   ------ -------

H2019-DC01 Off   0           0                 00:00:00 正y常¡ê运?行D   9.0   

PS C:\WINDOWS\system32> 

Powershell 创建Hyper-v 虚拟机_Powershll

Powershell 创建Hyper-v 虚拟机_创建虚机_02

标签:VHDPath,Hyper,虚拟机,VMPath,VM,VHD,Path,VMName,Powershell
From: https://blog.51cto.com/xingjia/6091655

相关文章