Home Page

Rotating Credentials in VCF using PowerCLI SDK

In Introducing the Hidden Power of the PowerCLI SDK blog post, we introduced the PowerCLI SDK. In today’s post, we’ll show you how to use this SDK. You’ll learn to automate repetitive tasks within your VMware Cloud Foundation (VCF) environment. Rotating ESXI credentials is example for such repetitive task.

Rotating ESXi Credentials

One important step toward securing your datacenter is regularly rotating the credentials of your ESXi hosts. In environments with tens or even hundreds of hosts, doing this manually is both tedious and error-prone.

In this post, we’ll walk through a PowerCLI script. This script automates the rotation of ESXi host credentials across your datacenter. It takes just a few minutes.

Identifying Required Cmdlets

To automate this task, we need to identify which cmdlets are required. The PowerCLI SDK makes this easy with the Get-VcfSddcManagerOperation cmdlet.

We’re looking to:

  1. Retrieve the list of ESXi hosts
  2. Invoke the credential rotation API
  3. Monitor the task status

Let’s start by exploring the available APIs.

This gives us:

Next:

Relevant output:

And finally:

We see:

We now know the three cmdlets we’ll use:

Understanding the Rotation API

To call Invoke-VcfUpdateOrRotatePasswords, we need to construct a CredentialsUpdateSpec payload. Let’s consult the help documentation.

From the examples section, we see a pattern for constructing the payload using:

PowerCLI Script: Rotate ESXi Credentials

# Retrieve all hosts

# Create an array to store resource credentials

# Loop through each host

    # Create a base credential for each host

    # Create a resource credential entry

# Execute the rotation

Verifying the Task Status

Notice how we saved the task object into a variable.
Now you can use the following routine to monitor the task:

Example output:

Conclusion

With just a few lines of PowerShell, we’ve automated a process that otherwise takes hours. Credential rotation is just one of the many use cases the PowerCLI SDK enables. Stay tuned for more automation tips in future posts.