WilliamLam.com

  • About
    • About
    • Privacy
  • VMware Cloud Foundation
  • VKS
  • Homelab
    • Resources
    • Nested Virtualization
  • VMware Nostalgia
  • Apple
You are here: Home / Automation / vSphere Content Library PowerCLI community module

vSphere Content Library PowerCLI community module

07.06.2017 by William Lam // 27 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 - Retrieving vSAN File Share Network Permissions using vSAN API & PowerCLI
  • Quick Tip - Using PowerCLI to query VMware Tools Configuration at scale 
  • Managing vSAN internet connectivity configuration using the vSAN API
  • Quick Tip - Retrieving NSX segment information from vCenter Server

Categories // Automation, PowerCLI Tags // content library, PowerCLI

Comments

  1. *protectedBen 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. *protectedGanadmin 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
      • *protectedGanadmin says

        07/11/2017 at 5:09 pm

        Thanks ..

        Reply
  3. *protectedlowercasew 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. *protectedRon 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
    • *protectedanthonysomerset 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
      • *protectedanthonysomerset 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. *protectedDavid 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. *protectedAlphaMale 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. *protectedDaniel 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. *protectedBen 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. *protectedDavid 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. *protectedDavid 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. *protectedJason 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
    • *protectedJason 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. *protectedanimatco 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
      • *protectedanimatco says

        01/05/2021 at 8:25 pm

        I was afraid of that.

        Reply
  13. *protectedJorge Garcia says

    03/02/2021 at 2:50 pm

    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?

    Reply
    • William Lam says

      03/02/2021 at 7:57 pm

      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

      Reply
      • *protectedJorge Garcia says

        03/04/2021 at 3:47 pm

        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/

        Reply
  14. *protectedRolf says

    01/07/2022 at 5:58 am

    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

    Reply
    • *protectedRolf says

      01/07/2022 at 6:04 am

      Nevermind, my bad, was not using the Connect-CiSServer Command

      Reply
  15. *protectedMatt says

    03/10/2022 at 8:15 pm

    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!

    Reply

Thanks for the comment!Cancel reply

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

Search

Thank Author

Author

William is Distinguished Platform Engineering Architect in the VMware Cloud Foundation (VCF) Division at Broadcom. His primary focus is helping customers and partners build, run and operate a modern Private Cloud using the VMware Cloud Foundation (VCF) platform.

Connect

  • Bluesky
  • Email
  • GitHub
  • LinkedIn
  • Mastodon
  • Reddit
  • RSS
  • Twitter
  • Vimeo

Recent

  • Programmatically accessing the Broadcom Compatibility Guide (BCG) 05/06/2025
  • Quick Tip - Validating Broadcom Download Token  05/01/2025
  • Supported chipsets for the USB Network Native Driver for ESXi Fling 04/23/2025
  • vCenter Identity Federation with Authelia 04/16/2025
  • vCenter Server Identity Federation with Kanidm 04/10/2025

Advertisment

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

Copyright WilliamLam.com © 2025

 

Loading Comments...