Create a Local Content Library on an Existing Datastore

You can use the vSphere Automation API to work with content library features. For example, you can create a local content library on a datastore.

The following sample script illustrates how you can get the content library service and retrieve information about existing content libraries. You can then combine working with both the vCenter Server API and the vSphere Automation API, and their corresponding objects, to create a content library on a specific datastore. Optionally, you can create an advanced function that lets you list all content libraries and their details.

Prerequisites

  • Verify that you are connected to a vCenter Server system.
  • Verify that you are connected to a vSphere Automation API server.

Procedure

  1. Get the service that works with local content libraries.
    $contentLibrary = Get-CisService com.vmware.content.local_library
  2. List the IDs of existing content libraries.
    $contentLibrary.list()
  3. Retrieve details of existing content libraries.
    $clID = $contentLibrary.list() | Select -first 1
    $contentLibrary.get($clID.Value)
  4. Get a datastore on which to create the content library.
    $datastoreID = (Get-Datastore | select -first 1).extensiondata.moref.value
  5. Create a local content library on the existing datastore.
    $createSpec = $contentLibrary.help.create.create_spec.Create() 
    $createSpec.name = "New Content Library 2" 
    $createSpec.description = "A new sample Content Library from PowerCLI"
    $createSpec.type = "LOCAL"
    $createSpec.publish_info.persist_json_enabled = $false
    $createSpec.publish_info.published = $false
    $createSpec.storage_backings = $contentLibrary.help.create.create_spec.storage_backings.Create() 
    $storageSpec = $contentLibrary.help.create.create_spec.storage_backings.Element.Create()
    $storageSpec.datastore_id = $datastoreID
    $storageSpec.type = "DATASTORE"
    $createSpec.storage_backings.Add($storageSpec) 
    $uniqueID = [guid]::NewGuid().tostring()
    $contentLibrary.create($uniqueID, $createSpec)
  6. Create a PowerShell advanced function that lists all content libraries and their details.
    Function Get-ContentLibrary ($name) {
        $contentLibrary = Get-CisService com.vmware.content.local_library
        $libraryIDs = $contentLibrary.list()
        Foreach ($library in $libraryIDs) {
            if ($name) {
                $contentLibrary.get($library.Value) | Where { $_.name -eq $Name } | Select Name, Type, Creation_Time, Last_Modified_Time, Storage_Backings
            } else {
                $contentLibrary.get($library.Value) | Select Name, Type, Creation_Time, Last_Modified_Time, Storage_Backings
            }
        }
    }