Executing Powershell and/or VMWare PowerCLI directly on your Linux Server

Author: @unic
Original: https://monitoring-portal.org/t/executing-powershell-and-or-vmware-powercli-directly-on-your-linux-server/2905
Revision: v0.1

Tested with:
Debian Stretch/Jessie

Introduction

As more and more Modules are coming out for Powershell I thought it would be nice to execute Powershell checks directly trough the Icinga Server instead of using a Windows machine to execute the checks. For example, I’am using the VMWare PowerCLI module to execute VM Snapshots checks directly from my Satellite without the need of an intermediate Windows Checker node. This could be used as an alternative to the VMWare Perl SDK.
Installation process is very easy, but at the moment not every command that runs on Windows is already included in the Powershell Core and sometimes a script needs to adapt to the Linux environment.

Requirements

Microsoft Powershell Core on Your Linux Machine ( Please wait until Your goose bumps are gone again )
PowerCLI Module (optional)

Installation

Get Powershell Core from Github for Your Linux distribution:
https://github.com/powershell/powershell

There are premade packages and instructions to add the repository for many Linux distributions

Example: Repository Method for Debian

# Install system components
apt-get update

#optional required packages
apt-get install gnupg apt-transport-https curl
 
# Import the public repository GPG keys
curl https://packages.microsoft.com/keys/microsoft.asc | sudo apt-key add -
 
#  Add Repository
echo "deb [arch=amd64] https://packages.microsoft.com/repos/microsoft-debian-stretch-prod stretch main" > /etc/apt/sources.list.d/microsoft.list

# for Jessie use
#echo "deb [arch=amd64] https://packages.microsoft.com/repos/microsoft-debian-jessie-prod jessie main" > /etc/apt/sources.list.d/microsoft.list
 
# Update the list of products
apt-get update
 
# Install PowerShell
apt-get install powershell
 
# Start PowerShell
pwsh

#Install Modules (VMWare PowerCLI example)
Install-Module -Name VMware.PowerCLI

Configuration

To Configure the PowerCLI Module the Set-PowerCLI Commandlet is used. In this Example the Customer Experience Program is disabled and invalid certificates for connections are accepted.

pwsh
#Configure the Powercli Module (disable 'Customer Expirience Program' and accept invalid certificates)
Set-PowerCLIConfiguration -Scope AllUsers -ParticipateInCEIP $false
Set-PowerCLIConfiguration -InvalidCertificateAction ignore -Scope AllUsers

Hints for editing and creating Powershell scripts

It’s always a good idea to let the script check on which platform it’s running to change some things at runtime, also some environment variables are needed, because Icinga will not load the user profile when it executes checks (more details here).

#!/usr/bin/pwsh
$platform = $PSVersionTable.Platform #get Platform details
if ($platform -match "Unix" ) {
  #change env variables, otherwise f.e. Powercli will crash on module import
  $env:PWD="/var/lib/nagios"
  $env:HOME="/var/lib/nagios"
  # Do some other fancy stuff
}

Example Scripts

check_dell_warranty.ps1

As an example a Dell Warranty Check is attached. To use this script an API Key from Dell is required to get real time warranty information for dell components. See Script for more information.
Example Output of the Script:

OK: Warranty expires in 679 days - 26-04-2020
Model: PowerEdge R730 - ServiceTag: 11122A/111EXPRESS-CODE222 - Support: ‘ProSupport with Next >Business Day Service’

send_telegram_message.ps1

Telegram uses a HTTP API:
https://core.telegram.org/bots/api

A very simple Powershell example:

$api = '9999999999:jgklejig43jgreljg43FDEGEGregERGE'

#Get ChatID
#$restResponse = Invoke-RestMethod -Uri ('https://api.telegram.org/bot{0}/getUpdates' -f $api)
#$restResponse.result.message.chat.id

$chatId = '444444'

function TelegramSend ($msg) {
  $restResponse = Invoke-RestMethod -Uri ('https://api.telegram.org/bot{0}/sendMessage?chat_id={1}&text={2}&parse_mode=Markdown' -f $api, $chatId, $msg)
    $restResponse.ok # True
}
TelegramSend "Hello"

Conclusion

It’s quite simple to execute Powershell Scripts on Linux, but most scripts do need some small changes to work perfectly.

FAQ

Were can i get the Powershell Snapshot Check ?
Please send @Unix a private Message and I’ll send You a copy. The Script is still heavy Windows based and all help options not fitting to linux. I will release it, when its cleaned up.

Known Issues

On Debian Jessie one Net Core package (can’t remember which one) had debug output enabled by default, so some Powershell checks will retun some lines of debug code. This can only be fixed, by updating to stretch or updated the package from some external source.

Attachments

check_dell_warranty.ps1.txt (17.6 KB)


I have uploaded the check_vmware_snapshot script to Github. It works on both Linux and Windows with Powershell/Powercli installed.

Features:

  • Checks “Age”,“Size” and “Size (sum) of all” snapshots.
  • Can create Credential file on the fly. On Windows credentials are stored encrypted.
  • bugs and ugly code :slight_smile:

https://github.com/NeverUsedID/scripts

2 Likes

Hello Janina,
thank you for your description “executing Powershell and/or VMWare PowerCLI directly on your Linux Server”.
This is a really good idea and works well except for the performance: every service check using powershell scripts (with PowerCLI) need more as 7 seconds for execution. With a lot of service checks using powershell the monitoring server gets a huge CPU problem.
Hopefully you could tell us please how to get a better performance?
For your help i would be very thankful.
best regards
Samuel

Hi, I am not running VMWare monitoring directly on my master server.

In order to reduce the load on the master server, a satellite serves as a check source for VMWare.

Currently we are monitoring 5 VMware environments. All via one satellite without CPU problems.

This method can also be questioned, so I am very curious to see what feedback comes from the community. :slight_smile:

Hello ShowMeYourSkil,
thank you for your good hint about satellite server.
Yes, we already have two Icinga satellite server. One of the satellites is only used to check the VMware ESXi Hosts (about 80 ESXi Hosts). Configure the host and service checks is done on the Master Icinga Server and execution of these checks (Powershell with PowerCLI) is on the satellite (Microsoft Powershell Core installed with PowerCLI Module).
We have tried to check the ESXi Host via Powershell with PowerCLI once directly (local user on ESXi) and once via vCenter. But always the time of check execution is about 7 seconds or more. Our “old” Service checks via Perl and VMware SDK need for execution less than 1 second. Maybe it is possible to get the check environment with Powershell/PowerCLI faster? But we have found no solution. We would greatly appreciate it if you can help us.
Best regards

Good day,

I think the problem is that the modules load very slowly.
or how do you see this?

If you load the modules directly through the ESX Cli. How long is the waiting time then? In my check of the VCenter the waiting time is about 3seconds.

https://communities.vmware.com/t5/VMware-PowerCLI-Discussions/Modules-Loading-slow/m-p/2283428#M79845

Best regards :slight_smile:

Good day David,
yes, this is also our experience that the modules load very slowly in the powershell scripts with powercli. In each Service Check with using a powershell script, the powershell environment is started (/usr/bin/pwsh) and the modules are loaded. Every execution of a powershell script with powercli needs 7 or more seconds. We have about 80 ESXi hosts and every host about 9 service checks. Therefore it is actually not possible to do these checks on our icinga satellite via powershell with powercli with this poor performance. Can you please be so kind and tell us what you mean with “If you load the modules directly through the ESX Cli.”
Best regards

1 Like

Hi, I just checked again as well. For me, the checks take about 3-5 seconds. I’ll see if I can find a solution to the problem and report back.

Hi David,
back again with the same problem and status quo.
Have you found in the meantime a solution to the problem?
best regards

Hi,

Unfortunately, no. I also didn’t have the time to look into the problem further.