I am attempting to register some VMs from disk files into a VCenter 6.7 server, and have them appear into specific VApps. For this situation, certain information about the VMs (datastore, filename, target Vapp name, etc.) is encapsulated in a CSV file. The original VCenter which housed the VMs previously is not available to be queried.
The problem I am running into is that, while the VMs in my test group successfully register, they appear at the root of the cluster, and not in the desired vapp under the appropriate resource group.
I've tried a number of different ways of specifying the new-vm parameters, and none seem to work. Below is my most recent attempt.
$LegacyVM is the record drawn from the CSV file. The documentation on new-vm says that the -vapp parameter is deprecated, but that -resourcepool accepts a vapp as a value, so I supply the vapp name there. From the "echo" output, I know that the vapp name I supply is valid and get-vapp returns the desired vapp object. I'm running the script with a full admin account, so it shouldn't be a permissions issue.
I assume I'm missing something simple and obvious, but I can't seem to figure out what. As I need to do this for several hundred VMs, I'd like to figure it out. Any pointers appreciated.
Script snippet
$TargetServer = 'vcenter-name.domain.com'
$TargetHost = 'host-name.domain.com'
$TargetVApp = get-vapp -name $LegacyVM.NewVapp -server $TargetServer
$TargetFilePath = $LegacyVM.NewFilePath
$TargetVM = $LegacyVM.FullName
$TargetNotes = $LegacyVM.Notes
Echo "Registering "$LegacyVM.FullName
echo " TargetVapp: "$TargetVapp
new-vm -VMFilePath $TargetFilePath -ResourcePool $TargetVapp -Name $TargetVM -Notes $TargetNotes -RunAsync -Server $TargetServer -VMHost $TargetHost