GarrettHayes
Contributor
Contributor

Trouble Registering a VM to a Specific VApp

Jump to solution

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

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership

Which PowerCLI version?
Did you already try leaving out the RunAsync switch?

The RegisterChildVM method, which might be the method the New-VM cmdlet is using in this ParameterSet, contains a strange remark.
"This operation only works for vApps or resource pools that are children of vApps."
Did you try with a nested vApp?

Does the action work from the Web Client?
If yes, you might want to use CodeCapture to see what is actually called.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

0 Kudos
5 Replies
LucD
Leadership
Leadership

Which PowerCLI version?
Did you already try leaving out the RunAsync switch?

The RegisterChildVM method, which might be the method the New-VM cmdlet is using in this ParameterSet, contains a strange remark.
"This operation only works for vApps or resource pools that are children of vApps."
Did you try with a nested vApp?

Does the action work from the Web Client?
If yes, you might want to use CodeCapture to see what is actually called.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

0 Kudos
GarrettHayes
Contributor
Contributor

LucD,

Thanks for the suggestions!  To answer your questions:

1) No - I hadn't tried omitting the RunAsync switch.  I will give that a try.

2) No - no Nested Vapps.  I'll have to give that some thought as to how best to approach.

3) Yes, it appears to work from the GUI.  (Tried so many variants, I won't swear to that without another test, but that's my recollection.) I'm not familiar with CodeCapture.  I shall inquire of Mr. Google about that.  It sounds promising.

GMH

0 Kudos
GarrettHayes
Contributor
Contributor

Sorry - I missed the question about PowerCLI Version. 

I'm running version 12.2.0 build 17538434

GMH

0 Kudos
LucD
Leadership
Leadership

Kyle wrote a good Intro to Code Capture.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
GarrettHayes
Contributor
Contributor

LucD,

Thank you.  A CodeCapture session held the answer.

I successfully registered a sample VM manually through the GUI.  Based on what was captured, I made two changes to the code. 

  1. Added a pipe to "get-view" on the line setting the variable for the target Vapp.
  2. Changed from "new-vm" command to a "Register Child VM" method  (as you mentioned)

The script now works, shuffling the VMs to the desired Vapps!  I will definitely check out the blog post on CodeCapture.  But 30 seconds using it tells me it'll be a major plus in my tool bag.  Thanks very much for the pointers.  

GMH

Revised Script Snippet

$TargetVApp = get-vapp -name $LegacyVM.NewVapp -server $TargetServer |get-view
$TargetFilePath = $LegacyVM.NewFilePath
$TargetVM = $LegacyVM.FullName
$TargetNotes = $LegacyVM.Notes

Echo "Registering "$LegacyVM.FullName
echo " TargetVapp: "$TargetVapp
$TargetVApp.RegisterChildVM_Task($TargetFilePath, $TargetVM, $null)

0 Kudos