VMware Cloud Community
szemmali
Contributor
Contributor

Deploy VMs(Excel file) from template and Configure the Guest OS

Powershell script to read CSV file and Create new VMs from template With vCenter Credential

#=================================================================================

##       Project:  create-vm-from-template

##        AUTHOR:  SADDAM ZEMMALI

##         eMail:  saddam.zemmali@gmail.com

##       CREATED:  20.11.2018 02:03:01

##      REVISION:  --

##       Version:  1.0  ¯\_(ツ)_/¯

##    Repository:  ---------------------------------------------------

##          Task:  Create new VM from template With vCenter Credential

##          FILE:  create-vm-from-template.ps1

##   Description:  Deploy VMs from template and Configure the Guest OS

##   Requirement:  --

##          Note:  Connect With USERNAME/PASSWORD Credential

##          BUGS:  Set-ExecutionPolicy -ExecutionPolicy Bypass

##=================================================================================

$StartTime = Get-Date

#################################

#       vSphere Variables       #

################################# 

$vCenter = Get-Content "PATH:\vCenter.txt"

$username = Read-Host 'Enter The vCenter Username'

$password = Read-Host 'Enter The vCenter Password' -AsSecureString   

$vccredential = New-Object System.Management.Automation.PSCredential ($username, $password)

#################################

#     LOG INFO                  #

################################# 

#################################

#   Virtual Machine Variables   #

#################################

$NUMvCPU = '2'

$RAM =  '4'

$DiskSpace = '40'

$VMName = Read-Host 'Enter The VM Name'

$DomainControllerVMName = "DESIRED DC NAME HERE"

$FSVMName = "DESIRED FS NAME HERE"

$TargetCluster = Get-Cluster -Name "TARGET CLUSTER IN VCENTER"

$SourceVMTemplate = Get-Template -Name "SOURCE TEMPLATE IN VCENTER"

$SourceCustomSpec = Get-OSCustomizationSpec -Name "SOURCE CUSTOMIZATION SPEC IN VCENTER"

#######################################################

# Get list of Computer names and IP address from FILE #

#######################################################

$CSVFile = ipcsv -Path "C:\list_vms.csv"

####################################

# IP Settings included in the GOSC #

####################################

$IPMask = "255.255.255.0"

$IPGateway = "a.b.c.d"                                    

$DNS = "8.8.8.8" 

#################################

#  GOSC and Template Variables  #

#################################

$GOSCName = "GOSC Name"

$TempName = "Template Name"

$TempToUse = Get-Template -Name $TempName

#################################

#      Connecting to vCenter    #

#################################

Write-Host "Connecting to $vCenter..." -Foregroundcolor "Yellow" -NoNewLine

$connection = Connect-VIServer -Server $vCenter -Cred $vccredential -ErrorAction SilentlyContinue -WarningAction 0 | Out-Null

$DS = Get-Cluster -Name $CLUSTER | Get-Datastore | Select Name, FreeSpaceGB | Sort-Object FreeSpaceGB -Descending | Select -first 1

If($? -Eq $True){

    Write-Host "Connected" -Foregroundcolor "Green"

    write-host "************ Creating $VMName; DO NOT CLOSE/STOP THIS SCRIPT! ************" -f red

   

    ##################

    #   Creating VM  #

    ##################

    foreach ($VM in $CSVFile){

        #Read the computer name and IP address

        $CompName = $VM.VMName

        $IPAddr = $VM.IPAddress

    

        #Amend the computer name and IP address in the GOSC

        Write-Host ("GOSC : Adding " + "IP Address" + $IPAddr + " and Computer Name " + $CompName)

        Get-OSCustomizationSpec $GOSCName | Set-OSCustomizationSpec -NamingPrefix $CompName -NamingScheme fixed | Out-Null

        Get-OSCustomizationNicMapping -OSCustomizationSpec $GOSCName | Set-OSCustomizationNicMapping -IpMode UseStaticIP -IpAddress $IPAddr -SubnetMask $IPMask -DefaultGateway $IPGateway -Dns $DNS | Out-Null

        #Create a new VM based on the set template and GOSC

        Write-Host ("Deploying : " + $CompName + " from template " + $TempName + "`n")

        New-VM -Name $CompName -Template $TempToUse -OSCustomizationSpec (Get-OSCustomizationSpec -Name $GOSCName) -Location $Location -VMHost $ESXHost -Datastore $Datastore | Out-Null

    }

    ##################

    #   Starting VM  #

    ##################

    foreach ($VM in $CSVFile){

        Write-Host("Power Up : " + $VM.VMName)

        Start-VM -VM $VMName -confirm:$false -RunAsync

    }

Else{

      Write-Host "Error in Connecting to $vCenter; Try Again with correct user name & password!" -Foregroundcolor "Red"

}   

##############################

# Disconnect session from VC #

############################## 

Write-Host "Disconnect to $vCenter..." -Foregroundcolor "Yellow" -NoNewLine

$disconnection =Disconnect-VIServer -Server $vCenter  -Force -confirm:$false  -ErrorAction SilentlyContinue -WarningAction 0 | Out-Null

If($? -Eq $True){

    Write-Host "Disconnected" -Foregroundcolor "Green"

    Write-Host "#####################################" -Foregroundcolor "Blue"

}

Else{

    Write-Host "Error in Disconnecting to $vCenter" -Foregroundcolor "Red"

}

##############################

#       End of Script        #

##############################

$EndTime = Get-Date

$duration = [math]::Round((New-TimeSpan -Start $StartTime -End $EndTime).TotalMinutes,2)

Write-Host "================================"

Write-Host "Create new VM from template Completed!"

Write-Host "StartTime: $StartTime"

Write-Host "  EndTime: $EndTime"

Write-Host "  Duration: $duration minutes"

Write-Host "================================"

1 Reply
Sunny4422000
Contributor
Contributor

Hi Mate

Thank you for the detailed script. I've used it deploy several virtual machines from this and have a small question

This issue is more around the Set-OSCustomizationNicMapping

Get-OSCustomizationNicMapping -OSCustomizationSpec $GOSCName | Set-OSCustomizationNicMapping -IpMode UseStaticIP -IpAddress $IPAddr -SubnetMask $IPMask -Def

Script works fine for the first set of deployment. If i try to deploy more VM's using the script i receive below error mesg

New-VM The operation for the entity "Temp2016Std_1607" failed with the following message: "The number of network adapter settings in the

customization specification: 0 does not match the number of network adapters present in the virtual machine: 1."

At line:29 char:1

+ New-VM -Name $VMName -Template $Template -OSCustomizationSpec $custom ...

+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    + CategoryInfo          : NotSpecified: (:) [New-VM], NicSettingMismatch

    + FullyQualifiedErrorId : Client20_TaskServiceImpl_CheckServerSideTaskUpdates_OperationFailed,VMware.VimAutomation.ViCore.Cmdlets.Commands.NewVM

The workaround I used is to reset the Customization script to default under network from "Manually Select Custom settings" to "use Standard network Settings for the operating System, including DHCP on all network Interfaces" . This is don on the GUI.

I've tried using the below cmdlet but it didn't help,

Get-OSCustomizationSpec $Custom | Get-OSCustomizationNicMapping | Remove-OSCustomizationNicMapping -Confirm:$false

Any suggestions on overcoming this?

Thank you in advance

Sunny

Reply
0 Kudos