• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

virtuallyGhetto

  • About
  • Privacy
  • VMware Cloud
  • Home Lab
  • Nested Virtualization
  • Automation
    • VMware Kickstart
    • VMware API/SDK/CLI
    • VMware vMA/VIMA
    • VMware OVF / OVFTOOL
  • Apple Mac
  • VCSA
  • VSAN
You are here: Home / Automation / vSphere Content Library PowerCLI community module

vSphere Content Library PowerCLI community module

07/06/2017 by William Lam 21 Comments

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

More from my site

  • Converting a vSphere Content Library from Subscriber to a Publisher?
  • Quick Tip – How to retrieve deleted VM history?
  • Which VM was this vSphere VM cloned from?
  • Automating HAProxy VM deployment with 3-NIC configuration using PowerCLI
  • Using PowerCLI and vSphere Tags to create/migrate HCX Mobility Groups to VMware Cloud SDDC
Share this...
  • Twitter
  • Facebook
  • Linkedin
  • Reddit
  • Pinterest

Filed Under: Automation, PowerCLI Tagged With: content library, PowerCLI

Reader Interactions

Comments

  1. Ben Liebowitz says

    07/06/2017 at 10:34 am

    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

    Reply
    • William Lam says

      07/06/2017 at 7:17 pm

      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

      Reply
  2. Ganadmin says

    07/11/2017 at 11:08 am

    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 ..

    Reply
    • William Lam says

      07/11/2017 at 4:57 pm

      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

      Reply
      • Ganadmin says

        07/11/2017 at 5:09 pm

        Thanks ..

        Reply
  3. lowercasew says

    07/20/2017 at 1:46 pm

    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

    Reply
  4. Ron Brown says

    09/29/2017 at 12:41 pm

    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?

    Reply
    • anthonysomerset says

      03/05/2018 at 10:11 pm

      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

      Reply
      • anthonysomerset says

        03/05/2018 at 10:23 pm

        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

        Reply
  5. David says

    10/17/2017 at 11:52 am

    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

    Reply
  6. AlphaMale says

    11/29/2017 at 6:16 pm

    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?

    Reply
    • William Lam says

      12/01/2017 at 6:54 am

      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 ๐Ÿ™‚

      Reply
  7. Daniel Petcher says

    02/11/2019 at 12:29 pm

    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.)

    Reply
  8. Ben Conrad says

    03/28/2019 at 9:28 am

    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

    Reply
  9. David N says

    04/03/2019 at 4:14 am

    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

    Reply
  10. David N says

    04/04/2019 at 8:06 am

    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….

    Reply
  11. Jason says

    04/30/2019 at 1:42 pm

    Is there a new-SubscibedContentLibrary command in the works? What can I use in its stead?

    Reply
    • Jason says

      05/06/2019 at 7:03 am

      Thank you so much for the New-SubscribedContentLibrary. I’ll be testing it soon. Thanks

      Reply
  12. animatco says

    01/05/2021 at 2:40 pm

    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.

    Reply
    • lamw says

      01/05/2021 at 7:38 pm

      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)

      Reply
      • animatco says

        01/05/2021 at 8:25 pm

        I was afraid of that.

        Reply

Thanks for the comment! Cancel reply

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

Primary Sidebar

Author

William Lam is a Senior Staff Solution Architect working in the VMware Cloud team within the Cloud Services Business Unit (CSBU) at VMware. He focuses on Automation, Integration and Operation for the VMware Cloud Software Defined Datacenters (SDDC)

  • Email
  • GitHub
  • LinkedIn
  • RSS
  • Twitter
  • Vimeo

Sponsors

Privacy & Cookies: This site uses cookies. By continuing to use this website, you agree to their use.
To find out more, including how to control cookies, see here: Cookie Policy