I was recently doing some work with the vSphere Content Library and I needed to extract some information using the Content Library API. I was hoping there were a few out of the box PowerCLI cmdlets but to my surprise, there was only one cmdlet called Get-ContentLibraryItem which did not get into the level of details that I was looking for, which was on a per-file level rather than item-level. I had also search the web to see if anyone had built any functions and although a few existed, none that did the very basics which was retrieving all libraries, library items and library item files.
UPDATE (05/05/19) - Updated module with a new function for creating a subscribed Content Library, below is an example:
New-SubscribedContentLibrary -LibraryName NestedESXi -DatastoreName vsanDatastore -SubscriptionURL https://download3.vmware.com/software/vmw-tools/lib.json -SubscriptionThumbprint "7a:c4:08:2d:d3:55:56:af:9f:26:43:65:d0:31:99:0b:d2:f3:d8:69" -OnDemand
UPDATE (11/14/17) - I have recently updated the Content Library module to enhance the Get-ContentLibrary function and added several additional functions listed below
- Set-ContentLibrary
- New-ExtReplicatedContentLibrary
- Remove-SubscribedContentLibrary
- New-LocalContentLibrary
- Remove-LocalContentLibrary
- Copy-ContentLibrary
Not having worked with the Content Library APIs before, I figure this would be a good learning opportunity and created a PowerCLI module called ContentLibrary.psm1 which contains the following functions:
- Get-ContentLibrary
- Get-ContentLibraryItems
- Get-ContentLibraryItemFiles
Before you can make use of these functions, make sure to connect to the CIS endpoint of your vCenter Server using the Connect-CisServer cmdlet.
Here is a screenshot of the vSphere Web Client showing all available Content Libraries:
Here is an example of retrieving the same information using the following command:
Get-ContentLibrary
Note: You also have the ability to filter for the name of a specific Content Library by using the -LibraryName parameter
Here is a screenshot of the vSphere Web Client showing the individual Items within a Content Library:
Here is an example of retrieving the same information using the following command:
Get-ContentLibraryItems -LibraryName Test
Note: You also have the ability to filter for the name of a specific Content Library Item by using the -LibraryItemName parameter
Although the granularity when using the vSphere Web Client is at a Content Library Item, using the Content Library API, you can get additional information on the Item itself which is then composed of a set of files. The following command will allow you to retrieve all Files or you can use -LibraryItemName parameter to filter on a specific Item.
Get-ContentLibraryItemFiles -LibraryName Test -LibraryItemName TinyVM
Ben Liebowitz says
Question, this section of the vSphere 6 documentation says it's Get-contentlibraryitem and your post says get-contentlibraryitems... which is it? 🙂
http://pubs.vmware.com/vsphere-6-0/index.jsp?topic=%2Fcom.vmware.powercli.ug.doc%2FGUID-906CEFBF-1EB4-41FC-9353-0E9AB71FC78C.html
William Lam says
You didn't read the blog post, did you? 😉 The singular is OOTB cmdlet which doesn't give too much info, I've implemented my own set of functions, hence plural
Ganadmin says
Is there anyway we can convert the item in content-lib directly to usual template instead of converting it to the VM and the template because we use cloud-stack for our internal cloud which will see only items in VMs and templates folders and not in the content-library ..
William Lam says
No, there is not. A VM Template in Content Library is completely different than a traditional VM Template and even the way it is stored differs. My suggestion is for the solution to integrate w/Content Library APIs directly, so you benefit from using CL 🙂 rather than limiting it to what traditional templates can do and not taking advantage of what CL provides
Ganadmin says
Thanks ..
lowercasew says
William,
I'm not sure if this is possible but, at my previous job, I had written a PowerCLI Script to take our templates once a month, convert them to VMs, install the latest windows updates, and then convert it back to template. I'd like to do something similar with the Content Library. I know the VM cannot be converted within the library, but are there cmdlets to export the VM from the library and download the OVA? From there I can script deploying the OVA into vCenter, installing the required updates, etc. I'd also need a way to delete the existing item in the Content Library and then import the new VM, replicate it, etc.
Thoughts?
Thanks!
Ben Liebowitz
Ron Brown says
Thank you for your work on this module. It gets me past a delegation issue I'm encountering when remotely running the default Get-ContentLibraryItem. But a new issue has arisen.
When I use the Get-ContentLibraryItems function to get a content library template, the object type that comes back is [pscustomobject] (as you specify in the function) but that object type cannot be passed to a New-VM command with the -ContentLibraryItem flag. Here is the error...
"Cannot bind parameter 'ContentLibraryItem'. Cannot convert the "" value of type "System.Management.Automation.PSCustomObject" to type "VMware.VimAutomation.ViCore.Types.V1.ContentLibrary.ContentLibraryItem"."
Is there a way to get the object that comes back from the function to be in the ContentLibraryItem object type?
anthonysomerset says
I've got this same issue
I can't use the Vmware provided cmdlet because i have a master lib and 2 subscribed libs within my Vmware deployment (so i have local to site libraries) so i can't filter down to a specific Library and Item otherwise
anthonysomerset says
My workaround ironically is to store the Get-ContentLibraryItems output to a variable e.g. $libitems, then call Get-ContentLibraryItem -Id $libitems.id and then carry on as before
David says
Is there a way to see the total uncommitted size of the drives from the contentlibraryitem? I know we can get this information from a template and surprised it's not available when importing to contentlibrary. I need this so I can calculate any over commitment before deploying the VM from the contentlibrary.
THanks
AlphaMale says
Hi William
This is great functionality, thanks. I'm trying to go a bit further. Any hints or tips on how I can add an OVA/OVF to the Content Library using PowerShell?
William Lam says
Take a look at this Content Library Developer Series https://blogs.vmware.com/developer/2015/05/content-library-blog-series.html which goes over the CL APIs needed to upload either ISO, OVF/OVA or any other file for that matter. All of these APIs can be access through PowerShell, if you decide to implement this, consider a pull request back 🙂
Daniel Petcher says
I can see how the Content Library allows me to distribute OVF/OVA and ISO data across my datacenters, but what about using the data on a guest VM? Is it possible, yet, to code a script that will tell a guest VM to mount a particular Content Library ISO? (and un-mount it when done, of course.)
Ben Conrad says
Get-ContentLibraryItemFiles works when connected to a single CIS server. When I connect to a second CIS server I get the error below. It's bombing out in the module here:
>>>> $library = $contentLibraryService.get($libraryId)
Any ideas?
PS C:\> Get-ContentLibraryItemFiles -LibraryName MyLibrary
Get-ContentLibraryItemFiles : Cannot convert argument "0", with value: "948c0dbc-c916-4d44-80c5-dc23b3868039", for "Get" to type "System.Int32": "Cannot convert the "948c0dbc-c916-4d44-80c5-dc23b3868039" value of type
"VMware.VimAutomation.Cis.Core.Types.V1.ID" to type "System.Int32"."
At line:1 char:1
+ Get-ContentLibraryItemFiles -LibraryName MyLibrary
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Get-ContentLibraryItemFiles], MethodException
+ FullyQualifiedErrorId : MethodArgumentConversionInvalidCastArgument,Get-ContentLibraryItemFiles
David N says
Hi, is anyone getting this whilst trying to create a new subscriber library? Subsequent errors appear but only including the first.
Method invocation failed because [System.Management.Automation.PSCustomObject] does not contain a method named 'Create'.
At V:\Scripts\Content Library Palo Alto\ContentLibrary.psm1:344 char:9
+ $createSpec = $subscribeLibraryService.Help.create.create_spe ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (Create:String) [], RuntimeException
+ FullyQualifiedErrorId : MethodNotFound
David N says
Think I found it - in the New-ExtReplicatedContentLibrary the $subscriberurl is built from a combination of $datastoreUrl + $SubscribeLibraryName + "/lib.json"
When I changed the $subscribeurl to point at the Local library https json linke, the create New-ExtReplicatedContentLibrary worked....
Jason says
Is there a new-SubscibedContentLibrary command in the works? What can I use in its stead?
Jason says
Thank you so much for the New-SubscribedContentLibrary. I'll be testing it soon. Thanks
animatco says
I know this is an old thread, but is there a way to get the status of the subscribed content library. The reason I am asking is that we have many centers (last count that I have found is around 25, way to many but I an not in charge) with various WAN speeds between them and the master content library. It would be handy to be able to find out the status of the Sync.
lamw says
afaik, there's not a single Sync property status to check and Content Library operates on a per-file basis, so you'd need to iterate through the library and checking for something like size property https://developer.vmware.com/docs/vsphere-automation/latest/content/data-structures/Library/ItemModel/ which would indicate that its not been downloaded to disk (this assumes you're not using On-Demand Sync, where the value would be 0)
animatco says
I was afraid of that.
Jorge Garcia says
Hi William, i have been trying to get disk drive info for an OVF template in CL using VAPI with no success, specifically with this object type> com_vmware_content_type_ovf_vm__template, have you done that or know if it can be done via powerCLI?
William Lam says
Have you taken a look at API ref at developer.vmware.com? That’ll tell you if the info you’re looking for is available
Jorge Garcia says
Hi william, i am looking at this datastructure type "TypeOvfVmTemplate" but i am not sure how to query it, https://developer.vmware.com/docs/vsphere-automation/v7.0.0/content/data-structures/Type/Ovf/VmTemplate/
Rolf says
Hey William,
Awesome blog I am though having issuse running it
Get-ContentLibrary
Get-CisService : You are not currently connected to any servers. Please connect first using a Connect cmdlet.
At C:\Users\rolfb\Documents\Untitled1.ps1:25 char:30
+ ... $contentLibraryService = Get-CisService com.vmware.content.library
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Get-CisService], ViServerConnectionException
+ FullyQualifiedErrorId : VMware.VimAutomation.Sdk.Types.V1.ErrorHandling.VimException.ViServerConnectionException,VMware.VimAutomation.Cis.Core.Commands.Cmdlets.Ge
tCisService
You cannot call a method on a null-valued expression.
At C:\Users\rolfb\Documents\Untitled1.ps1:26 char:5
+ $LibraryIDs = $contentLibraryService.list()
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
Rolf says
Nevermind, my bad, was not using the Connect-CiSServer Command
Matt says
I've run into an oddity attempting to use both the community scripts and the ones built into the latest version of powercli.
On vCenter 7u3 / vSphere 7u3, when creating a subscribed content library from a local content library using powercli, the subscribed content library is created and my ISO's in 'Other Types' sync, but not my VM Templates. In fact, when looking at the local content library, it doesn't even show the subscribed library as a subscriber.
I then noticed that the subscribed URL, while right, is missing the port :443. even though it was included in the powershell command, something in the process seems to be stripping it out?
Going to the vCenter HTML5 interface. I can reproduce the issue.... Create a content library copy/paste the subscribe URL validate the port is there, everything looks good. I can publish and sync VM templates. Else if I create a content library copy/paste the subscribe URL and Remove the port from the url, the content library creates but doesn't show up as a subscriber and I'm unable to publish templates to it.
It's weird... I haven't found a single blog or anywhere in the powercli documentation that mentions port numbers... So I'm trying to figure out if it's something in my home lab environment, or if there has been a change in the way content libraries react to powercli recently.
Any guidance would be appreciated!