CSV format
Script:
#PowerCLI to create multiple VMs from template
Add-PSSnapin vmWARE.VimAutomation.Core
$vc="vcname"
connect-viserver $vc
#Input the VM template logon Account and passowrd
$GC = $Host.UI.PromptForCredential("Please enter credentials", "Enter Guest credentials for VM", "Administrator", "")
#Import VM information
$VMs = Import-CSV "C:\NewVMs.csv"
foreach ($vm in $vms){
#Assign Variables
$VMName = $vm.Name
$Template = $vm.Template
$Cluster = $vm.Cluster
$Datastore = $vm.Datastore
$vCPU = $vm.vCPU
$Memory = $vm.Memory
$Location = $vm.Location
$ESXi = $vm.ESXi
$Custom = $vm.Customization
$Network = $vm.Network
$HDisk1=$vm.HDisk1
$HDisk2=$vm.HDisk2
$IP=$vm.IP
$Subnetmask=$vm.Subnetmask
$Gateway=$vm.Gateway
$DNS1=$vm.DNS1
$DNS2=$vm.DNS2
#Create VM from template
New-VM -Name $VMName -Template $Template -VMHost $ESXi -Datastore $Datastore -Location $Location -OSCustomizationSpec $Custom
#Change VM‘s vCPU, Memory, NetworkAdapter
$NewVM = Get-VM -Name $VMName
$NewVM | Set-VM -MemoryGB $Memory -NumCpu $vCPU -Confirm:$false
$NewVM | Get-NetworkAdapter | Set-NetworkAdapter -NetworkName $Network -Confirm:$false
$NewVM | New-Harddisk -CapacityGB $HDsize1 -StorageFormat EagerZeroedThick -Confirm:$false
$NewVM | New-Harddisk -CapacityGB $HDsize2 -Confirm:$false
#Power on the VM
$NewVM | Start-VM
sleep -Seconds 300
#Check the VMTools status and upgrade
DO {(Get-VMGuest $VMName).HostName}
while (((Get-VMGuest $VMName).HostName) -Ne "$VMName")
Get-VM $VMName | Update-Tools
$VMTool = Get-VM $VMName | Out-Null
$VMTool | Select -ExpandProperty ExtensionData | Select -ExpandProperty guest
$VMToolStatus = $VMTool.ToolsRunningStatus
Write-host "Checking that VMWare Tools are running on"$VMName -ForegroundColor Yellow
Sleep -Seconds 5
Do {Write-host "Still checking for VMWare Tools on"$VMName -ForegroundColor Yellow; sleep -Seconds 5}
While ($VMToolStatus -eq "guestToolsRunning")
Write-Host "VMWare tools are now running on"$VMName -ForegroundColor Green
#Set VM IP, Subnetmask, Gateway, DNS
$Network = Invoke-VMScript -VM $VMName -ScriptType Powershell -ScriptText "(gwmi Win32_NetworkAdapter -filter ‘netconnectionid is not null‘).netconnectionid" -GuestCredential $GC
$NetworkName = $Network.ScriptOutput
$NetworkName = $NetworkName.Trim()
Write-Host "Setting IP address for $VMname..." -ForegroundColor Yellow
Sleep -Seconds 60
$netsh = "c:\windows\system32\netsh.exe interface ip set address ""$NetworkName"" static $IP $Subnetmask $Gateway"
$netsh2 = "c:\windows\system32\netsh.exe interface ip set dnsservers ""$NetworkName"" static $DNS1"
$netsh3 = "c:\windows\system32\netsh.exe interface ip add dnsservers ""$NetworkName"" $DNS2"
Invoke-VMScript -VM $VMname -GuestCredential $GC -ScriptType bat -ScriptText $netsh
Invoke-VMScript -VM $VMname -GuestCredential $GC -ScriptType bat -ScriptText $netsh2
Invoke-VMScript -VM $VMname -GuestCredential $GC -ScriptType bat -ScriptText $netsh3
Write-Host "Setting IP address completed." -ForegroundColor Green
}
Disconnect-ViServer -Confirm:$false
原文地址:http://blog.51cto.com/549687/2114366