Every time you get a virtual machine for an experiment, you get a copy of the electronic file that describes the virtual network settings needed to pre-create the Hyper-V virtual machine for the lab environment import, and some of the experimental types of virtual machines are too cumbersome to create, But do not create a virtual machine and can not import the environment of Hyper-V, so as lazy combat lazy, I wrote a script, basically to get to the virtual machine path to be imported, find the virtual machine configuration file, considering that multiple virtual networks overlap, so the location of each independent virtual machine network is preserved Finally, these virtual machine networks are matched to the virtual machine network in the current environment, and if not, imported.
Lazy people here is really lazy, so in fact, for the experimental virtual machine only need to import the private internal type is enough, our lab virtual machine environment does not provide an external network connection, if you really want to distinguish between internal and external network, there will be a lot more factors to consider!
Write the shell is not I am very good at, dare not say with caution, say only for reference and test use it!
$VMNetconfig = @()
$CurrentSwitches = (get-vmswitch) . Name
$VMpath = "Virtual machine path to import"
$VMconfigfiles = (get-childitem-recurse$VMPath\*. XML| Where-object {$_. BaseName -match"^[\da-za-z]{8}-[\da-za-z]{4}-[\da-za-z]{4}-[\da-za-z]{4}-[\da-za-z]{12}$" })
$VMconfigfiles | ForEach {
[XML]$VMconfig =get-content$_
$VMNetconfig += Select-xml -xml $VMconfig -xpath "//altswitchname" | % {$_. Node.' #text '}
}
[System.Collections.Generic.HashSet[string]]$UniqueNetconfig =$ Vmnetconfig. Split ("")
foreach ($UniqueNetin$UniqueNetconfig)
{
if ($CurrentSwitches-notcontains"$UniqueNet")
{
New-vmswitch -switchtype Private -name $UniqueNet -verbose
}
}
Automatically create virtual network card associated Virtual switch when importing Hyper-V virtual machines in bulk