bright ideas

PowerCLI - Automation's gateway drug

IT Infrastructure, SDDC

|

15 Jun 2016

Automation.jpg

I recall my days as an infrastructure administrator, and I won’t be alone in this, where I would spend a fair bit of time implementing changes manually. The act of selecting the option, entering in a value, and hitting “next”, provided a level of control as well as exposing options and variations, such that I learned a lot about what was going on and how to configure things.

Then came wave upon wave of messaging around automation, how automation will save you time/enable greater capabilities and rescue cats in trees. As an admin, I was sceptical – not of process automation, that made sense. Rather, being risk averse, the thought of one wrong setting in an automation workflow applying en masse filled me with dread. After all, who would have to clean up a wide-scale mess?

The thing is, you can’t run before you can walk. If you filter out the reality from the dream, automation can save you a great deal of time and, if you had access to the right equipment, could probably rescue at least two out of five cats.

Automation can be daunting for admins with no background in programming, but you’re not really expected to dive into complex scripting with error handling and logic right off the bat. The trick is starting small, with repeatable tasks you know are benign and build from there. When performing these tasks manually, you’ll be really good at reading and following instructions the first few times. After that, you might think you recall everything and complete the task yourself. Then, a day or so later you’ll likely realise you missed something. We all make mistakes, and this is where simple automation can really help.

Think of how long it takes to add 8+ hosts to a cluster, one-by-one, configuring all the variables and validating these. I can do it pretty quickly, but not as fast as a script can.

In the world of vSphere, PowerCLI (PowerShell) is the best tool to use as you begin this journey.

OK, how do I get started?

Well, as always Google is your friend. There’s no reason to try and re-invent the wheel, so a good way to get started is to deconstruct someone else’s script so you see what’s going on. Some great resources are provided by Alan Renouf and Luc Dekens. Also very handy is the PowerCLI commandlet library provided by VMware.

You’ll also need a good editor. There are some great ones out there - Notepad++ is a common favourite, although I’ve found some quirks with it. If you’re a Mac user, then Sublime Text is a great option. Lately, I’ve relied on the free Windows Powershell ISE as my editor-of-choice.

Lastly, you’ll need a sandpit. Because you’re not going to experiment with scripting on your Production infrastructure, right? Right? If you don’t have a budget for a lab, then a nested ESXi virtual lab is a great option for testing against. Yes, you can even automate the creation of a virtual Lab by leveraging AutoLab.

Great... so now what?

Start small. What’s a common task you might do that is quite simple, has many steps that might result in a setting being missed but also one that won’t cause widespread destruction.

I’ll use the task of adding an ESXi host to a cluster, as this is low risk yet involves a number of configuration items for your environment that might be missed.

The first task will be knowing what’s required to add a host to a cluster. You’ll need the host name (which hopefully will be in DNS by now), the root password and somewhere to put it. If you search through the PowerCLI library, you’ll come across the appropriately named “Add-VMHost” commandlet. This is the one to use.

Add-VMHost -Server $myServer -Name MyVMHost1 -Location MyDatacenter1 -User MyUsername1 -Password MyPassword1  

If you plan on re-using this process for more than one host, I recommend setting variables so that you can easily swap information in and out of your script without accidentally wrecking the syntax. I’d also recommend against storing any credentials in plain-text for obvious reasons.

Defining a variable is quite easy, so let’s start with what we know we’ll always need – the host name.

$myServer = "MyVMHost1"

Add-VMHost $myServer -location MyDatacenter1

OK, that part is done. But what about those credentials? The Add-VMhost example page shows one way of doing this, by parsing a file with your credentials in it. There’s another way, which is to prompt you for the credentials with the “Get-Credential” command. Thinking ahead, we’d better set a variable for this.

$myServer = "MyVMHost1"

$esxcred = Get-Credential

Add-VMHost $myServer -location MyDatacenter1 -Credential $esxcred

We’re not done yet though. In most environments, there’s usually no requirement to configure each host with a trusted SSL certificate. This causes a prompt for the admin to accept the self-generated certificate as valid. Of course, we can get past this in the script.

$myServer = "MyVMHost1"

$esxcred = Get-Credential

Add-VMHost $myServer -location MyDatacenter1 -Credential $esxcred -force:$true

Now, recall that the reason we might set variables is so that we can execute these types of operations against more than one object. We can tell the process to execute the next command immediately without waiting for the previous one to complete with the “RunAsync” parameter. This is handy when adding lots of hosts.

$myServer = "MyVMHost1"

$esxcred = Get-Credential

Add-VMHost $myServer -location MyDatacenter1 -Credential $esxcred -RunAsync -force:$true

But how are we going to get a big list of hosts in there? You can tackle this a few ways. If you’re comfortable calling a .csv file full of details, then there are ways to do that. As this is an introduction we’ll keep things simple and define multiple variables within the script.

We’ll also need to look at using a “foreach” loop so we can execute the same command against a list of objects.

$myServer1 = "MyVMHost1"

$myServer2 = "MyVMHost2"

$myServer3 = "MyVMHost3"

$myHosts = $myServer1, $myServer2, $myServer3

$esxcred = Get-Credential

foreach ($host in $myHosts) {

Add-VMHost $host -location MyDatacenter1 -Credential $esxcred -RunAsync -force:$true

}

I’ll provide a bit extra in the below example. It’s not complex, so you should be able to figure out what I’ve done. You’ll also see I make a lot of notes. As you build out your scripts, these will be of huge importance so that you and others can follow the logic.

 

#--------------------------------------------------------------------#

#Set and edit variables here:

#--------------------------------------------------------------------#

$myServer1 = "MyVMHost1"

$myServer2 = "MyVMHost2"

$myServer3 = "MyVMHost3"

$myHosts = $myServer1, $myServer2, $myServer3

#--------------------------------------------------------------------#

#Add new hosts to the cluster for initial configuration

#--------------------------------------------------------------------#

$confirmAddHost = Read-Host "About to add hosts - continue? Y/N"

if ($confirmAddHost -eq "Y") {

#Prompt for ESXi root credentials

$esxcred = Get-Credential

foreach ($host in $myHosts) {

Write-Host "Adding $host to MyDatacenter1" -ForegroundColor Green

Add-VMHost $host -location MyDatacenter1 -Credential $esxcred -RunAsync -force:$true

}

}

else {Write-Host "Skipped ..."}

#--------------------------------------------------------------------#

#Put new hosts into Maintenance Mode

#--------------------------------------------------------------------#

$confirmMaintMode = Read-Host "About to add hosts into Maintenance mode - continue? Y/N"

if ($confirmMaintMode -eq "Y") {

foreach ($host in $myHosts) {

Write-Host "Putting $host into Maintenance Mode" -ForegroundColor Green

Set-VMHost $host -State "Maintenance" -RunAsync -confirm:$false

}

}

else {Write-Host "Skipped ..."}  

 

Why not take this and build on it? What other tasks will you normally do when adding hosts to a cluster? Will you configure DNS? NTP? Power Profile?

Nick Bowie

Author: Nick Bowie

Nick specialises in cloud infrastructure and the Software Defined Data Centre, helping to facilitate implementation of first class, architecturally driven infrastructure solutions.

15 June 2016 / 0 Comments