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

相关文章
|
4月前
|
网络协议 前端开发 JavaScript
HTTP协议初认识、速了解
系统讲解HTTP定义、特点(无状态、无连接、灵活等)、版本演进(HTTP/1.0→1.1→2.0对比),以及URI/URL/URN区别和请求流程,适合初学者快速掌握核心概念。
2811 0
HTTP协议初认识、速了解
|
4月前
|
人工智能 安全 测试技术
AI红队测试是什么:转岗大模型评测的最短路径
本文详解AI红队测试——面向大模型与Agent的安全评测新方向。结合欧盟AI法案落地、前沿安全风险(如Prompt注入、越权调用)及企业工程需求,阐明其非传统渗透测试,而是覆盖对抗诱导、边界守卫与可控性验证的系统性质量工程。为测试工程师提供从自动化能力迁移至AI安全评测的清晰进阶路径。
|
4月前
|
缓存 文字识别 安全
【第二部分:大模型应用开发基础】6. Prompt Engineering 与 Context Engineering:生产级 Agent 如何管理上下文
本文从 Agent 开发实践出发,分析 Prompt Engineering 的作用与边界,说明复杂 Agent 为什么不能依赖超长 Prompt,并系统介绍 Context Engineering 对系统指令、用户目标、业务数据、工具结果、历史消息和任务状态的动态选择、组装、裁剪与压缩。文章结合合同审查 Agent 与 Java 示例,进一步讨论 Context Rot、Token 预算、Prompt Cache、可观测性及 Prompt Injection 防护,帮助开发者建立生产级 Agent 的上下文管理思路。
355 1
|
5月前
|
数据采集 供应链 监控
1688评论接口:B2B电商自动化运营与数据风控的核心抓手
1688官方评论接口(alibaba.trade.rate.get)是合规、结构化获取B2B商品评价的唯一标准通道,支持批量、时序化拉取评分、SKU评价、追评等核心字段,赋能货源风控、智能选品、竞品分析、舆情监控与产品迭代,助力电商系统实现数据驱动自动化运营。
454 1
|
5月前
|
SQL 人工智能 数据库
机器友好,人类透明:面向 AI Agent 的企业级知识包装规范 OKF 深度指南
本文深度解析Google新发布的Open Knowledge Format(OKF)规范——一种专为AI Agent设计的轻量级知识包装标准。它以Markdown+YAML为核心,倡导原子化、去中心化的知识组织,通过index.md实现渐进式上下文加载,显著缓解RAG的噪声与Token爆炸问题,并与Karpathy的LLM Wiki形成“理念共鸣、路径互补”的双雄格局。
901 0
机器友好,人类透明:面向 AI Agent 的企业级知识包装规范 OKF 深度指南
|
6月前
|
人工智能 前端开发 物联网
历史科普视频的AI自动化生产工作流:从全手动到半自动的工程演进
本文量化历史科普视频制作瓶颈,对比全手动(Stable Diffusion/GPT-SoVITS/Manim等开源栈)与半自动(花生AI为核心)方案。实测混合工作流将单期耗时从29–49小时压缩至10–15小时,效率提升60%+,兼顾质量、可控性与落地性。
|
6月前
|
人工智能 弹性计算 运维
零代码分钟级落地:阿里云ECS 部署 Hermes与OpenClaw智能体完整实操教程
Hermes Agent与OpenClaw是两款面向自动化办公、智能开发、多场景任务调度的AI智能体工具,具备自主任务拆解、本地文件处理、多技能复用、多Agent协同、定时无人值守等核心能力。传统智能体部署需要配置环境、编写部署脚本、调试依赖、适配模型接口,技术门槛高、耗时久,普通用户难以独立完成。
435 0
|
12月前
|
运维 监控 Devops
Linux 开发进阶:从初级运维到 DevOps 工程师的能力提升路径
在云计算时代,Linux运维正迈向自动化与智能化。本文梳理从初级运维到DevOps工程师的三阶跃迁路径:先掌握Shell脚本与Ansible实现自动化;再学习Docker容器化技术,构建高可用架构;最终落地CI/CD流水线与全链路监控,打通开发与运维壁垒。通过实战案例详解LNMP环境的DevOps改造,助力运维人员系统提升效率,实现能力突破。(238字)
605 0