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 $VMName -Template $Template -ResourcePool (Get-Cluster $Cluster | Get-ResourcePool) -Location $Location -StorageFormat Thin -Datastore $Datastore -OSCustomizationSpec $Custom


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 = Get-VM -Name $VMName
$NewVM | Set-VM -MemoryGB $Memory -NumCpu $vCPU -Confirm:$false
$NewVM | Get-NetworkAdapter | Set-NetworkAdapter -NetworkName $Network -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 = Import-CSV "C:\Scripts\NewVMs.csv"
 
foreach ($vm in $vms){
#Assign Variables
$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
$VMName = $vm.Name
 
#Where the VM gets built
New-VM -Name $VMName -Template $Template -ResourcePool (Get-Cluster $Cluster | Get-ResourcePool) -Location $Location -StorageFormat Thin -Datastore $Datastore -OSCustomizationSpec $Custom
Start-Sleep -Seconds 10
 
#Where the vCPU, memory, and network gets set
$NewVM = Get-VM -Name $VMName
$NewVM | Set-VM -MemoryGB $Memory -NumCpu $vCPU -Confirm:$false
$NewVM | Get-NetworkAdapter | Set-NetworkAdapter -NetworkName $Network -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

相关文章
|
3天前
|
Ubuntu Linux 内存技术
Linux(14)Debain Make image and module configuration instructions
Linux(14)Debain Make image and module configuration instructions
5 0
|
26天前
|
Linux Docker 容器
problem with installed package podman-1.4.2-5.module_el8.1.0
problem with installed package podman-1.4.2-5.module_el8.1.0
11 0
|
C++ iOS开发
报错解决:Could not build wheels for soxr, which is required to install pyproject.toml-based projects(可用)
链接如下:【金山文档】 1-Microsoft Visual C++ Build Tools。找了好久,才找到正确的解决方案,网上一大堆升级setuptools的方法只对少数人管用。注意,虽然我的这个报错内容有点长,但是我感觉和其它的。如果网页提示登录,可以不用登录,直接下载即可。然后打开镜像ios文件(双击即可)错误是一样的解决方案。文件,打开后安装即可。
2354 1
报错解决:Could not build wheels for soxr, which is required to install pyproject.toml-based projects(可用)
|
Kubernetes 安全 应用服务中间件
Kubernetes CKS【24】---System Hardening - Kernel Hardening Tools(seccomp)
Kubernetes CKS【24】---System Hardening - Kernel Hardening Tools(seccomp)
Kubernetes CKS【24】---System Hardening - Kernel Hardening Tools(seccomp)
|
Linux
登录linux黑屏the configuration defaults for gnome power manager have not been installed correctly无法进入
登录linux黑屏the configuration defaults for gnome power manager have not been installed correctly无法进入
332 0
登录linux黑屏the configuration defaults for gnome power manager have not been installed correctly无法进入