Author: @unic
Original: https://monitoring-portal.org/t/executing-powershell-and-or-vmware-powercli-directly-on-your-linux-server/2905
Revision: v0.1Tested 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