Hyper-V批次建立虛擬機器

项希盛發表於2024-05-27
Function MyNewVM($vmItem) {
    New-VHD -Path $vmItem.VhdVMPath -ParentPath $vmItem.VhdParentPath
    New-VM -Name $vmItem.GuestName -Generation $vmItem.Generation -Path $vmItem.osDirBase -MemoryStartupBytes $vmItem.MemoryStartupBytes -VHDPath $vmItem.VhdVMPath -SwitchName $vmItem.SwitchName
    Rename-VMNetworkAdapter -VMName $vmItem.GuestName -NewName "eth0"
    Set-VMNetworkAdapter -VMName $vmItem.GuestName -Name "eth0" -StaticMacAddress $vmItem.StaticMacAddress
    Set-VMMemory -VMName $vmItem.GuestName -DynamicMemoryEnabled 1 -MinimumBytes $vmItem.MemoryMinimumBytes -MaximumBytes $vmItem.MemoryMaximumBytes
    Set-VMProcessor -VMName $vmItem.GuestName -Count $vmItem.ProcessorCount -Maximum $vmItem.ProcessorMaximum
}
$baseDir = "C:\Hyper-V\os";
$osName = "os-ft-20240526";
$vmItem=@{}
$vmItem.Generation = 1;
$vmItem.osDirBase = Join-Path -Path $baseDir -ChildPath $osName
$vmItem.VhdParentPath = $osDir + ".vhdx"
$vmItem.SwitchName = "wan"
$vmItem.MemoryStartupBytes = 512MB#啟動記憶體
$vmItem.MemoryMinimumBytes = 512MB#最低記憶體
$vmItem.MemoryMaximumBytes = 1GB#最大記憶體
$vmItem.ProcessorCount = 1#CPU核心數
$vmItem.ProcessorMaximum = 100#CPU限制
$vmItem.StaticMacAddress = "00155D010203"
for ($i=2; $i -le 30; $i++) {
    $vmItem.GuestName = "ft-" + $i
    $vmItem.vmDir = Join-Path -Path $osDirBase -ChildPath $vmItem.GuestName;
    $vmItem.VhdVMPath = Join-Path -Path $vmItem.vmDir -ChildPath ($vmItem.GuestName + ".vhdx")
    MyNewVM($vmItem)
}

相關文章