You can use the
vSphere
Automation SDK 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 SDK, as well as their corresponding objects, to create a new
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.
Procedure
-
Connect to a
vSphere
Automation SDK server.
Connect-CisServer -Server cis3.example.com -User 'MyAdministratorUser' -Password 'MyPassword'
-
Get the service that
works with local content libraries.
$contentLibrary = Get-CisService com.vmware.content.local_library
-
List the IDs of
existing content libraries.
-
Retrieve details of
existing content libraries.
$clID = $contentLibrary.list() | Select -first 1
$contentLibrary.get($clID.Value)
-
Get a datastore on
which to create the content library.
$datastoreID = (Get-Datastore | select -first 1).extensiondata.moref.value
-
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)
-
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
}
}
}