Professional Documents
Culture Documents
New Rich Text Document
New Rich Text Document
$CSVfile = "c:\admin\scripts\ehc_vapps.csv"
-------------------------------
--------------------------------------
name,template,oscust,cluster,folder,datastore-cluster,ip,mask,gw,dns1,dns2
winxp_01,winxp_template,winxp_custom-spec,VDI-Cluster,WinXP-Desktops,VDIStorageCluster,10.0.0.75,255.255.255.0,10.0.0.1,10.0.0.32,10.0.0.34
winxp_02,winxp_template,winxp_custom-spec,VDI-Cluster,WinXP-Desktops,VDIStorageCluster,10.0.0.76,255.255.255.0,10.0.0.1,10.0.0.32,10.0.0.34
win2k3_01,win2k3_template,win2k3_custom-spec,Server-Cluster,Win-Servers,ServersStorageCluster,10.0.1.77,255.255.255.0,10.0.1.1,10.0.0.32,10.0.0.34
----------------------------------------
<#
.SYNOPSIS
Script automates deployment of multiple vms loaded from pre-defined .csv file
.DESCRIPTION
Script reads the .csv file, deploys vms from template, applies os customization spec then attempts to set
up IP configuration
.PARAMETER
none
.EXAMPLE
trivia
#>
#variables
$ScriptRoot = Split-Path $MyInvocation.MyCommand.Path
$csvfile = "$ScriptRoot\vms2deploy.csv"
$vcenter_srv = 'vcenter.seba.local'
$timeout = 1800
$loop_control = 0
#Connect to vCenter
Connect-VIServer -Server $vcenter_srv
#deploy vms as per information in each line, wait for customization (a reboot) then wait for
vmware tools to change ip settings
foreach ($vm in $vms2deploy) {
#validate input, at least vm name, template name and OS Customization Spec name should be
provided
if (($vm.name -ne "") -and ($vm.template -ne "") -and ($vm.oscust -ne "")){
#check if vm with this name already exists (some funny results are produced
once we deploy vm with duplicate name)
if (!(get-vm $vm.name -erroraction 0)){
$vmhost = get-cluster $vm.cluster | get-vmhost -state connected | GetRandom
#check if we want to attempt IP configuration, if "none" is written in .csv file (because we use
DHCP for example) we deploy immediately, otherwise we insert IP into CustomizationSpec, then deploy
if ($vm.ip -match none){
#clone the "master" OS Customization Spec, then use it to apply vm specific IP configuration
$cloned_oscust = Get-OSCustomizationSpec $vm.oscust | NewOSCustomizationSpec -name "$($vm.oscust)_$($vm.name)"
Set-OSCustomizationNicMapping
-OSCustomizationNicMapping ($cloned_oscust | Get-OscustomizationNicMapping) -Position 1 -IpMode
UseStaticIp -IpAddress $vm.ip -SubnetMask $vm.mask -DefaultGateway $vm.gw -Dns
$vm.dns1,$vm.dns2 | Out-Null
write-Host "Deploying VM $($vm.name) to datastore cluster $($vm.datastore)"
new-vm -name $vm.name -template $(get-template -name
$vm.template) -vmhost $vmhost -oscustomizationspec $cloned_oscust -datastore $(get-datastorecluster
-name $vm.datastore-cluster) -location $(get-folder -name $vm.folder) | Out-Null
}
do {
$toolsStatus = (Get-VM -name $vm.name).extensiondata.Guest.ToolsStatus
Start-Sleep 3
$loop_control++
} until ( ($toolsStatus -match toolsOk) -or ($loop_control -gt $timeout) )
#wait another minute "just in case" feel free to remove this line
Start-Sleep 60
-Confirm:$false | Out-Null