Windows OS Hub
  • Windows
    • Windows 11
    • Windows Server 2022
    • Windows 10
    • Windows Server 2019
    • Windows Server 2016
  • Microsoft
    • Active Directory (AD DS)
    • Group Policies (GPOs)
    • Exchange Server
    • Azure and Microsoft 365
    • Microsoft Office
  • Virtualization
    • VMware
    • Hyper-V
  • PowerShell
  • Linux
  • Home
  • About

Windows OS Hub

  • Windows
    • Windows 11
    • Windows Server 2022
    • Windows 10
    • Windows Server 2019
    • Windows Server 2016
  • Microsoft
    • Active Directory (AD DS)
    • Group Policies (GPOs)
    • Exchange Server
    • Azure and Microsoft 365
    • Microsoft Office
  • Virtualization
    • VMware
    • Hyper-V
  • PowerShell
  • Linux

 Windows OS Hub / PowerShell / Zabbix: How to Get Data from PowerShell Scripts

March 17, 2024

Zabbix: How to Get Data from PowerShell Scripts

In this article, we will look at configuring the Zabbix Agent to retrieve monitoring data from PowerShell scripts. Let’s look at two PowerShell scripts to get some data into Zabbix. The first returns the number of active RDP user sessions on a Windows RDS server, and the second returns the number of days since Windows updates were last installed on the server.

The Zabbix agent has two features for data retrieval from an external PowerShell script:

  • The UserParameter option in the agent’s configuration file allows you to execute PowerShell code. If you use this option, you must enable the UserParameter option and copy the PS1 script file to each Windows host.
  • You can run PowerShell scripts using system.run. This allows you to specify the PowerShell script directly in the Zabbix web interface and can run arbitrary commands.

Let’s start with an example of running a PowerShell script using UserParameter. Suppose you already have a Zabbix agent installed and configured on your Windows computer.

Previously we showed you how to use UserParameter to run a bash script in Zabbix to check a domain’s expiration date.

Create a simple PowerShell script that returns the number of active RDP sessions and save it to a file: C:\Program Files\Zabbix Agent 2\Script\GetActiveRDPSessionCount.ps1

$RDSsessions= qwinsta |ForEach-Object {$_ -replace "\s{2,18}",","} | ConvertFrom-Csv
$RDSActiveSessions=@($RDSsessions| where State -eq 'Active').count
Write-Host $RDSActiveSessions

You can also get information about active RDS connections from Event Viewer logs.

Now edit the Zabbix agent configuration file (zabbix_agent2.conf) and add the option:

UserParameter=ActiveRDSSessions,powershell -NoProfile -ExecutionPolicy bypass -File "C:\Program Files\Zabbix Agent 2\Script\GetActiveRDPSessionCount.ps1"

Enable UserParameter parameter with PowerShell script in Zabbix agent

The -ExecutionPolicy bypass parameter allows you to run a PowerShell script without changing the PowerShell execution policy settings.

Restart the Zabbix Agent service:

Get-Service 'Zabbix Agent 2'| Restart-Service -force

Make sure that the Zabbix agent can receive the data from the new parameter. Use the built-in zabbix-get command line tool to test the agent:

zabbix_get -s 127.0.0.1 -p 10050 -k ActiveRDSSessions

In this example, Zabbix ran a PowerShell script and returned that there were two RDP user sessions active on the host.

zabbix_get: get data from UserParameter

PowerShell code is usually quite slow to run. Therefore, you need to increase the timeout in the agent configuration from the default 3 seconds to 20 seconds (set Timeout=20), otherwise, Zabbix will return an error when receiving data from a script:

ZBX_NOTSUPPORTED: Timeout while executing a shell script.

When running the command, you can see another error:

zabbix_get [4292]: Get value error: ZBX_TCP_READ() failed: [0x00002746] An existing connection was forcibly closed by the remote host.
zabbix_get [4292]: Check access restrictions in Zabbix agent configuration.

If so, allow to accept local connections in the agent configuration file (zabbix_agent2.conf). Add the 127.0.0.1 address.

Server=192.168.10.100,127.0.0.1

Then you can add a new parameter to your template. Go to the Items tab and set:

  • Name: Number of active RDS sessions
  • Type: Zabbix Agent (active)
  • Key: ActiveRDSSessions
  • Type of information: Numeric (unsigned)
  • Update Interval: 1m
  • History storage period: 90d
  • Trend storage period: 365d

Create PowerShell script in Zabbix template

Go to Monitoring -> Latest data and check that Zabbix is now receiving the value from the PowerShell script.

Now, let’s allow PowerShell scripts to be run using system.run. This method is less secure because you can run any command on the remote host via Zabbix. However, it is convenient because it allows to configure PowerShell scripts directly from the Zabbix web interface.

Enable the following parameter in the configuration file of the agent:

AllowKey=system.run[*]

Then create a new Zabbix Item:

  • Name: Days since last Windows Update installation
  • Type: Zabbix Agent
  • Key: system.run[powershell.exe -command "(New-Timespan -Start ((New-Object -com 'Microsoft.Update.AutoUpdate').Results|Select -ExpandProperty LastInstallationSuccessDate) -End (Get-Date)).days"]
  • Type of information: Numeric (unsigned)
  • Update Interval: 1d
  • History: 180d
  • Trenfd: 365d
This command returns the number of days since the last time a security update was installed on Windows.

So we have looked at how you can get data into Zabbix from PowerShell scripts running on Windows.

0 comment
4
Facebook Twitter Google + Pinterest
PowerShellWindows 10Windows Server 2019
previous post
Tracking Printer Usage with Windows Event Viewer Logs
next post
Send Emails with Microsoft Graph API and PowerShell

Related Reading

Wi-Fi (Internet) Disconnects After Sleep or Hibernation on...

March 15, 2024

Fix: Remote Desktop Licensing Mode is not Configured

August 24, 2023

How to Install Remote Server Administration Tools (RSAT)...

March 17, 2024

How to Find the Source of Account Lockouts...

March 12, 2024

How to Delete Old User Profiles in Windows

March 15, 2024

Managing Windows Firewall Rules with PowerShell

March 11, 2024

How to Allow Non-Admin User to Start/Stop Service...

March 15, 2024

How to Install and Configure Free Hyper-V Server...

March 16, 2024

Leave a Comment Cancel Reply

join us telegram channel https://t.me/woshub
Join WindowsHub Telegram channel to get the latest updates!

Recent Posts

  • Configuring Windows Protected Print Mode (WPP)

    May 19, 2025
  • Map a Network Drive over SSH (SSHFS) in Windows

    May 13, 2025
  • Configure NTP Time Source for Active Directory Domain

    May 6, 2025
  • Cannot Install Network Adapter Drivers on Windows Server

    April 29, 2025
  • Change BIOS from Legacy to UEFI without Reinstalling Windows

    April 21, 2025
  • How to Prefer IPv4 over IPv6 in Windows Networks

    April 9, 2025
  • Load Drivers from WinPE or Recovery CMD

    March 26, 2025
  • How to Block Common (Weak) Passwords in Active Directory

    March 25, 2025
  • Fix: The referenced assembly could not be found error (0x80073701) on Windows

    March 17, 2025
  • Exclude a Specific User or Computer from Group Policy

    March 12, 2025

Follow us

  • Facebook
  • Twitter
  • Telegram
Popular Posts
  • Install and Manage Windows Updates with PowerShell (PSWindowsUpdate)
  • Fix: Remote Desktop Licensing Mode is not Configured
  • How to Delete Old User Profiles in Windows
  • How to Install Remote Server Administration Tools (RSAT) on Windows
  • Configuring Port Forwarding in Windows
  • Start Menu or Taskbar Search Not Working in Windows 10/11
  • Adding Drivers into VMWare ESXi Installation Image
Footer Logo

@2014 - 2024 - Windows OS Hub. All about operating systems for sysadmins


Back To Top