#通过自增IP批量创建虚机

$vc = "vcenter.v.com"
$user = "username"
$password = "pass"

Connect-VIServer -Server $vc -username $user -Password $password

#选择宿主
$vmhost="10.10.20.13"
#选择模板
$template_centos = "CentOS7.6-2"
#选择虚机规范
$custsysprep = "CentOS7.6_Rule"
#设置虚机存储位置
$datastore_103 = "data200.103"
#选择网卡VLAN(分布式虚拟端口组)
$network = "VLAN201"

#设置CPU、内存、虚机描述
$cpu = 4
$memory = 8
$Notes = "开发使用"

$ip_range = 90..191

$IP_Prefix = "10.10.201"
$VMName_Prefix = "Dev"
$NetMask = "255.255.255.0"
$Gateway = "10.10.201.254"

foreach ($n in $ip_range[0..1])
{
$IP = -Join($IP_Prefix,'.',$n)
$vmname = -Join($VMName_Prefix,'_',$IP)
$IP + " " + $vmname
#自定义虚机规范中的IP信息,DNS不需要更改
Get-OSCustomizationSpec $custsysprep | Get-OSCustomizationNicMapping | Set-OSCustomizationNicMapping -IpMode UseStaticIP -IpAddress $IP -SubnetMask $NetMask -DefaultGateway $Gateway
#Get-OSCustomizationSpec $custsysprep | Get-OSCustomizationNicMapping | Set-OSCustomizationNicMapping -IpMode UseStaticIP -IpAddress $IP -SubnetMask $NetMask -DefaultGateway $Gateway -Dns 10.10.10.4,10.10.100.5
#新建虚机,使用模板及规范配置IP,虚机MAC会重新生成
New-VM -Name $vmname -VMHost $vmhost -Portgroup $network -Datastore $datastore_103 -Template $template_centos -OSCustomizationSpec $custsysprep
#设置虚机CPU、内存、描述信息
Get-VM -Name $vmname|Set-VM -NumCPU $cpu -MemoryGB $memory -Notes $Notes -Confirm:$false
#磁盘扩容到500GB
#Get-VM -Name $vmname |Get-HardDisk |Set-HardDisk -CapacityGB 500 -Confirm:$false
#新增磁盘,大小为500GB
#Get-VM -Name $vmname | New-HardDisk -CapacityGB 500 -Persistence persistent
Get-VM -Name $vmname |Start-VM
}

通过CSV批量创建虚机,默认使用模板,创建过程中通过修改规范中的IP信息,从而设置虚机IP,MAC地址不会重复,模板默认只有一块磁盘,如果扩容,则新增第二块磁盘

$vc = "vc.vm.com"
$user = "luser@vm.com"
$password = "mypass"

Connect-VIServer -Server $vc -username $user -Password $password

$vm_file = "d:\vms.csv"
$vms = import-csv $vm_file
$vms.Count
foreach ($vm in $vms)
{
#选择宿主、模板、规范、虚机存储位置、VLAN、CPU、内存、描述信息、第二磁盘大小、IP、子网掩码、网关、虚机名称
$vmhost = $vm.Host
$template = $vm.Template
$custsysprep = "CentOS7.6_Rule"

$datastore = $vm.Datastore
$network = $vm.Vlan
$cpu = $vm.CPU
$memory = $vm.Memory
$Notes = $vm.Description
#默认使用模板机磁盘,如果需要大磁盘,则新增一块磁盘2
[int]$Disk2 = $vm.Disk2

$IP = $vm.IP
$NetMask = "255.255.255.0"
$Gateway = ($IP.Split('.')[0..2] -join ".") + ".254"

#虚机名称拼接IP信息
$vmname = -join($vm.Name,'_',$IP)

#将IP信息写入规范,DNS不需要更改
Get-OSCustomizationSpec $custsysprep | Get-OSCustomizationNicMapping | Set-OSCustomizationNicMapping -IpMode UseStaticIP -IpAddress $IP -SubnetMask $NetMask -DefaultGateway $Gateway


#新建虚机,使用模板及规范配置IP,虚机MAC会重新生成
New-VM -Name $vmname -VMHost $vmhost -Portgroup $network -Datastore $datastore -Template $template -OSCustomizationSpec $custsysprep
#设置虚机CPU、内存、描述信息
Get-VM -Name $vmname|Set-VM -NumCPU $cpu -MemoryGB $memory -Notes $Notes -Confirm:$false

#新增磁盘2
if ($Disk2 -gt 0)
{ Get-VM -Name $vmname | New-HardDisk -CapacityGB $Disk2 -Persistence persistent }
#启动虚机
Get-VM -Name $vmname |Start-VM

}

 csv信息如下:

PowerCLI批量创建VMWare虚机_ipad

根据CSV批量创建虚机,windows和linux都可用

#该脚本用来根据csv定义的信息批量创建虚机
$vc = "vcs.yx.com"
$user = "username@yx.com"
$password = "password"

Connect-VIServer -Server $vc -username $user -Password $password
#Disconnect-VIServer -Server $vc
$vm_file = "D:\Work\Scripts\VMWare\开发需求统计\vms_win.csv"
$vms = import-csv $vm_file
$vms.Count
# $vms

foreach ($vm in $vms)
{
#选择宿主、模板、规范、虚机存储位置、VLAN、CPU、内存、描述信息、IP、子网掩码、网关、虚机名称
$vmhost = $vm.Host
$template = $vm.Template
$custsysprep = $vm.Rule

$datastore = $vm.Datastore
$network = $vm.Vlan
$cpu = $vm.CPU
$memory = $vm.Memory
$Notes = $vm.Description

$computername = $vm.ComputerName
$IP = $vm.IP
$NetMask = "255.255.255.0"
#自动匹配网关
$Gateway = ($IP.Split('.')[0..2] -join ".") + ".254"
[array]$DNSs="10.10.1.4","10.10.10.5"

#虚机名称拼接IP信息
$vmname = -join($vm.Name,'_',$IP)

#如果CSV中提供了计算机名称,则进行设置
if ($vm.ComputerName.Length -gt 0)
{ Get-OSCustomizationSpec $custsysprep | Set-OSCustomizationSpec -NamingScheme fixed -NamingPrefix $vm.ComputerName }
#将IP信息写入规范,DNS不需要更改
if ( (Get-OSCustomizationSpec $custsysprep).OSType -eq "Linux")
{ Get-OSCustomizationSpec $custsysprep | Get-OSCustomizationNicMapping | Set-OSCustomizationNicMapping -IpMode UseStaticIP -IpAddress $IP -SubnetMask $NetMask -DefaultGateway $Gateway }
else { Get-OSCustomizationSpec $custsysprep | Get-OSCustomizationNicMapping | Set-OSCustomizationNicMapping -IpMode UseStaticIP -IpAddress $IP -SubnetMask $NetMask -DefaultGateway $Gateway -Dns $DNSs }


#新建虚机,使用模板及规范配置IP,虚机MAC会重新生成
New-VM -Name $vmname -VMHost $vmhost -Portgroup $network -Template $template -OSCustomizationSpec $custsysprep -Datastore $datastore
#如果复制模板的时候有报错,则需要先等待拷贝完成再执行后面命令
if ( (Get-OSCustomizationSpec $custsysprep).OSType -eq "Linux")
{ Start-Sleep -Seconds 60 }
else { Start-Sleep -Seconds 240 }
#设置虚机CPU、内存、描述信息
Get-VM -Name $vmname|Set-VM -NumCPU $cpu -MemoryGB $memory -Notes $Notes -Confirm:$false
#启动虚机
Get-VM -Name $vmname |Start-VM

}

csv内容如下:

PowerCLI批量创建VMWare虚机_存储位置_02

​windows_Rule_IP 计算机名称和IP提前随意设置一个,在脚本执行过程中会自行更改



Get-template

Get-VDPortgroup

 #获取虚机IP: (get-vm vmname).guest

PowerCLI批量创建VMWare虚机_centos_03

​#获取虚机名称、IP、状态、CPU、内存

get-vm vmname |select name,@{name="IP";express={($_.Guest.IPAddress|select-string 10.10).Line}},PowerState,NumCpu,memoryGB |ft -AutoSize -Wrap

 #获取磁盘大小:

  get-vm vmname |Get-harddisk  |Measure-Object CapacityGB -Sum