I am trying to run the below Powercli script to create VM's by reading user inputs and some basic data from a CSV file. The data that I read from the CSV file includes 1. VMName, Cluster, Network Information and Storage. The remaining information is coming from user input. I am prompting the user to pick a site, region and template and collect some VM annotation information. I am able to run this without any issues with a single VM. I am trying to run through a loop and spin up multiple VM's by reading the info from the CSV file. Can someone help me storing the user input in an array so that I can use the information to spin up multiple VMs?
I want to store the template names and the VM annotations and prompt the user if they want to spin up multiple VMs. I am trying to Do/Until loop but it's giving me issues.
param(
[parameter(Mandatory = $true)]
[string]$CSVname,
[string]$vcenter
)
$admin = [Environment]::UserName
$vcenter = read-host 'Type the vcenter FQDN'
Connect-VIServer -Server $vcenter
#Getting List of VMs from .csv
$VMs = Import-CSV $CSVname -UseCulture
start-sleep -s 2
# Getting Region, Site & Template Details
$Region = read-host "Choose Region to Deploy VM : `n 1) US `n 2) EMEA `n 3) APAC"
if ($Region -eq "1" -or $Region -eq "US")
{
$Site = read-host "Choose Site to Deploy VM : `n 1) NY `n 2) MP `n 3) SF"
if ($Site -eq "1" -or $Site -eq "NY")
{
$Template = read-host "Choose Template : `n 1) 2008 `n 2) 2012 `n 3) SQL"
if ($Template -eq "1" -or $Template -eq "2008")
{
$TemplateName = "TempNY_W2K8_R2_STD"
}
elseif ($Template -eq "2" -or $Template -eq "2012")
{
$TemplateName = "TempNY_W2K12_R2_DC"
}
elseif ($Template -eq "3" -or $Template -eq "SQL")
{
$TemplateName = "TempNYSQL"
}
else
{
write-host "Choose the right Template"
exit
}
}
elseif ($Site -eq "2" -or $Site -eq "MP")
{
$Template = read-host "Choose Template to Deploy VM : `n 1) 2008 `n 2) 2012 `n 3) SQL"
if ($Template -eq "1" -or $Template -eq "2008")
{
$TemplateName = "TempMP_Windows_2008_STD_R2"
}
elseif ($Template -eq "2" -or $Template -eq "2012")
{
$TemplateName = "TempMP_WIN_2012_R2_DATACENTER"
}
elseif ($Template -eq "3" -or $Template -eq "SQL")
{
$TemplateName = "TempMPSQL"
}
else
{
write-host "Choose the right Template"
exit
}
}
elseif ($Site -eq "3" -or $Site -eq "SF")
{
$Template = read-host "Choose Template : `n 1) 2008 `n 2) 2012 `n 3) SQL"
if ($Template -eq "1" -or $Template -eq "2008")
{
$TemplateName = "TempSF_Windows_2008_STD_R2"
}
elseif ($Template -eq "2" -or $Template -eq "2012")
{
$TemplateName = "TempSF_Windows_2008_STD_R2"
}
elseif ($Template -eq "3" -or $Template -eq "SQL")
{
$TemplateName = "TempSFSQL"
}
else
{
write-host "Choose the right Template"
exit
}
}
}
elseif ($Region -eq "2" -or $Region -eq "EMEA")
{
$Site = read-host "Choose Site : `n 1) LON `n 2) DUB"
if ($Site -eq "1" -or $Site -eq "LON")
{
$Template = read-host "Choose Template : `n 1) 2008 `n 2) 2012 `n 3) SQL"
if ($Template -eq "1" -or $Template -eq "2008")
{
$TemplateName ="TempLON_Windows_2008_STD_R2"
}
elseif ($Template -eq "2" -or $Template -eq "2012")
{
$TemplateName = "TempLON_WIN_2012_R2_DATACENTER"
}
elseif ($Template -eq "3" -or $Template -eq "SQL")
{
$TemplateName = "LONSQL"
}
else
{
write-host "Choose the right Template"
exit
}
}
elseif ($Site -eq "2" -or $Site -eq "DUB")
{
$Template = read-host "Choose Template : `n 1) 2008 `n 2) 2012 `n 3) SQL"
if ($Template -eq "1" -or $Template -eq "2008")
{
$TemplateName = "DUB2008"
}
elseif ($Template -eq "2" -or $Template -eq "2012")
{
$TemplateName = "DUB2012"
}
elseif ($Template -eq "3" -or $Template -eq "SQL")
{
$TemplateName = "DUBSQL"
}
else
{
write-host "Choose the right Template"
exit
}
}
else
{
write-host "Invalid Site"
exit
}
}
elseif ($Region -eq "3" -or $Region -eq "APAC")
{
$Site = read-host "Choose Site : `n 1) SG `n 2) HK"
if ($Site -eq "1" -or $Site -eq "SG")
{
$Template = read-host "Choose Template : `n 1) 2008 `n 2) 2012 `n 3) SQL"
if ($Template -eq "1" -or $Template -eq "2008")
{
$TemplateName = "SG2008"
}
elseif ($Template -eq "2" -or $Template -eq "2012")
{
$TemplateName = "SG2012"
}
elseif ($Template -eq "3" -or $Template -eq "SQL")
{
$TemplateName = "SGSQL"
}
else
{
write-host "Choose the right Template"
exit
}
}
elseif ($Site -eq "2" -or $Site -eq "HK")
{
$Template = read-host "Choose Template : `n 1) 2008 `n 2) 2012 `n 3) SQL"
if ($Template -eq "1" -or $Template -eq "2008")
{
$TemplateName = "HK2008"
}
elseif ($Template -eq "2" -or $Template -eq "2012")
{
$TemplateName = "HK2012"
}
elseif ($Template -eq "3" -or $Template -eq "SQL")
{
$TemplateName = "HKSQL"
}
else
{
write-host "Choose the right Template"
exit
}
}
else
{
write-host "Invalid Site"
exit
}
}
else
{
write-host "Invalid Choice"
exit
}
#Getting VM Annotation Information
$IT_GroupKey = "IT_Group"
$OwnerKey = "Owner"
$Application_StackKey = "Application_Stack"
$LifecycleKey = "Lifecycle"
$PurposeKey = "Purpose"
$IT_Group = read-host "Enter IT Group: `n 1) Private `n 2) Public `n 3) Infrastructure"
if ($IT_Group -eq "1" -or $IT_Group -eq "Private")
{
$IT_GroupValue = "Private"
}
elseif ($IT_Group -eq "2" -or $IT_Group -eq "Public")
{
$IT_GroupValue = "Public"
}
elseif ($IT_Group -eq "3" -or $IT_Group -eq "Infrastructure")
{
$IT_GroupValue = "Public"
}
else
{
write-host "Invalid IT Group"
exit
}
start-sleep -s 2
$OwnerValue = read-host "Enter Owner Name"
start-sleep -s 2
$Application_StackValue = read-host "Enter Application Stack e.g) IT, Finance, Risk etc"
start-sleep -s 2
$Lifecycle = read-host "Enter IT Group: `n 1) Prod `n 2) Dev `n 3) QA `n 4) Test"
if ($Lifecycle -eq "1" -or $Lifecycle -eq "Prod")
{
$LifecycleValue = "Prod"
}
elseif ($Lifecycle -eq "2" -or $Lifecycle -eq "Dev")
{
$LifecycleValue = "Dev"
}
elseif ($Lifecycle -eq "3" -or $Lifecycle -eq "QA")
{
$LifecycleValue = "QA"
}
elseif ($Lifecycle -eq "4" -or $Lifecycle -eq "Test")
{
$LifecycleValue = "Test"
}
else
{
write-host "Invalid Lifecycle"
exit
}
start-sleep -s 2
$PurposeValue = read-host "Enter purpose of the VM"
start-sleep -s 5
$emailCC = read-host "Send Email to"
#Loop to Provision VMs
ForEach($VM in $VMs){
#Selecting a Random hots to place the VM. There is a maximum of 8 concurrent deployments to a single host. VAAI allows for 2 concurrent svmotions when running multiples of this script from the same template.
$ClusterHost = Get-Cluster $VM.Cluster | Get-VMHost | Where{$_.ConnectionState -eq "Connected"} | Get-Random
If($VM.IPaddress -gt 1){
#Define OS Customization with IP Addressing
If($TemplateName -eq "KKR_W2K8_R2_STD"){
$OSspec = "2008_Server_NY"
Get-OSCustomizationSpec $OSspec | Get-OSCustomizationNicMapping | Set-OSCustomizationNicMapping -IpMode UseStaticIp -IpAddress $VM.IPaddress -SubnetMask $VM.Netmask -DefaultGateway $VM.Gateway -Dns $VM.DNS1,$VM.DNS2
}
elseif ($TemplateName -eq "KKR_W2K12_R2_DC"){
$OSspec = "2012_Server_NY"
Get-OSCustomizationSpec $OSspec | Get-OSCustomizationNicMapping | Set-OSCustomizationNicMapping -IpMode UseStaticIp -IpAddress $VM.IPaddress -SubnetMask $VM.Netmask -DefaultGateway $VM.Gateway -Dns $VM.DNS1,$VM.DNS2
}
elseif ($TemplateName -eq "MP_Windows_2008_STD_R2"){
$OSspec = "2008_Server_MP"
Get-OSCustomizationSpec $OSspec | Get-OSCustomizationNicMapping | Set-OSCustomizationNicMapping -IpMode UseStaticIp -IpAddress $VM.IPaddress -SubnetMask $VM.Netmask -DefaultGateway $VM.Gateway -Dns $VM.DNS1,$VM.DNS2
}
elseif ($TemplateName -eq "MP_WIN_2012_R2_DATACENTER"){
$OSspec = "2012_Server_MP"
Get-OSCustomizationSpec $OSspec | Get-OSCustomizationNicMapping | Set-OSCustomizationNicMapping -IpMode UseStaticIp -IpAddress $VM.IPaddress -SubnetMask $VM.Netmask -DefaultGateway $VM.Gateway -Dns $VM.DNS1,$VM.DNS2
}
elseif ($TemplateName -eq "SF_Windows_2008_STD_R2"){
$OSspec = "2008_Server_SF"
Get-OSCustomizationSpec $OSspec | Get-OSCustomizationNicMapping | Set-OSCustomizationNicMapping -IpMode UseStaticIp -IpAddress $VM.IPaddress -SubnetMask $VM.Netmask -DefaultGateway $VM.Gateway -Dns $VM.DNS1,$VM.DNS2
}
elseif ($TemplateName -eq "SF_WIN_2012_R2_DATACENTER"){
$OSspec = "2012_Server_SF"
Get-OSCustomizationSpec $OSspec | Get-OSCustomizationNicMapping | Set-OSCustomizationNicMapping -IpMode UseStaticIp -IpAddress $VM.IPaddress -SubnetMask $VM.Netmask -DefaultGateway $VM.Gateway -Dns $VM.DNS1,$VM.DNS2
}
elseif ($TemplateName -eq "LON_Windows_2008_STD_R2"){
$OSspec = "2008_Server_Lon"
Get-OSCustomizationSpec $OSspec | Get-OSCustomizationNicMapping | Set-OSCustomizationNicMapping -IpMode UseStaticIp -IpAddress $VM.IPaddress -SubnetMask $VM.Netmask -DefaultGateway $VM.Gateway -Dns $VM.DNS1,$VM.DNS2
}
elseif ($TemplateName -eq "LON_WIN_2012_R2_DATACENTER"){
$OSspec = "2012_Server_Lon"
Get-OSCustomizationSpec $OSspec | Get-OSCustomizationNicMapping | Set-OSCustomizationNicMapping -IpMode UseStaticIp -IpAddress $VM.IPaddress -SubnetMask $VM.Netmask -DefaultGateway $VM.Gateway -Dns $VM.DNS1,$VM.DNS2
}
else
{
Write-Host Error: Template $TemplateName ~ is not the one specified in the file. Please make sure you are using the right template!!! -backgroundcolor "Yellow" -foregroundcolor "Black"
Exit
}
}
#Creating the New VM. SDRS automatic recomendations take hold.
New-vm -VMhost $ClusterHost -Name $VM.VMName -ResourcePool $VM.ResourcePool -Datastore $VM.Datastore -Template $TemplateName -Description $VM.Description -DiskStorageFormat "Thin" -OScustomizationSpec $OSspec
start-sleep -s 60
#Get-VM once for Optimization
$gVM = Get-VM $VM.VMName
#Setting the Configured Memory
$gVM | Set-VM -MemoryGB $VM.MemoryGB -NumCpu $VM.NumCpu -Confirm:$false
#Setting the PortGroup of Network Adapter 1
$gVM | Get-NetworkAdapter | Set-NetworkAdapter -NetworkName $VM.NetworkName -confirm:$false
#Setting the CustomAttribute Fields and Values
$gVM | Set-CustomField -Name $IT_GroupKey -Value $IT_GroupValue -confirm:$false
$gVM | Set-CustomField -Name $OwnerKey -Value $OwnerValue -confirm:$false
$gVM | Set-CustomField -Name $Application_StackKey -Value $Application_StackValue -confirm:$false
$gVM | Set-CustomField -Name $LifecycleKey -Value $LifecycleValue -confirm:$false
$gVM | Set-CustomField -Name $PurposeKey -Value $PurposeValue -confirm:$false
#Configuring OSdisk if more than template required
If ([int]$VM.OSdisk -gt 1){
$VMdisk1 = $gVM | Get-HardDisk | ?{$_.Name -eq "Hard disk 1"}
If ([int]$VM.OSdisk -gt $VMdisk1.CapacityGB){
$gVM | Get-HardDisk | ?{$_.Name -eq "Hard disk 1"} | Set-HardDisk -CapacityGB $VM.OSdisk -Persistence persistent -confirm:$false
}
}
start-sleep -s 20
#Adding HDD2 if exist and converting GB to KB
If ([int]$VM.Disk2 -gt 1){
$Disk2 = [int]$VM.Disk2
$gVM | New-HardDisk -CapacityGB $Disk2 -Persistence persistent -StorageFormat Thin
}
start-sleep -s 20
#Adding HDD3 if exist and converting GB to KB
If ([int]$VM.Disk3 -gt 1){
$Disk3 = [int]$VM.Disk3
$gVM | New-HardDisk -CapacityGB $Disk3 -Persistence persistent -StorageFormat Thin
}
#PowerOn VM
$gVM | Start-VM
#Next VM
}
#Send Email
$emailSmtpServer = "mailserver"
$emailFrom = "admin@company.com>"
$emailTo = user1@company.com
#$emailCC = user2@company.com
$emailSubjectInstance = "NEW VMWare VM created by $admin"
#$body = "The following servers were created $VM"
$body = "
New VMs created:
"
foreach($VM in $VMs){
$body = $body +
"
" + $VM.VMName + " " + "(" + $VM.IPAddress +")" + "`n"
$body = $body +
"Template" + " " + "(" + $TemplateName +")" + "`n"
$body = $body +
"Datastore" + " " + "(" + $VM.Datastore +")" + "`n"
$body = $body +
"Cluster" + " " + "(" + $VM.Cluster +")" + "`n"
$body = $body +
"ResourcePool" + " " + "(" + $VM.ResourcePool +")" + "`n"
$body = $body +
"Network" + " " + "(" + $VM.NetworkName +")" + "`n"
$body = $body +
"vCPU Assigned" + " " + "(" + $VM.NumCpu +")" + "`n"
$body = $body +
"Memory Assigned" + " " + "(" + $VM.MemoryGB +")" + "`n"
$body = $body +
"IT Group" + " " + "(" + $IT_GroupValue +")" + "`n"
$body = $body +
"Owner" + " " + "(" + $OwnerValue +")" + "`n"
$body = $body +
"Application Stack" + " " + "(" + $Application_StackValue +")" + "`n"
$body = $body +
"Lifecycle" + " " + "(" + $LifecycleValue +")" + "`n"
$body = $body +
"Purpose" + " " + "(" + $PurposeValue +")" + "`n"
}
Send-MailMessage -To $emailTo -Cc $emailCC -From $emailFrom -Subject $emailSubjectInstance -body "$body" -SmtpServer $emailSmtpServer
Message was edited by: vcloudguy