VMware PowerCLI 是基于 PowerShell 构建的命令行和脚本工具,提供 7000 多个 cmdlet,用于管理和自动化 VMware vSphere、VMware Cloud Director、vRealize Operations Manager、vSAN、VMware NSX-T、VMware Cloud Services、VMware Cloud on AWS、VMware HCX、VMware Site Recovery Manager、vSphere Replication、VMware Cloud Foundation 和 VMware Horizon 环境。
Download address:https://developer.vmware.com/web/tool/vmware-powercli/
安装PowerCli
在线安装
用管理员打开powershell
Install-Module -Name VMware.PowerCLI -Scope CurrentUser
离线安装
官网下载最新版本离线包
解压安装包到这个目录:C:WindowsSystem32WindowsPowerShellv1.0Modules
用管理员打开powershell,执行命令导入安装包,Import-Module VMware.PowerCLI
关闭CEIP( Customer Experience Improvement Program 客户体验改进计划)
Set-PowerCLIConfiguration -Scope User -ParticipateInCEIP $false
忽略证书验证
Set-PowerCLIConfiguration -InvalidCertificateAction Ignore
连接vCenter验证安装成功
Connect-VIServer -Protocol https -User '账号' -Password '密码' -Server 'vCenter的IP地址'
成功连接vC后可输入get-vm和get-vmhost查看虚拟机和ESXi主机
批量创建虚拟机
创建csv表格,补充虚拟机信息。
Name Template PhysicalHost Datastore Network Cpu Memory DISK IPV4 NetMask IPV4GW IPV6 IPV6GW Usage Esxidir
创建脚本
$vms = Import-CSV VM.csv #表格名称,表格和脚本在同一目录
$templatenic = 'eth0'#虚拟机模板网卡名称
$templateuser = 'root'#虚拟机用户
$templatepass = 'password'#虚拟机模板密码
#连接vCenter
Connect-VIServer -Protocol https -User 'administrator@vsphere.local' -Password 'VMware' -Server 192.168.1.245
foreach ($vm in $vms){
$VMName = $vm.Name
$Network = $vm.Network
$Cpu = $vm.Cpu
$Memory = $vm.Memory
$Notes = $vm.Usage
$Disk = $vm.DISK
$Template = Get-Template $vm.Template
$VMHost = Get-VMHost $vm.PhysicalHost
$Datastore = Get-Datastore $vm.Datastore
#1、新建虚拟机文件夹
New-Folder -Name $vm.Esxidir -Location VM
Start-Sleep -s 5
#2、从模板新建虚拟机,并设置名称、端口组和存放位置
New-VM -Name $VMName -Template $Template -VMHost $VMHost -NetworkName $Network -Datastore $Datastore -RunAsync -Location $vm.Esxidir
Start-Sleep -s 15
#3、配置虚拟机cpu,内存,和备注
Get-VM -Name $VMName|Set-VM -NumCPU $Cpu -MemoryGB $Memory -Notes $Notes -Confirm:$false
Start-Sleep -s 5
#4、配置虚拟机硬盘
if ($Disk -gt 0)
{ Get-VM -Name $VMName | New-HardDisk -CapacityGB $Disk -Persistence persistent }
Start-Sleep -s 5
#5、打开虚拟机电源
#Get-VM $VMName| Start-VM
#Start-Sleep -s 15
#6、执行脚本(修改虚拟机ip和主机名)
$vmcfg='sed -i s/IPADDR=null/IPADDR='+$vm.IPV4+'/ /etc/sysconfig/network-scripts/ifcfg-'+$templatenic+' && sed -i s/NETMASK=null/NETMASK='+$vm.NetMask+'/ /etc/sysconfig/network-scripts/ifcfg-'+$templatenic+' && sed -i s/GATEWAY=null/GATEWAY='+$vm.IPV4GW+'/ /etc/sysconfig/network-scripts/ifcfg-'+$templatenic+' && sed -i s/IPV6ADDR=null/IPV6ADDR='+$vm.IPV6+'/ /etc/sysconfig/network-scripts/ifcfg-'+$templatenic+' && sed -i s/IPV6_DEFAULTGW=null/IPV6_DEFAULTGW='+$vm.IPV6GW+'/ /etc/sysconfig/network-scripts/ifcfg-'+$templatenic+' && hostnamectl set-hostname '+$VMName+' && systemctl restart network'
Get-VM $VMName | Invoke-VMScript -ScriptText $vmcfg -GuestUser $templateuser -GuestPassword $templatepass
}
将脚本复制到PowerShell ISE中执行或保存为ps1文件后执行,注意VM.csv文件路径
参考链接:
https://developer.vmware.com/docs/15315/GUID-D9597FE4-90EE-4CE4-A520-1A8D65854B26.html
There are currently no comments available