PowerCLI从模版批量新建虚拟机

有一个需求,新建200个虚拟机,这要是一个一个去建那我可就疯了。想起之前听vmware原厂工程师说过的这个powercli,就着手研究了一下。

将以下文件存储为 *.ps1

#在使用时应使用VMware PowerCLI (32-Bit)
$vc='86.12.92.4'    #vc地址
Connect-VIServer -Server $vc -username "administrator@vsphere.local" -Password "123"    #vc用户名&密码
$vmhost="86.12.87.66"   #指定主机
$namestart="test"    #虚拟机test名称
$template="CentOS_7.7_X64_scanned"  #使用模板
$datastore="vsanDatastore_Desktop"  #使用存储
$custsysprep = Get-OSCustomizationSpec linux    #自定义规范,需要现在vc中配置,vsphere6.5 策略和配置文件>自定义规范管理器>创建新规范
$network="86.12.101.*"  #指定使用的端口组,写端口组名称
#$ipstart="86.12.101."  #可以指定ip的开始地址,不过我不太会用
$endipscope=1..1    #可以指定虚拟机的数量,例如:1..2就是建两台虚拟机

foreach($endip in $endipscope)
{
#$ip=$ipstart+$endip
$name=$namestart+$endip #虚拟机名
#$custsysprep|Set-OScustomizationSpec -NamingScheme fixed -NamingPrefix $name
#$custsysprep|Get-OSCustomizationNicMapping|Set-OSCustomizationNicMapping -IpMode UseStaticIP -IpAddress $ip -SubnetMask 255.255.255.0 -Dns 86.12.24.2 -DefaultGateway 86.12.101.254
New-VM -Name $name -VMHost $vmhost -Portgroup $network -Datastore $datastore -Template $template -OSCustomizationSpec $custsysprep
}
#之前在网络选择是使用的是-NetworkName选项,这个选项已经不再被支持。会触发警告: Specifying a distributed port group name as network name is no longer supported. Use the -Portgroup parameter.

VMwarePowerCLI65.zip


[1]VMware PowerCLI5.1在线手册 https://pubs.vmware.com/vsphere-51/index.jsp?topic=%2Fcom.vmware.powercli.cmdletref.doc%2FSet-OSCustomizationSpec.html

[2]安装PowerCLI https://docs.vmware.com/cn/VMware-vSphere/6.5/com.vmware.vsphere.install.doc/GUID-F02D0C2D-B226-4908-9E5C-2E783D41FE2D.html

[3]PowerCLI各版本下载 https://code.vmware.com/web/tool/12.0.0/vmware-powercli

[4] vmware-powercli-115-user-guide.pdf

posted @ 2020-06-13 21:00  吴诺克  阅读(788)  评论(0编辑  收藏  举报
Copyright © 豆子林克 DOUZI.LINK  版权所有. 除特别申明外,本站原创内容版权遵循 CC-BY-NC-SA 协议规定