You are on page 1of 8

#Get vApp names and port groups

$CSVfile = "c:\admin\scripts\ehc_vapps.csv"

# Set PowerCLI Options


Set-PowerCLIConfiguration -InvalidCertificateAction Ignore -Confirm:$false | Out-Null

$EHC_vApps = Import-Csv -Path $CSVfile


ForEach ($EHC_vApp in $EHC_vApps)
{
#Creates new vApp
New-VApp -Name $EHC_vApp.name -Datastore $EHC_vApp.datastore -Location $EHC_vApp.cluster
-VApp $EHC_vApp.template

#Get list of vApp VMs to set network card


$vApp_vms = Get-VApp $EHC_vApp.name | Get-VM
ForEach ($vApp_vm in $vApp_vms)
{
Get-VM -Location $EHC_vApp.name $vApp_vm | Get-NetworkAdapter | Set-NetworkAdapter
-Portgroup $EHC_vApp.portgroup -Confirm:$false
}

#Move vApp to StudentPod folder


Move-VApp -Destination $EHC_vApp.folder -VApp $EHC_vApp.name
}

-------------------------------

Import-vApp -Source "c:\vApps\WebServer\WebServer.ovf" -VMHost $vmHost -Location $myCluster


-Name "MyWebServerProduction1"

$vmHost | Import-vApp -Source "c:\vApps\WebServer\WebServer.ovf" -Datastore $myDatastore

--------------------------------------

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

$vmsnapin = Get-PSSnapin VMware.VimAutomation.Core -ErrorAction SilentlyContinue


$Error.Clear()
if ($vmsnapin -eq $null)
{
Add-PSSnapin VMware.VimAutomation.Core
if ($error.Count -eq 0)
{
write-host "PowerCLI VimAutomation.Core Snap-in was successfully enabled."
-ForegroundColor Green
}
else
{
write-host "ERROR: Could not enable PowerCLI VimAutomation.Core Snap-in, exiting
script" -ForegroundColor Red
Exit
}
}
else
{

Write-Host "PowerCLI VimAutomation.Core Snap-in is already enabled" -ForegroundColor


Green
}

# 32-bit PowerCLI is required to run *-OSCustomizationSpec cmdlets


if ($env:Processor_Architecture -eq "x86") {

#Connect to vCenter
Connect-VIServer -Server $vcenter_srv

$vms2deploy = Import-Csv -Path $csvfile

#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){

Write-Host "No IP configuration in .csv file, moving on"


-ForegroundColor Yellow
write-Host "Deploying VM $($vm.name) to datastore cluster $($vm.datastore-cluster)"
new-vm -name $vm.name -template $(get-template -name
$vm.template) -vmhost $vmhost -oscustomizationspec $(get-oscustomizationspec -name $vm.oscust)
-datastore $(get-datastorecluster -name $vm.datastore-cluster) -location $(get-folder -name $vm.folder) |
Out-Null
}
else {

#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
}

#this is where we try to track deployment progress


$loop_control = 0
write-host "Starting VM $($vm.name)"
start-vm -vm $vm.name -confirm:$false | Out-Null

write-host "Waiting for first boot of $($vm.name)" -ForegroundColor


Yellow

do {
$toolsStatus = (Get-VM -name $vm.name).extensiondata.Guest.ToolsStatus
Start-Sleep 3
$loop_control++
} until ( ($toolsStatus -match toolsOk) -or ($loop_control -gt $timeout) )

write-host "Waiting for customization spec to apply for $($vm.name) (a


reboot)" -ForegroundColor Green
do {
$toolsStatus = (Get-VM -name $vm.name).extensiondata.Guest.ToolsStatus
Start-Sleep 3
$loop_control++
} until ( ($toolsStatus -match toolsNotRunning) -or ($loop_control -gt
$timeout) )

Write-Host "OS customization in progress for $($vm.name)"


-ForegroundColor red
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

#clean-up the cloned OS Customization spec


Remove-OSCustomizationSpec -CustomizationSpec $cloned_oscust

-Confirm:$false | Out-Null

if ($loop_control -gt $timeout){


Write-Host "Deployment of $($vm.name) took more than $
($timeout/20) minutes, check if everything OK" -ForegroundColor red
}
else {
Write-Host "$($vm.name) successfully deployed, moving on"
-ForegroundColor Green
}
}
else {
Write-Host "$($vm.name) already exists, moving on" -ForegroundColor
Red
}
}
else {
Write-Host "Check input file $csvfile for line with empty vm, template or OS
customization spec value" -ForegroundColor Red
}
}

Write-Host "All vms deployed, exiting" -ForegroundColor Green


#disconnect vCenter
Disconnect-VIServer -Confirm:$false
}
else {
Write-Host "This script should be run from 32-bit version of PowerCLI only, Open 32-bit PowerCLI

window and start again" -ForegroundColor Red

PS C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI>


Import-VApp -Source "C:\OVF\Test Temp\Test Temp.ovf" -vmhost esxi2.msat.local -datastore scsi-1

You might also like