PowerCLI to Clone a vApp


Jonathan Frappier Virtxpert

So had a need to clone a vApp several times, I finally got around to automating thanks again to PowerCLI.  A few things I had to consider; with the New-VApp cmdlet you cannot select portgroups so I had to do that after the vApp was clone and also needed to put the vApp into a specific folder after it was clone.  Otherwise, it was actually kind of easy to figure out based on what I needed to accomplish.  Here it is, in case you need to accomplish it as well :)

#Get vApp names and port groups
$CSVfile = "c:adminscriptsehc_vapps.csv"

# Set PowerCLI Options
Set-PowerCLIConfiguration -InvalidCertificateAction Ignore -Confirm:$false | Out-Null

$EHC_vApps = Import-Csv -Path $CSVfile
ForEach ($EHC_vApp in $EHC_vApps)
{
#Creates new vApp
New-VApp -Name $EHC_vApp.name -Datastore $EHC_vApp.datastore -Location $EHC_vApp.cluster -VApp $EHC_vApp.template

#Get list of vApp VMs to set network card
$vApp_vms = Get-VApp $EHC_vApp.name | Get-VM
ForEach ($vApp_vm in $vApp_vms)
{
Get-VM -Location $EHC_vApp.name $vApp_vm | Get-NetworkAdapter | Set-NetworkAdapter -Portgroup $EHC_vApp.portgroup -Confirm:$false
}

#Move vApp to StudentPod folder
Move-VApp -Destination $EHC_vApp.folder -VApp $EHC_vApp.name
}

PowerCLI to Clone a vApp