Hello
I am trying to recompose an existing vApp in vCD using “recomposevApp”, I have gleaned the below script from the
Link:
I don’t need the networking part of this script, I just want multiple vms created from a template, but bringing in the details from a csv file.
The below seems to run through, but then only create the last VM in the CSV file, I would like to be able to run through the csv file, adding them all, then recompose, which will build them all simultaneously.
Can anyone see what i may be doing wrong .....
#------------------------------------------------------------------------
### CSV File ###
vApp template newVMName compname
test01 win_07 vm01 vm01
test01 win_07 vm02 vm02
test01 win_07 vm03 vm03
### The script ###
$configs = import-csv “C:\users\user\vCloudImporttest.csv” (Copy of the CSV file is above)
Foreach ($item in $configs) {
$vAppName = $item.vApp
$templateVMName = $item.template
$newVMName = $item.newVMName
$compName = $item.compName
# Get API views to the target vApp, the template VM
# vApp Extensiondata
$vapp = Get-CIVApp $vappName
$vappView = $vapp.ExtensionData
# select template details of template being cloned
$vmTemplateRecord = Search-Cloud -QueryType "vm" -Name $templateVmName -Filter "IsVAppTemplate==True"
# cloud view of template in question
$vmTemplateView = Get-CIView -Id $vmTemplateRecord.Id
# Define the guest OS settings of the newly created VM by configuring the new Computer Name (hostname).
$templateGuestSection = $vmTemplateView.Section | where { $_ -is [VMware.VimAutomation.Cloud.Views.GuestCustomizationSection] }
# Creates a new object of guestCustomizationSection, and reconfigure it under $guestSection variable for use in new VM Creation
# items defined here will be brought in lower down in $importedVMItem section.
[VMware.VimAutomation.Cloud.Views.GuestCustomizationSection] $guestSection = `
New-Object VMware.VimAutomation.Cloud.Views.GuestCustomizationSection
$guestSection.Enabled = $templateGuestSection.Enabled
$guestSection.ComputerName = $compName
$guestSection.Info = $templateGuestSection.Info
# Define the recomposition specification
# creates a new object of sourcedCompositionItemParam, assigns new values to it for use in new VM Creation
[VMware.VimAutomation.Cloud.Views.SourcedCompositionItemParam] $importedVmItem = `
New-Object VMware.VimAutomation.Cloud.Views.SourcedCompositionItemParam
$importedVmItem.Source = New-Object VMware.VimAutomation.Cloud.Views.Reference
$importedVmItem.Source.Href = $vmTemplateView.Href # Specify the template VM to be used
$importedVmItem.Source.Name = $newVMName # Specify the name of the newly create VM
$importedVmItem.InstantiationParams = New-Object VMware.VimAutomation.Cloud.Views.InstantiationParams
$importedVmItem.InstantiationParams.Section = $guestSection # Define the computer name of the new VM
# here all the bits come together,
[VMware.VimAutomation.Cloud.Views.SourcedCompositionItemParam[]] $sourcedItem = , $importedVmItem
}
Write-Host "Cloning VM '$templateVmName' into vApp '$vappName' under the name '$newVMName' with Computer Name '$compName'"
$vappView.RecomposeVApp($null, $null , $null, $null, $sourcedItem, $null, $null, $null, $null, $vappView.Name, $vappView.Description)