How to add a VM to Content Library using PowerCLI

This task sounds very simple.  Target the VM, then import to the primary content library for replication to the subordinate libraries.  This task can be accomplished right now with a few clicks in the GUI.  I’m not a GUI kind of person, I like the mash the keys to get stuff done.  So after a lot of research, I found that the PowerCLI commands to perform this function just don’t exist at this point.

During my research, I came across a website ( from the VMware PowerCLI team, that is devoted to getting user suggestions for new features for PowerCLI. It seems that others have requested this feature for PowerCLI and the functions for the content library.  The original suggestion was submitted by Alan Renouf (  So if Alan is asking for this feature, I might be out of luck, but I didn’t stop there.

I then came across a post from William Lam that looked promising.  The post titled vSphere Content Library PowerCLI community module ( seem to have everything that I needed, but not how to import to the content library.  This is getting me pointed into the correct direction for writing my own function.

More research on this topic has now has lead me to a post ( on the VMware communities forum. Where Luc Dekens (, replied about using a new function from William Lam.  The only issue is that it was wrote for VMC (VMware Cloud).  There was another reply on the same thread about a powerCLI script writen by a user named LokeshHK that he uploaded to the VMware {code} site (  This was what helped me to get what I needed as it had the proper syntax calling the APIs via PowerCLI.

Developing the API Call

Using the API explorer (https://YourvCenterNameHere/apiexplorer/) built into vCenter you can found out information about the varies APIs that are available.  I need to find the API call that best fit the need I was looking for.


After looking through the API explorer, I found the ovf/library_item API.  This is used to create a library item in the content library from a virtual machine or virtual appliance. This one was perfect for what I needed.

vCenter API Explorer ovf/library_item API

Clicking on the API shows details about the API itself, like the syntax and how the API provides a response back.  Using the API explorer, you can make the API call by providing the needed information via the interface, and clicking the ‘TRY IT OUT!’ button.  It will then attempt to make the API call, and provide you with the fully formed API code.

Converting API to PowerCLI

PowerCLI has a cmdlet for using the API calls, Get-CisService.  So to begin, you will need to authentication to the vSphere Automation SDK server using Connect-CisServer.  Once connected you can call the API.

$ContentLibraryOvfService  = Get-CisService com.vmware.vcenter.ovf.library_item

This command starts the configuration of the API which is saved as the variable $ContentLibraryOvfService.  To be able to properly make this API call you have to gather some information to be passed with the final call.

Below is the information that the API call is looking to receive to process the call correctly.

  "source": {
  "type": "string",
  "id": "string"
  "client_token": "string",
  "target": {
  "library_id": "string",
  "library_item_id": "string"
  "create_spec": {
     "flags": [
  "name": "string",
  "description": "string"

So using the new variable above, I need to start to adding the data to it.

Getting the ‘client_token’ is by far the easiest.

$UniqueChangeId = [guid]::NewGuid().tostring()

Getting the ‘target’ information requires the id of the content library.  If I was updating the item already in the content library it would then need the id of the item that already exists in the content library as well.

$ContentLibraryService = Get-CisService com.vmware.content.library
$libaryIDs = $contentLibraryService.list()
foreach($libraryID in $libaryIDs){
    $library = $contentLibraryService.get($libraryID)
    if($ -eq $LibraryName){
        $library_ID = $libraryID
$createOvfTarget = $$createOvfTarget.library_id = $library_ID

Now to gather the needed info for the ‘source’, which requires 2 pieces of info, the type and ID of the Virtual Machine.

$createOvfSource = $ContentLibraryOvfService.Help.create.source.Create()&lt;br /&gt;$createOvfSource.type = ((Get-VM $VMname).ExtensionData.MoRef).Type&lt;br /&gt;$ = ((Get-VM $VMname).ExtensionData.MoRef).Value&lt;/p&gt;<br>

Now the gather the last of the information, the ‘create_spec’.  This consists of the name and description that the item will get once imported into the content library

$createOvfCreateSpec = $;br /&gt;$ = $LibItemName&lt;br /&gt;$createOvfCreateSpec.description = $Description

Now just to put all of this script together into a single function.

Function Add-TemplateToLibrary {
Created by : Stuart Yerdon
Website :
This function uploads a VM to the Content library.
.PARAMETER LibraryName
Name of the libray to which item needs to be uploaded.
Name of the VM to upload.
Name of the template after imported to library.
.PARAMETER Description
Description of the imported item.
Add-TemplateToLibrary -LibraryName 'LibraryName' -VMname '2016 STD Template v1.0 VM' -LibItemName '2016 STD Template' -Description 'Uploaded via API calls.'


$ContentLibraryService = Get-CisService com.vmware.content.library

$libaryIDs = $contentLibraryService.list()
foreach($libraryID in $libaryIDs) {
$library = $contentLibraryService.get($libraryID)
if($ -eq $LibraryName){
$library_ID = $libraryID

write-host -ForegroundColor red $LibraryName " -- is not exists.."
} else {
$ContentLibraryOvfService = Get-CisService com.vmware.vcenter.ovf.library_item
$UniqueChangeId = [guid]::NewGuid().tostring()

$createOvfTarget = $
$createOvfTarget.library_id = $library_ID

$createOvfSource = $ContentLibraryOvfService.Help.create.source.Create()
$createOvfSource.type = ((Get-VM $VMname).ExtensionData.MoRef).Type
$ = ((Get-VM $VMname).ExtensionData.MoRef).Value

$createOvfCreateSpec = $
$ = $LibItemName
$createOvfCreateSpec.description = $Description
#$createOvfCreateSpec.flags = ""

write-host "Creating Library Item -- " $LibItemName
$libraryTemplateId = $ContentLibraryOvfService.create($UniqueChangeId,$createOvfSource,$createOvfTarget,$createOvfCreateSpec)




Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.