Getting Started with PowerCLI for Linux (PowerCLI Core)

powerclicoreBased on Microsoft PowerShell Core which enables users to use PowerShell on Linux, Mac and Docker, VMware has started to make PowerCLI compatible with the new PowerShell. This allows to run scripts that were previously only available for Windows on Linux or Mac based systems.

In this post I am going to give a quick startup guide to the installation and configuration of PowerCLI Core.

This guide is based on an Ubuntu 14.04 LTS Linux box which I am using to manage my vSphere Lab.

PowerCLI Core Installation

  1. Install required packages.
    # apt-get install ca-certificates curl libunwind8 libicu52 unzip wget libcurl4-openssl-dev
  2. Download the latest version of PowerShell. You can get the latest version for your OS here.
    copy-link-address-powershell

    # wget https://github.com/PowerShell/PowerShell/releases/download/v6.0.0-alpha.11/powershell_6.0.0-alpha.11-1ubuntu1.14.04.1_amd64.deb
  3. Install the PowerShell package. The apt-get install -f command will install missing dependencies.
    # dpkg -i powershell_6.0.0-alpha.11-1ubuntu1.14.04.1_amd64.deb
    # apt-get install -f
  4. Create the PowerShell module directory.
    # mkdir -p ~/.local/share/powershell/Modules
  5. Download PowerCLI Core and extract the modules to the default module directory.
    # wget https://download3.vmware.com/software/vmw-tools/powerclicore/PowerCLI_Core.zip
    # unzip PowerCLI_Core.zip && unzip 'PowerCLI.*.zip' -d ~/.local/share/powershell/Modules
  6. You should now be able to start PowerShell and load VMware PowerCLI Core modules.
    # powershell
    PS /> Get-Module -ListAvailable PowerCLI* | Import-Module
  7. Connect to a vCenter Server
    PS /> Connect-VIServer -Server vcsa.virten.lab -User administrator@vsphere.local -Password vmware123

Fix Self-Signed Certificates Issue

When the vCenter Server uses an untrusted self-signed certificate, you will see the following warning and the connection will fail:

WARNING: Invalid server certificate. Use Set-PowerCLIConfiguration to set the value for the InvalidCertificateAction option to Prompt if you'd like to connect once or to add a permanent exception for this server.
Connect-VIServer : 23.10.16 19:53:11 Connect-VIServer An error occurred while sending the request.

Starting with vSphere 6.0 you can easily download root certificates from the vCenter Server. Include the certificates to /etc/ca-certificates.conf and do a dpkg-reconfigure ca-certificates.

# wget https://vcsa.virten.lab/certs/download --no-check-certificate -O cacert.zip
# unzip cacert.zip
# mkdir /usr/share/ca-certificates/extra
# cp certs/*.0 /usr/share/ca-certificates/extra/vcsa.virten.lab.crt
# echo "extra/vcsa.virten.lab.crt" >> /etc/ca-certificates.conf
# dpkg-reconfigure ca-certificates

If this does not resolve the issue, you can set PowerShell to ignore certificate proglems.

PS /> Set-PowerCLIConfiguration -InvalidCertificateAction Ignore

Load PowerCLI Modules on PowerShell Startup

A profile script makes it possible to load PowerCLI Modules on startup. I had a small issue with the configuration because Linux is case-sensitive while Microsofts PowerShell isn't. The default profile script is stored in the $profile variable:

PS /> $profile
/root/.config/powershell/Microsoft.PowerShell_profile.ps1

However, the directory that comes with the installer is: /root/.config/PowerShell/
As a workaround, I've created a symlink:

# ln -s /root/.config/PowerShell/ /root/.config/powershell

Add the command to import modules to the profile script.

# echo "Get-Module -ListAvailable PowerCLI* | Import-Module" >> /root/.config/PowerShell/Microsoft.PowerShell_profile.ps1

PowerCLI commands are now automatically loaded when you start PowerShell.

# powershell
PowerShell
Copyright (C) 2016 Microsoft Corporation. All rights reserved.

Loading personal and system profiles took 851ms.
PS />
  1. Hot VMware Topics - October - pingback on November 9, 2016 at 4:48 pm

Leave a Comment

NOTE - You can use these HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

Trackbacks and Pingbacks: