贴一份创建Hyper-V虚拟机的代码:
function CreateVM
{
param
(
[string]$vmName = $(throw "param -vmName is required."),
[long]$vmRAMSize = $(throw "param -vmRAMSize is required."),
[long]$vmDiskSize = $(throw "param -vmDiskSize is required."),
[string]$storePath = $(throw "param -storePath is required."),
[string]$isoPath = $(throw "param -isoPath is required."),
[string]$internalLan = $(throw "param -internalLan is required."),
[string]$externalLan = $(throw "param -externalLan is required."),
[int]$cpuCount = $(throw "param -cpuCount is required."),
[string]$eth2ip = $(throw "param -eth2ip is required."),
[string]$eth2Netmask = $(throw "param -eth2Netmask is required."),
[string]$eth2GW = $(throw "param -eth2GW is required.")
)
$eth0="Eth0"
##delete existing VM and VM file which has same VMname
foreach($existingVMItem in Get-VM)
{
if($existingVMItem.Name -eq $vmName)
{
Remove-VM -Name $vmName -Force
}
}
$existingVMLoc=Test-Path $storePath
if(-not $existingVMLoc)
{
MD $storePath
}else
{
rmdir $storePath -Recurse -Force
}
new-vm $vmName -path $storePath
##delete existing VM switch by same switch name and
foreach($existingVMSwitch in Get-VMSwitch)
{
if(($existingVMSwitch.NetAdapterInterfaceDescription) -or ($existingVMSwitch.Name -eq $eth0) -or ($existingVMSwitch.Name -eq $eth1) -or ($existingVMSwitch.Name -eq $eth2))
{
Remove-VMSwitch -Name $existingVMSwitch.Name -Force
}
}
##add new VM switch
New-VMSwitch $eth0 -NetAdapterName $internalLan
##Define disk size
$vmDiskSize = $vmDiskSize*1024*1024*1024
New-VHD -Path $storePath\$vmName.vhdx -size $vmDiskSize
Add-VMHardDiskDrive -VMName $vmName -ControllerType ide -ControllerNumber 0 -path $storePath\$vmName.vhdx
##define ram size
$vmRAMSize = $vmRAMSize*1024*1024
get-vm $vmName | Set-VMMemory -DynamicMemoryEnabled $True -MinimumBytes $vmRAMSize -StartupBytes $vmRAMSize -MaximumBytes 4GB -Priority 80 -Buffer 25
##add DVD driver (iso)
Add-VMDvdDrive -VMName $vmName -Path $isoPath
##set CPU processor
Set-VMProcessor $vmName -Count $cpuCount -Reserve 10 -Maximum 90
##remove default VMNetworkAdapter which named Network Adapter
foreach($existingVMNetworkAdapter in Get-VMNetworkAdapter -VMName $vmName)
{
Remove-VMNetworkAdapter -VMName $vmName -Name $existingVMNetworkAdapter.Name
}
##add new VMNetworkAdapter
Add-VMNetworkAdapter -VMName $vmName -Name $eth0
Get-VMNetworkAdapter -VMName $vmName -Name $eth0 | Connect-VMNetworkAdapter -SwitchName $eth0
}下一篇再说C#下的调用
&spm=1001.2101.3001.5002&articleId=50904804&d=1&t=3&u=c97c8b69ef1a4452a7fccd5a61e11884)
2081

被折叠的 条评论
为什么被折叠?



