Hello
I am trying to recompose an existing vApp in vCD using “recomposevApp”, I have gleaned the below script from the
Link: https://communities.vmware.com/message/2309766
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)
So the solution is to input all of the entries processed from the CSV file, into an array.
Passing this array to the recomposevApp command will then do as I ask.
Example:
#------------------------------------------------------------------------
### 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
## this is the key, add each processed entry to an array
$array += $importedVMItem
## End of for each loop
}
## Now you can process the whole array
$vappView.RecomposeVApp($null, $null , $null, $null, $array, $null, $null, $null, $null, $vappView.Name, $vappView.Description)
Thread moved to vCloud Director PowerCLIvCloud Director PowerCLI
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
So the solution is to input all of the entries processed from the CSV file, into an array.
Passing this array to the recomposevApp command will then do as I ask.
Example:
#------------------------------------------------------------------------
### 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
## this is the key, add each processed entry to an array
$array += $importedVMItem
## End of for each loop
}
## Now you can process the whole array
$vappView.RecomposeVApp($null, $null , $null, $null, $array, $null, $null, $null, $null, $vappView.Name, $vappView.Description)
It appears this code is no longer working. I get an error when trying the += to add to the array plus the last command to recompose the vapp complains about missing the correct amount of paramters. Does anyone know how to correct this for a v9.7 vCloud Director install?