Welcome to the new year from the PowerCLI team! The first blog of the year is to look at some of the improvements to the way we manage one of the hidden gems of vSphere, Content Library. Little known fact, Content Library was one of the very first services in vCenter which was offered through a REST API as part of the vSphere 6.0 release! Since then, there’s been numerous updates and improvements to the Content Library. These updates have been discussed in the following blog by my teammate Nigel Hickey: The Evolution of Content Library
Late last year, we released PowerCLI 11.5. This new version included quite a few updates and number of new cmdlets to manage things like vCenter Alarms, VMware Cloud on AWS, and Content Library.
The new cmdlets for Content Library management are as follows:
- New-ContentLibraryItem
- Set-ContentLibraryItem
- Remove-ContentLibraryItem
- Export-ContentLibraryItem
- New-ContentLibrary
- Set-ContentLibrary
- Get-ContentLibrary
- Remove-ContentLibrary
As part of a tandem blog post with Nigel, we’re going to walk through interacting with a Content Library. Nigel will handle the tasks from the vSphere Client side over on the vSphere blog post “Creating and Using Content Library” and I’ll be showing how to do those same tasks with the new PowerCLI cmdlets here!
Creating a Local Content Library
First things first, in order to manage a Content Library, we need to create one. Luckily, one of the new cmdlets turns this process from roughly a 10-line process into, essentially, a one-liner!
The key pieces of information we need to make note of prior to creating a new local Content Library by running the New-ContentLibrary cmdlet:
- Name
- Description (optional, but recommended)
- Whether publishing should be enabled
- Datastore location
For my environment, I can use the following code to create a new local Content Library:
1 2 3 4 5 6 7 |
$contentLibraryInput = @{ Name = "PowerCLI Publisher" Description = "PowerCLI Publisher Library" Published = $true Datastore = Get-Datastore -Name "Datastore Name" } New-ContentLibrary @contentLibraryInput |
Adding Items to Content Library
A Content Library can be used to store and share a number of things. From VMs to templates, scripts to certificates, we can import and make use of these items from any vCenter which has access. The first item we’ll be adding to our new Content Library is going to be a PowerShell script, which is small and simple to import.
For this task, we will want to identify a script to import and get familiar with the new cmdlet, New-ContentLibraryItem. This cmdlet has a couple parameters we will need to populate:
- ContentLibrary: the Content Library we are looking to interact with
- Files: the file/s to be imported to the Content Library
- Name: the name of the item displayed in the Content Library
I’ll be referencing a script from one of the last PowerCLI blogs, Get-TriggeredAlarms.ps1, which I have stored in a local git repository.
We can use the following code to create a new Content Library item by importing our PowerCLI script:
1 2 3 4 5 6 |
$contentLibraryItem = @{ ContentLibrary = Get-ContentLibrary -Name "PowerCLI Publisher" Files = Get-Item -Path "./Documents/GitHub/kmr-PowerCLI-Example-Scripts/Scripts/Get-TriggeredAlarm.ps1" Name = "PowerCLI - Get Triggered Alarms" } New-ContentLibraryItem @contentLibraryItem |
Exporting Items from Content Library
We have created a Content Library and imported our first item. The next step would be to use that item. There’s no native way to run, or even modify, our imported script, so we will want to export the script so we can run it from our local PowerShell session. In order to do this, we will use another new cmdlet named Export-ContentLibraryItem
There are two main parameters we will need to use for this task:
- ContentLibraryItem: the item we’re looking to export
- Destination: this is the location of where the item should be downloaded and stored to
We can use the following code to export our script from the Content Library:
1 2 3 4 5 |
$contentLibraryItem = @{ ContentLibraryItem = Get-ContentLibraryItem -Name "PowerCLI - Get Triggered Alarms" Destination = "./Downloads" } Export-ContentLibraryItem @contentLibraryItem |
Remove Item from Content Library
For our last task, we’re going to complete the lifecycle of a Content Library item by removing our PowerCLI script. In order to accomplish this task, PowerCLI has another new cmdlet which can simplify this task to a single line by using Remove-ContentLibraryItem
There’s only one parameter we need to be concerned with, and that is the following:
- ContentLibraryItem: this is the item we’re looking to remove
We can use the following code to remove our script from the Content Library:
1 |
Get-ContentLibraryItem -Name "PowerCLI - Get Triggered Alarms" | Remove-ContentLibraryItem |
Summary</2h>
Content Library is one of those vSphere features with so much untapped potential. This blog post took a look at how the release of PowerCLI 11.5 can streamline the management of a Content Library and the entire lifecycle of those items it contains.
Let us know in the comments how you’re using PowerCLI to automate the Content Library!
More information about PowerCLI 11.5’s release: New Release – PowerCLI 11.5.0
More information about performing these tasks in the vSphere Client: Creating and Using Content Library