Bootstrapping VM’s Using VMWare PowerCLI
Lately I’ve been spinning up more Linux VM’s (mostly Debian/Ubuntu) in my VMWare ESXI home lab. While it’s simple enough to launch a new VM, I found it tedious to configure certain settings after launch. Most of these were the same for each VM – update/install packages, configure SSH, create directories, open firewall ports, etc. The obvious answer to this is to write a bash script and be done with it – which I did. But without SSH first being enabled, I wasn’t sure how to get that bash script on the guest OS without manually creating it using the VMWare guest console and typing in the script contents. Enter PowerCLI.
PowerCLI is a PowerShell module for managing VM’s in a VMWare vSphere or ESXI environment. It’s chocked full of commands that will make your life much easier if you are managing more than a handful of VM’s. You can install it by running “Install-Module VMware.PowerCLI” in an administrative PowerShell console. The two commands I used to accomplish my goal here were “Copy-VMGuestFile” and “Invoke-VMScript”. The first one I used to copy my bash script from my local computer (a Windows 10 machine) to the VM guest OS filesystem (Ubuntu Linux). I then used “Invoke-VMScript” to execute that script on the guest OS. Throw this and some supporting variables into a PowerShell script, and you’re a double-click away from performing base-configuration on your new VM. Here’s my PowerShell code on GitHub so you can see exactly what I did.
Below is the bash script I copied over to the VM. You could put anything you want in here, but just for reference:
#!/usr/bin/env bash
apt-get -y update
apt-get -y install openssh-server
ufw allow 22
mkdir ~/.ssh
touch ~/.ssh/authorized_keys
Now, this is a pretty simple script, and there’s probably a lot more I could and should do to improve error-checking and make this modular for different VM configurations or for running on different guest OS’s. There may be other ways of accomplishing this as well, but hopefully this will give you what you need to tailor this solution to your requirements. Happy scripting!