PowerCLI... on Linux?

Posted by Brad on Fri 17 August 2018

I spend a lot of time in the vcenter gui, and it got me thinking that there might be a more efficient way. I'm also always paranoid I'll accidentally mess with the wrong VM. Then I remembered on the Windows side there was the powercli module, and as it turns out its now available on linux on powershell... What madness is this? I believe this is just another step in the conspiracy theory that Windows will eventually be just another linux distro.

Obviously with everything there is a learning curve so I've decided to challenge myself to not use the vcenter gui in my homelab without trying the task via command-line first for a solid month. Here is my experience over the first few days.

Getting Started

First we need powershell, even crazier MS actually provides a yum repo like they seriously want people to use powershell on linux.

Then we just install PowerCLI from the Powershell gallery thing with Install-Module -Name VMware.PowerCLI -Scope CurrentUser. Well, technically speaking you have to run pwsh first.

We're almost ready, but there's one more thing we need to do. Disable certificate checking, since we all know you aren't using a valid cert on your vcenter server.

Set-PowerCLIConfiguration -InvalidCertificateAction ignore

Now, you have all the PowerCLI awesomeness you could ever dream of.

Kicking the Tires

The first thing I found myself doing was adding a disk to a few VMs, which is probably the most common task I do outside of deploying a new VM. It's fairly easy with PowerCLI and it gets us off to a good start.

Add a new disk: New-HardDisk -VM $VM -StorageFormat Thin -CapacityGB 100 -Datastore myDatastore -Confirm

Next up I was curious about some resource pool info. This is also where I ran into some powershell fun. Initially, I wanted to run get-vm and pipe it to something like where -Property ResourcePool -eq infra but for some reason that wasn't working, so I flipped it around and got the resource pool and piped that to get-vm.

Get VMs and their resource pools: get-vm | Format-Table -Property Name,ResourcePool -AutoSize

get VMs in a resoure pool Get-ResourcePool -Name infra | get-vm

The real trouble, or rather inconvenience came when deploying a new VM. The "recommended" way is to set a bunch of variables and reference those in the new-vm command, but that's rather clunky. And to top it off, I get some vague error about parameters when I try to set the portgroup and use a template. I'm sure I could edit the VM after its created but that shouldn't really be necessary.

From the examples:

$myResourcePool = Get-ResourcePool -Name MyResourcePool
$myTemplate = Get-Template -Name WindowsTemplate
$mySpecification = Get-OSCustomizationSpec -Name WindowsSpec
New-VM -Name MyVM2 -Template $myTemplate -ResourcePool $myResourcePool -OSCustomizationSpec $mySpecification

Creates a virtual machine from the specified template and applies the specified customization specification.

First Impression

I don't see this completely replacing the vcenter gui, but I do think it will come in handy for things like information gathering and routine tasks like taking a snapshot. As far as managing/creating VMs I need to dig deeper into terraform and maybe revisit ansible's vmware modules as well. I also haven't dug into the normal powershell stuff on linux yet, but so far it looks like it might make linux a bit more approachable to windows folks who only have a couple linux boxes to manage.