PowerShell Create virtual machines (second, batch creation)

Source: Internet
Author: User

In the previous article, we learned about how to create a virtual machine with PowerShell script (PS or PS script), and a custom virtual machine-related configuration, and we'll explore how to implement batch creation of virtual machines.

Batch creation, as the name implies, is to let the script run once, create more than one virtual machine, the simplest way to implement, is the loop, let's look at a piece of code together:

for ($i =1; $i-le10; $i + +) {$i}


This is one of the simplest loops in the PS script, and after running, the effect is as follows:

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/6F/99/wKiom1WijfPDpKZXAABs5v1bzgQ091.jpg "/>

The meaning of this PS script, is from 1 to 10, a total of 10 times, each time the thing to do, is the variable $i the result output to the screen.

OK, let's take another look at the code:

for ($i =1; $i-le10; $i + +) {1}


This code is basically similar to the above code, but the contents of the curly braces change slightly, first look at the results of the operation:

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/6F/96/wKioL1Wij8myO3DwAACR8FVcbe8349.jpg "/>

This code is the same as just the same, but the output of the result is different, this code is output 10 a 1. OK, someone must want to ask, not just a cycle, need two times? Paste two pieces of code? In fact, I think this is necessary, because for us to do operations, programming is a threshold, I believe that most of the friends do operation and maintenance of the code is not very familiar, may just be some simple script (the major gods, Daniel except ah ~ everyone greatly mo laughter ~), so I personally think, from the simplest comparison, more can see the difference.

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/6F/99/wKiom1WijfPTPn9KAAAbu-HEc3E601.jpg "/>650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/6F/96/wKioL1Wij8nAouNZAAAeD54LjYA991.jpg "/>

OK, the above two scripts, although only a single row, but the output of the difference is more than one place. The above two pieces of code illustrate the following points:

    1. You can output multiple times through a loop. Crap I also feel that ... )

    2. Through loops, the output can be the same, or it can be different.

    3. The inner body of the loop can be combined with a loop variable, or it can be independent of the loop variable.

What is the use of these points? 1th, solve our main problem, batch create virtual machine; Remember the code of the previous article? Plus the loop, let's take a look at the code:


for ($i =1; $i-le10; $i + +) {#输入部分: $vhdpath _new= read-host ("Please enter the template path" including the file name and suffix "") $NewVMGeneration = Read-host (" Please enter the version number of the new virtual machine ") $NewVMMemoryStartupBytes = Read-host (" Enter startup Memory ") $name = Read-host (" Enter the virtual machine name ") $NewVMSwitchName = Read-host ("Enter the name of the network to which the virtual machine needs to connect") $NewVMvCPU = Read-host ("Enter virtual CPU number of virtual machine") #创建部分: New-vm-vhdpath $vhdpath _new-generation $ Newvmgeneration-memorystartupbytes $NewVMMemoryStartupBytes-name $name-switchname $NewVMSwitchNameSet-vmprocessor -vmname $name-count $NewVMvCPU}


In fact, this code is the second section of the code above the loop body "1" replaced with the previous code, the final function is to put the previous script run 10 times, manually entered 10 times after you can create 10 virtual machines!

After the operation, we find that the problem is coming! Every time to enter a lot of duplicate content, in fact, we need a custom configuration, such as only the virtual machine name and hard disk name, the other configuration can remain the same, what should be done? So easy! Put the input part, in addition to the name of the virtual machine, the other parts to take out is AH!


#输入部分: $vhdpath _new= read-host ("Enter the template path" including the file name and suffix ") $NewVMGeneration = Read-host (" Enter the version number of the new virtual machine ") $ newvmmemorystartupbytes= read-host ("Enter startup Memory") $NewVMSwitchName = Read-host ("Please enter the name of the network to which the virtual machine needs to connect") $NewVMvCPU = Read-host ( "Please enter virtual CPU number of virtual machine") for ($i =1; $i-le10; $i + +) {#输入部分: $name = Read-host ("Enter virtual machine name") #创建部分: New-vm-vhdpath $vhdpath _new- Generation $NewVMGeneration-memorystartupbytes $NewVMMemoryStartupBytes-name $name-switchname $ Newvmswitchnameset-vmprocessor-vmname $name-count $NewVMvCPU}


Now that we're done, our script has been able to create virtual machines in bulk! Here, we have all the script core content is roughly completed, but still the user experience is not very good, the next chapter, we continue to explore, so that our script can adapt to different environments ~


digression: Today in Nanjing, we 51cto in Nanjing, our small partners play a very high, together participate in the 51cto 10 anniversary celebration, Technical Achievement Dream, Wish 51cto happy 10 Anniversary! Finally, attached to the pictures we played together, Haha, look for, where am I?

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/6F/99/wKiom1WijfSDniDgAAJ5DMr89KU245.jpg "/>

650) this.width=650; "Width=" height= "src=" http://s3.51cto.com/wyfs02/M01/6F/76/ Wkiol1wdzr3sowszaaaubhkpm-8754.jpg "alt=" Wkiol1wdzr3sowszaaaubhkpm-8754.jpg "/>

This article is from the "Pinkneyleo" blog, make sure to keep this source http://pinkneyleo.blog.51cto.com/6311072/1673593

PowerShell Create virtual machines (second, batch creation)

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: info-contact@alibabacloud.com 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.