Based 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.
- PowerShell for every system (GitHub)
- PowerCLI Core (Fling)
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
- Install required packages.
# apt-get install ca-certificates curl libunwind8 libicu52 unzip wget libcurl4-openssl-dev
- Download the latest version of PowerShell. You can get the latest version for your OS here.
# 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
- 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
- Create the PowerShell module directory.
# mkdir -p ~/.local/share/powershell/Modules
- 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
- You should now be able to start PowerShell and load VMware PowerCLI Core modules.
# powershell PS /> Get-Module -ListAvailable PowerCLI* | Import-Module
- 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 />
Pingback: Hot VMware Topics - October
Is there an equivalent to
# 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
for the VCSA 6.5 on Photon OS?
Thanks
Mike van
Sure, there are only minor changes to the format in vSphere 6.5:
Downloading is now "download.zip" instead of "download" and the bundle contains subfolders:
# wget https://vcsa.virten.lab/certs/download.zip --no-check-certificate
# unzip download.zip
# mkdir /usr/share/ca-certificates/extra
# cp certs/win/*.crt /usr/share/ca-certificates/extra/vcsa.virten.lab.crt
# echo "extra/vcsa.virten.lab.crt" >> /etc/ca-certificates.conf
# dpkg-reconfigure ca-certificates
Thanks for the quick response.
I can do everything except the dpkg-reconfigure.
It doesn't seem to be installed, apt-get is also not installed. how do I get dpkg-reconfigure so that I can complete the steps.
Thanks again
mike van
The guide uses Ubuntu 14.04, both commands are valid for Debian based Linux only. What distribution do you have?
I'm trying to do this on the VCSA 6.5 which is Photon 1.0.
Thanks for nice tutorial
I try installing powercli on ubuntu 16.04.2 LTS with these instructions but I'm stuck at step 6:
Get-Module -ListAvailable PowerCLI* | Import-Module
Import-Module : The system cannot find the file specified.
(Exception from HRESULT: 0x80070002)
here is the output of "Get-Module -ListAvailable" :
PS /root> Get-Module -ListAvailable
Directory: /root/.local/share/powershell/Modules
ModuleType Version Name ExportedCommands
---------- ------- ---- ----------------
Binary 6.0.0.0 PowerCLI.Cis
Binary 1.21 PowerCLI.Vds
Binary 1.21 PowerCLI.ViCore HookGetViewAutoCompleter
Directory: /opt/microsoft/powershell/6.0.0-beta.3/Modules
ModuleType Version Name ExportedCommands
---------- ------- ---- ----------------
Manifest 1.1.0.0 Microsoft.PowerShell.Archive {Compress-Archive, Expand-Archive}
Manifest 3.0.0.0 Microsoft.PowerShell.Host {Start-Transcript, Stop-Transcript}
Manifest 3.1.0.0 Microsoft.PowerShell.Management {Add-Content, Clear-Content, Clear-ItemProperty, Join-Path...}
Manifest 3.0.0.0 Microsoft.PowerShell.Security {Get-Credential, Get-ExecutionPolicy, Set-ExecutionPolicy, ConvertFrom-SecureString...}
Manifest 3.1.0.0 Microsoft.PowerShell.Utility {Format-List, Format-Custom, Format-Table, Format-Wide...}
Script 1.1.4.0 PackageManagement {Find-Package, Get-Package, Get-PackageProvider, Get-PackageSource...}
Script 3.3.9 Pester {Describe, Context, It, Should...}
Binary 6.0.0.0 PowerCLI.Cis
Binary 1.21 PowerCLI.Vds
Binary 1.21 PowerCLI.ViCore HookGetViewAutoCompleter
Script 1.1.3.1 PowerShellGet {Install-Module, Find-Module, Save-Module, Update-Module...}
Script 0.0 PSDesiredStateConfiguration {ThrowError, Get-PSMetaConfigDocumentInstVersionInfo, New-DscChecksum, ValidateNodeResourceSource...}
Script 1.2 PSReadLine {Get-PSReadlineKeyHandler, Set-PSReadlineKeyHandler, Remove-PSReadlineKeyHandler, Get-PSReadlineOption...}
I faced with same problem. It is a bug of a powercli. WA - use alpha version instead beta (18th, for example).
Sorry, not powercli, it is bug of powershell. And you should use older version of powershell :)
Just saying PowerCLI on Core (Windows) works very nicely as well.