PowerCli Core Fling for MAC! How-to guide.

At VMworld 2016 in Las Vegas I attended the session INF8260 – Automated Deployment and Configuration of the vCenter Server Appliance which was presented by Alan Renouf and William Lam.  If you have not view this then I highly recommend you do so.  During the session Alan briefly touched on the ability to run PowerCLI on MAC and Linux which was very exciting news and without much delay the Fling was release 2 days ago:

https://labs.vmware.com/flings/powercli-core

PowerCLI Core uses Microsoft PowerShell Core and .Net Core to enable users of Linux, Mac and Docker to now use the same cmdlets which were previously only available on windows. Crazy right!? Well we have Microsoft to thank for making Powershell opensource and available for Linux and MAC

Below are my instructions on how I setup PowerCLI core to work on my MAC.

Step 1:   Download and Install .NET Core for Mac OS X from here

  • Install pre-requisites
    • In order to use .NET Core, we first need to install the latest version of OpenSSL. The easiest way to get this is from Homebrew.(Homebrew installs the stuff you need that Apple didn’t.)
  • Run the following command to install Homebrew:
    • # /usr/bin/ruby -e “$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)”
    •  After installing brew, run the following commands:
      • # brew update
      • # brew install openssl
      • # ln -s /usr/local/opt/openssl/lib/libcrypto.1.0.0.dylib /usr/local/lib/
      • # ln -s /usr/local/opt/openssl/lib/libssl.1.0.0.dylib /usr/local/lib/
  • Install .NET Core SDK
    • Download and install the official installer.
    • This installer will install the tools and put them on your PATH so you can run dotnet from the Console
    • Screen Shot 2016-10-18 at 8.57.10 PM.png
    • Screen Shot 2016-10-18 at 8.57.16 PM.png
    • Screen Shot 2016-10-18 at 8.57.22 PM.png
    • Screen Shot 2016-10-18 at 8.58.21 PM.png

Step 2:   Download and Install PowerShell for Mac OS X

  • https://github.com/PowerShell/PowerShell
  • Click “Clone or Download” -> Download Zip
  • Extract the Zip file
  • Installation doc can be found here:  docs/installation/linux.md#macos-1011
  • Download the PKG package powershell-6.0.0-alpha.11.pkg from the releases page onto the macOS machine.
    • Either double-click the file and follow the prompts, or install it from the terminal:  # sudo installer -pkg powershell-6.0.0-alpha.11.pkg -target /
  • You might get the error if your security settings have allowed apps only from App store and identified developers.
  • Screen Shot 2016-10-19 at 9.21.48 AM.png
  • Click OK
  • Go to MAC System Preferences
  • Security and Privacy
  • Unlock
  • Screen Shot 2016-10-19 at 9.25.59 AM.png
  • The powershell-6.0.0-alpha.11.pkg will show and you can click “Open Anyway”
  • Screen Shot 2016-10-19 at 9.26.23 AM.png
  • Screen Shot 2016-10-19 at 9.26.28 AM.png
  • Screen Shot 2016-10-19 at 9.26.41 AM.png

 

Step 3:  Perform the following steps to ensure you are using the latest OpenSSL and Curl

  • # brew install openssl
  • # brew install curl –with-openssl
  • Screen Shot 2016-10-19 at 9.38.59 AM.png
  • Since we installing 6.0.0-alpha.11, make sure you folder is updated correctly.
  • # sudo install_name_tool -change /usr/lib/libcurl.4.dylib /usr/local/opt/curl/lib/libcurl.4.dylib /usr/local/microsoft/powershell/6.0.0-alpha.11/System.Net.Http.Native.dylib
  • # sudo install_name_tool -add_rpath /usr/local/opt/openssl/lib /usr/local/microsoft/powershell/6.0.0-alpha.11/System.Security.Cryptography.Native.dylib

Step 4: Verify directoy /.local/share/powershell/Modules exists

  • If the directory does not exist, then create it by running the following commands:
    • # mkdir -p ~/.local/share/powershell/Modules

Step 5:  Extract the PowerCLI modules

  • Download the module zip file from:
  • Extract the PowerCLI modules into the directory you created above by running the following command:
  • # unzip PowerCLI.ViCore.4523941.zip -d ~/.local/share/powershell/Modules
  • # unzip PowerCLI.Vds.4523941.zip -d ~/.local/share/powershell/Modules

 

Step 6: Launch PowerShell

  • Open terminal
  • Start Powershell in the terminal by running the following command:
    • # powershell
  • Import the PowerCLI Modules into your PowerShell Session:
    • PS > Get-Module -ListAvailable PowerCLI* | Import-Module
  • Connect to your vCenter Server using Connect-VIServer
    • PS> Connect-VIServer -Server 10.20.30.10 -User administrator@vSphere.local -Password Vmware!
  • I received  a WARNING when trying to connect:  “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”

    • Best is to avoid the warning all together, with an official certificate or a self-signed certificate or run the following command:
      • PS >  Set-PowerCLIConfiguration -InvalidCertificateAction Ignore
    • Screen Shot 2016-10-19 at 10.00.49 AM.png
  • Now you should connect successfully to your vCenter server from MAC!

 

Links:

https://labs.vmware.com/flings/powercli-core

https://github.com/PowerShell/PowerShell/

https://blogs.vmware.com/PowerCLI/2016/10/powercli-core-fling-available.html

 

Powercli – Create alarm actions triggers for existing alarm definitions

Here is a script i created to add the following actions for triggers on alarms:
– Send a notification email from specified alarm state and email address
– Send a notification trap from specified alarm state and email address

I recommend just testing with a single Alarm definition in CSV file to make sure it works correctly for you. Only run this once otherwise you will create duplicate triggers.

Connect-VIServer
$alarmlist = Import-CSV C:\vcenter-alarm.csv

foreach ($item in $alarmlist) {

$alarmname = $item.alarmname
$alarmstartstatus = $item.alarmstartstatus
$alarmendstatus = $item.alarmendstatus
$alarmemail = $item.alarmemail

Get-AlarmDefinition -Name $alarmname | New-AlarmAction -Email -To $alarmemail | New-AlarmActionTrigger -StartStatus $alarmstartstatus -EndStatus $alarmendstatus
Get-AlarmDefinition -Name $alarmname | New-AlarmAction -Snmp | New-AlarmActionTrigger -StartStatus $alarmstartstatus -EndStatus $alarmendstatus
}

CSV file has the following columns: (no quotes necessary for text)

alarmname alarmstartstatus alarmendstatus alarmemail

Disclaimer:
Please use this script at your own risk and test it out in your test lab first before using it in production.

PowerCLI – Identify LUN names for each datastore

Nice quick and easy script with Powercli for identify the LUN names for each datastore.
This is useful for debugging the log files where only the LUN name is specified.

I am a big fan of Powergui where i run and create all my scripts in!
http://powergui.org/index.jspa
http://communities.vmware.com/community/vmtn/automationtools/powercli

Script:
(replace localhost with your own vcenter server)

Connect-VIServer localhost
Get-VMHost | Get-Datastore | where {$_.Type -eq “VMFS”} |
Select Name,@{N=”LUN”;E={(Get-ScsiLun -Datastore $_ | Select -First 1).CanonicalName}}

Disclaimer:
Please use this script at your own risk and test it out in your test lab first before using it in production.