Using PowerCLI to build multiple VMs

简介:

http://notesofascripter.com/2016/03/28/using-powercli-build-multiple-vms/


My first script that I ever wrote was a script to build VMs. I was the newest member on the team, and i was giving the task of building 50 VMs for a new project that was getting started. It was a very daunting task, due to the completion date to have these 50 VMs to be completed. So one of the other members of the team told me about PowerCLI, but didn’t have any knowledge about it. So I started to read about it, and got it installed. I started to run very simple commands, such as Get-Template just to see what it does. The more I got used to running these commands, I started to put together the script. So I started with getting all of the information that I needed to build the VM, the VM specs. So I put all of this information into a CSV file.

Using PowerCLI to build multiple VMs

So now that I have all of the VM specs into a CSV file, I needed to import them into the script for it to use.


$vms  =  Import-CSV  "C:\Scripts\NewVMs.csv"


So now we have the bases of a the new script. Now I need to assign some variables. So as seen above in the CSV file we have header information, Name, Template, Cluster, Datastore,
Customization, vCPU, Memory, Network, Harddrive, Harddrive2, Diskformat, and Location. This step might be able to be avoided now, but had problems with it before, so that is why I chose
to set it up this way.


#Assign Variables
$VMName  $vm .Name
$Template  Get-Template  -Name  $vm .Template
$Cluster  $vm .Cluster
$Datastore  Get-Datastore  -Name  $vm .Datastore
$Custom  Get-OSCustomizationSpec  -Name  $vm .Customization
$vCPU  $vm .vCPU
$Memory  $vm .Memory
$Network  $vm .Network
$Location  $vm .Location


Now that we have the variables going into the script we need to do the actually work. So the command that enables the VM to be created is New-VM. With that we can pump the most of the
variables into the command New-VM, and a new VM will be generated.


New-VM -Name VMNameTemplateTemplate -ResourcePool (Get-Cluster Cluster|GetResourcePool)LocationLocation -StorageFormat Thin -Datastore DatastoreOSCustomizationSpecCustom


That only uses a portion of the variables that we collected, so lets add some more of them to get a more complete VM. So we still have the number of vCPUs, amount of memory and the Network adapter setting.


NewVM=GetVMNameVMName
NewVM|SetVMMemoryGBMemory -NumCpu vCPUConfirm:false
NewVM|GetNetworkAdapter|SetNetworkAdapterNetworkNameNetwork -Confirm:$false


At this point we have the parts for a working script to build VMs. Now just to put it all together and we’ll have a script that can build any number of VMs based on the information placed in to the CSV file.


vms=ImportCSV"C:\Scripts\NewVMs.csv"foreach(vm in $vms){
#Assign Variables
Template=GetTemplateNamevm.Template
Cluster=vm.Cluster
Datastore=GetDatastoreNamevm.Datastore
Custom=GetOSCustomizationSpecNamevm.Customization
vCPU=vm.vCPU
Memory=vm.Memory
Network=vm.Network
Location=vm.Location
VMName=vm.Name
 
#Where the VM gets built
New-VM -Name VMNameTemplateTemplate -ResourcePool (Get-Cluster Cluster|GetResourcePool)LocationLocation -StorageFormat Thin -Datastore DatastoreOSCustomizationSpecCustom
Start-Sleep -Seconds 10
 
#Where the vCPU, memory, and network gets set
NewVM=GetVMNameVMName
NewVM|SetVMMemoryGBMemory -NumCpu vCPUConfirm:false
NewVM|GetNetworkAdapter|SetNetworkAdapterNetworkNameNetwork -Confirm:$false
}


In the next post we will go over how to assign the IPs, subnet mask, gateway, and update the VMware tools.

– Stuart

Find this and all of my scripts at https://github.com/NotesofaScripter/PowerCLI

本文转自学海无涯博客51CTO博客,原文链接http://blog.51cto.com/549687/1865514如需转载请自行联系原作者


520feng2007

目录
打赏
0
0
0
0
91
分享
相关文章
【Node系列】REPL详解
Node.js REPL(Read-Eval-Print Loop)是一个交互式环境,允许用户在命令行中直接输入JavaScript代码并立即看到结果。REPL是Node.js的一个重要组成部分,它提供了一个方便的方式来测试代码片段、快速尝试新功能或进行调试。
240 1
阿里云短信服务平台价格表
阿里云短信服务平台价格表,阿里云短信服务价格表,阿里云短信0.032元一条,阿里云短信价格?阿里云短信怎么收费?阿里云短信多少钱一条,阿里云短信价格0.032元一条
498 0
第十四届蓝桥杯集训——练习解题阶段(无序阶段)-Java全排列公式
第十四届蓝桥杯集训——练习解题阶段(无序阶段)-Java全排列公式
96 0
带你读《2022技术人的百宝黑皮书》——CoHOZ: Contrastive Multimodal Prompt Tuning for Hierarchical Open-set Zero-shot Recognition(11)
带你读《2022技术人的百宝黑皮书》——CoHOZ: Contrastive Multimodal Prompt Tuning for Hierarchical Open-set Zero-shot Recognition(11)
103 0
常见数据结构-散列表(下)散列表和链表的组合
常见数据结构-散列表(下)散列表和链表的组合
195 0
Flink 助力美团数仓增量生产
本文由美团研究员、实时计算负责人鞠大升分享,主要介绍 Flink 助力美团数仓增量生产的应用实践。内容包括:1、数仓增量生产;2、流式数据集成;3、流式数据处理;4、流式 OLAP 应用;5、未来规划。
Flink 助力美团数仓增量生产
AI助理

你好,我是AI助理

可以解答问题、推荐解决方案等

登录插画

登录以查看您的控制台资源

管理云资源
状态一览
快捷访问