Correct me if I'm wrong, but there appears to be NO WAY to "add to inventory" a template file (i.e., vm.vmtx ) through PowerCLI? This can be done with a VIrtualMachine (i.e., vm.vmx) with the New-VM command, but not with a template file.
A workaround I tried was to copy the VMTX to a VMX file, but the problem is that it doesn't preserve the snapshots that are embeded on in the template and is not 100% reliable.
Thanks in advance!
There used to be a problem with the call to RegisterVM_Task from PowerShell. But I'm not sure if that still is the case in PowerCLI 5.
You can find a bypass (lines 88-95) in my Raiders of the Lost VMX post, which you can also use to register vmtx files btw.
If the name parameter is $null, the template will be registered with the DisplayName taken from the vmtx file
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Hello-
You could use the RegisterVM_Task() method of a Folder object from the API to register the template's .vmtx file. This method has a parameter that allows you to register the item as a template. Something like:
## folder in which to put the registered template
$strMyFolderName = "myTemplates"
## host on which to register the template
$strMyHostName = "myhost.domain.com"
## the datastore path to the .vmtx file
$strMyTemplateDatastorePath = "[myDatastore] mytemplate0/mytemplate0.vmtx"
## get the .NET View object for the destination folder
$viewDestinationFolder = Get-View -ViewType Folder -Property Name -Filter @{"Name" = $strMyFolderName}
## register the template (third param of $true means to register as template) -- reference: http://pubs.vmware.com/vsphere-50/index.jsp?topic=/com.vmware.wssdk.apiref.doc_50/vim.Folder.html&pa...
$viewDestinationFolder.RegisterVM_Task($strMyTemplateDatastorePath, "mytemplate0", $true, $null, (Get-View -ViewType HostSystem -Property Name -Filter @{"Name" = $strMyHostName}).MoRef)
How does that do for you?
Message was edited by mattboren: minor code correction
Matt,
Thanks for that! That's a great method. However, I couldn't figure out why I get this error:
My variable $strMyTemplateDatastorePath has only 79 characters whereas the documentation says it may not be more than 80 characters. I created another vmtx and shorted the name down to 68 characters and still get the same error. Any more clues?
I think you looked at the wrong property in the SDK Ref, it's the Name property that is limited to 80 characters, not the Path.
I suspect the problem is related to the "invalid" indication in the message.
Would you mind showing us the Path you have in $strMyTemplateDatastorePath ?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
$strMyTemplateDatastorePath = [AA2222_XFS_NFS] Windows2003r2_Standard_32bit/Windows2003r2_Standard_32bit.vmtx
Hello, markvcp-
Like Luc said, it is the "name" parameter that has the 80-character limit. While the documentation also says that the Name parameter "Need not be set", as indicated by the red asterisk after the parameter name, its seems that you may have to supply a non-null value for Name. In my testing, I get the same exception message you posted when using $null for the Name parameter.
What value are you using for the Name parameter? If an empty string or $null, can you try it with an actual value, and see how it does?
There used to be a problem with the call to RegisterVM_Task from PowerShell. But I'm not sure if that still is the case in PowerCLI 5.
You can find a bypass (lines 88-95) in my Raiders of the Lost VMX post, which you can also use to register vmtx files btw.
If the name parameter is $null, the template will be registered with the DisplayName taken from the vmtx file
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
The NAME paramenter was the missing link for me and this method works.
I would have never thought to look on the FOLDER vsphere object for a the method to add a task to inventory.
To Honorable Matt and LucD, thank you for your help.
It seems that the problem does still exist in PowerCLI 5. Though the docs clearly say that the "name" parameter need not be set, and that the DisplayName from the config file will be used if "name" is $null, I had to specify a value for "name" in both PowerCLI v4 and v5. Using the name specified in the vmx/vmtx file is definitely preferable.
Pretty sly work-around, Luc -- getting the method and then using Invoke() to call it. Thanks for pointing out that subtle feature.