开发者社区> 科技探索者> 正文
阿里云
为了无法计算的价值
打开APP
阿里云APP内打开

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

版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。

相关文章
登录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无法进入
0 0
Yes Small Companies Can – and Should – Build Secure Software
"For large software companies or major corporations such as banks or health care firms with lar...
858 0
op5 Appliance Multiple Remote Command Execution Vulnerabilities
 http://www.securityfocus.com/bid/51212/exploit
396 0
GuestStealer allows for the stealing of VMware guests from vulnerable hosts based on the Directory Traversal Vulnerability
GuestStealer allows for the stealing of VMware guests from vulnerable hosts based on the Directo...
525 0
文章
问答
文章排行榜
最热
最新
相关电子书
更多
Network-Automation-Isn\'t-Your-Safe-Haven-Protocol-Analysis-And-Vulnerabilities-Of-Autonomic-Network
立即下载
Azure Container Services --The best place to run your workload
立即下载
Taking-Windows-10-Kernel-Exploitation-To-The-Next-Level–Leveraging-Write-What-Where-Vulnerabilities-In-Creators-Update
立即下载