Skip to content

Instantly share code, notes, and snippets.

@LVLAaron
Created January 24, 2014 20:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save LVLAaron/8605549 to your computer and use it in GitHub Desktop.
Save LVLAaron/8605549 to your computer and use it in GitHub Desktop.
Deploy multiple vmware guests based on limited input data provided by web request form.
Clear-Host
# Add PowerCLI bits
Add-PSSnapin -Name "VMware.VimAutomation.Core" -ErrorAction SilentlyContinue
# Connect to Virtual Infrastructure
Connect-VIserver LouPrMgt011.zcloud.com
$vmlist = Import-CSV "C:\Users\aaron.anderson\Downloads\ServerBuild.csv"
# Syntax and sample for CSV File:
# ipaddress,vmname,template
Clear-Host
foreach ($item in $vmlist) {
# Map variables
$ipaddr = $item.ipaddress
$vmname = $item.vmname
$template = $item.template
$description = $item.description
# Determine cluster, spec file, and storage pool.
if ($vmname -like "LouPr*b") {$vmcluster = "Production";$customspecfile = "scripted_prod_on_zcloud";$VMFSVol = "XIO_Production_b"}
if ($vmname -like "LouPr*") {$vmcluster = "Production";$customspecfile = "scripted_prod_on_zcloud";$VMFSVol = "Production"}
if ($vmname -like "LouQa*") {$vmcluster = "Development";$customspecfile = "scripted_qa_on_zcloud";$VMFSVol = "XIO_DevQa"}
if ($vmname -like "LouDv*") {$vmcluster = "Development";$customspecfile = "scripted_dev_on_zcloud";$VMFSVol = "XIO_DevQa"}
# Determine DNS servers
if ($vmname -like "Lou*") {$pdns = "192.168.0.33";$sdns = "192.168.0.43"}
if ($vmname -like "Vgs*") {$pdns = "10.20.0.33";$sdns = "10.20.0.43"}
# Determine gateway and port-group
if ($ipaddr -like "192.168.0.*") {$gateway = "192.168.0.1";$dvpg = "VM Network"}
if ($ipaddr -like "192.168.2.*") {$gateway = "192.168.2.1";$dvpg = "PCI Transit"}
if ($ipaddr -like "192.168.3.*") {$gateway = "192.168.3.1";$dvpg = "PCI Storage"}
if ($ipaddr -like "192.168.4.*") {$gateway = "192.168.4.1";$dvpg = "Qa LAN"}
if ($ipaddr -like "192.168.5.*") {$gateway = "192.168.5.1";$dvpg = "Qa PCI Storage"}
if ($ipaddr -like "192.168.6.*") {$gateway = "192.168.6.1";$dvpg = "Qa PCI Transit"}
if ($ipaddr -like "192.168.7.*") {$gateway = "192.168.7.1";$dvpg = "Testing LAN"}
if ($ipaddr -like "192.168.8.*") {$gateway = "192.168.8.1";$dvpg = "Caringo Prod"}
if ($ipaddr -like "192.168.9.*") {$gateway = "192.168.9.1";$dvpg = "Caringo Dev"}
if ($ipaddr -like "192.168.10.*") {$gateway = "192.168.10.1";$dvpg = "Dev LAN"}
if ($ipaddr -like "192.168.11.*") {$gateway = "192.168.11.1";$dvpg = "Dev PCI Storage"}
if ($ipaddr -like "192.168.12.*") {$gateway = "192.168.12.1";$dvpg = "Dev PCI Transit"}
if ($ipaddr -like "10.10.230.*") {$gateway = "10.10.230.1";$dvpg = "Management"}
# Find host with most available memory
$BestHost = Get-Cluster $vmcluster | Get-VMHost | Sort MemoryUsageGB | Select -Last 1
# Deal with customization spec file
Get-OSCustomizationSpec $customspecfile | Get-OSCustomizationNicMapping | Set-OSCustomizationNicMapping -IpMode UseStaticIp -IpAddress $ipaddr -SubnetMask "255.255.255.0" -DefaultGateway $gateway -Dns $pdns,$sdns
#Deploy the VM based on the template with the adjusted Customization Specification
New-VM -Name $vmname -Template $template -Datastore $VMFSVol -VMHost $BestHost -OSCustomizationSpec $customspecfile -Description $description
#Set the Port Group Network Name (Match PortGroup names with the VLAN name)
Get-VM -Name $vmname | Get-NetworkAdapter | Set-NetworkAdapter -NetworkName $dvpg -Confirm:$false
Start-VM $vmname
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment