PowerCLI to create VMs from template (Newest)

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

时间: 2024-10-31 07:04:01

PowerCLI to create VMs from template (Newest)的相关文章

PowerCLI to create multiple VMs from template

CSV format PowerCLI script #PowerCLI to create multiple VMs from template Add-PSSnapin vmWARE.VimAutomation.Core$vc="devvc55"connect-viserver $vc #Import VM information $VMs = Import-CSV "C:\NewVMs.csv" foreach ($vm in $vms){#Assign Va

XenServer 6.5实战系列之十:Create VMs from a VM Template

1.  Create VMs 1.1 右键点击对应的模板"Test-01",点击"New VM wizard" 1.2 选择对应的模板"Test-01",Next 1.3 为新虚机命名, Next 1.4 保持默认,通过模板创建虚拟机不需要插入DVD光盘,Next 1.5 选择该虚拟机默认启动的服务器,Next 1.6 根据需求调整vCPU 数量,内存大小,Next 1.7 选择相应的存储,Next 1.8 选择VM 使用的网络,Next 1.9

initramfs-tools ... update-initramfs: Generating /boot/initrd.img-3.14-kali1-amd64 mktemp: failed to create directory via template `/var/tmp/mki

Processing triggers for initramfs-tools ...update-initramfs: Generating /boot/initrd.img-3.14-kali1-amd64mktemp: failed to create directory via template `/var/tmp/mkinitramfs_XXXXXX': No such file or directoryupdate-initramfs: failed for /boot/initrd

virsh 基于xml create VMs虚机

查看虚机one-8 virsh 基于xml create VMs虚机

Create Oracle 12c Template Database In Silent

1.静默文件 [[email protected] ~]$ cat create_template_db.rsp  [GENERAL] RESPONSEFILE_VERSION = "12.1.0" OPERATION_TYPE = "createTemplateFromDB" [createTemplateFromDB] SOURCEDB = "rhndb" SYSDBAUSERNAME = "system" SYSDBAP

[MODx] 1. Add Html5 template into the MODx

1. Connet MODx by SSH: Go to the MODx cloud; Find you current user and right click selet Edit Cloud; Find your SSH connect information: 2. Upload an HTML5 template You can grap one from www.html5up.com Upload the files to the root dir (for learning p

[Cycle.js] Hyperscript as our alternative to template languages

Usually we use template languages like Handlebars, JSX, and Jade to create. One simple way we can create our own template language is to write a function that returns these objects for us. This lessons shows how we can use these functions as a DSL to

[PowerShell Utils] Create a list of virtual machines based on configuration read from a CSV file in Hyper-V

Hello everyone, this is the third post of the series. .   Background =============== In my solution, I have 15 Windows hosts. I need to configure them from the OS installation to configure fail over cluster and then create and run VMs on them. Withou

XML Publiser For Excel Template

1.XML Publisher定义数据 2.XML Publisher定义模板 模板类型选择Microsoft Excel,默认输出类型选择Excel,上传.xls模板 3.定义并发程序 4.定义请求 请求输出格式选择XML 5.制作Excel模板(97-2003格式保存) http://docs.oracle.com/cd/E21764_01/bi.1111/e13881/T527073T571887.htm Creating Excel Templates This chapter cove