Automatically create virtual network card associated Virtual switch when importing Hyper-V virtual machines in bulk

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.