How to use script sensor with parameterized PowerShell script
Fill any gaps in delivering monitoring data to NetCrunch with custom scripts - and get alerts based on parameters' readings
Unlike other systems, NetCrunch does not force you to deliver data in one native data format, because there are thousands of scripts written for open source systems already. We want these scripts to be adaptable to NetCrunch, and this is the role of Data Parsers that are responsible for translating data returned by an external script to NetCrunch native format.
Example - using a script to check computer certificate
Suppose you want the script to check computer certificates and get info about their status. You want to generate an alert automatically if the Status of the certificate is other than OK. We will show you how to do this in NetCrunch with an example of PowerShell scripts without parameters, with command-line arguments, and with named parameters.
Read more about scripting sensors in NetCrunch.
Initial configuration
The first thing you need is the PowerShell script that collects the Status of computer certificates:
param(
[System.Management.Automation.PSCredential]$Credential
)
Invoke-Command -ComputerName <YOUR_COMPUTER_NAME> -Credential $Credential -ScriptBlock {
[array]$objs = @()
$today = Get-Date
$certs = Get-ChildItem -Path 'Cert:\LocalMachine\My'
foreach ( $cert in $certs )
{
$obj = [pscustomobject][ordered] @{
Subject = $cert.Subject
FriendlyName = $cert.FriendlyName
NotBefore = $cert.NotBefore.ToString()
NotAfter = $cert.NotAfter.ToString()
DaysValid = (New-TimeSpan -Start $today -End $cert.NotAfter).Days
}
$objs += $obj
}
$Friendlyname = $cert.FriendlyName
$warningCerts = $objs | Where-Object {($_.DaysValid -le '30') -and ($_.DaysValid -gt '0')}
$errorCerts = $objs | Where-Object {$_.DaysValid -le '0'}
$objs = $objs | Where-Object {$_.DaysValid -le '30'}
if ((-not($warningCerts)) -and (-not($errorCerts))) {$status = 'OK'}
elseif ($errorCerts) {$status = 'ERROR'}
elseif ($warningCerts) {$status = 'WARNING'}
[array]$output = @()
$output = [pscustomobject][ordered] @{
Value = $status
ServerName = $env:COMPUTERNAME
}
ConvertTo-Json -InputObject @($output)
}
Now we can check what information is displayed, after running such a script in PowerShell. The output from the script looks like this:
[
{
"Value": "OK",
"ServerName": "<YOUR_COMPUTER_NAME>"
}
]
The output is in the form of JSON. So we can copy it and create the Data Parser for the output.
Data parser
To parse the data from JSON into NetCrunch, we need to define a simple parser:
- Top Menu -> Monitoring -> Data Parsers
- In the Data Parsers window add a new parser
- Select Type = JavaScript
- Open Test Data tab
- Paste JSON copied before
- Write a script to parse data e.g.
const doc = typeof data === 'string' ? JSON.parse(data) : data;
result
.status('Status', selectByJSONPath(doc, '$..Value'))
.status('ServerName', selectByJSONPath(doc, '$..ServerName'))
- Check Result tab
In the Result tab, the Status and the ServerName should be displayed.
Set Script sensor without parameters in the script
You can set the Script sensor to do not use any parameters. If your PowerShell script does not use parameters, it is not necessary to set them in NetCrunch. This can be used if you wish to execute the script for a single node.
Our sample script will collect the status of the computer certificate and server name, and deliver it in JSON format. We can use it now in the Script sensor in NetCrunch
To add such sensor:
- Open Node Settings on a node, when the script will be executed
- Add the Script sensor
- Select Script Type = PowerShell
- Mark
Pass credentials through PSCredential object
checkbox - Script Output Data Format: select parser created before
- If you want to monitor e.g. Status, Add Alert, select
New Event for Status Object Change
, and set the appropriate states to be monitored - Save all settings
Your sensor will now run the PowerShell script on the machine defined in step 1, and will inform about Status changes.
Powershell script with command-line arguments
You may want to use parameters instead of defining the computer name directly in the script. NetCrunch can take advantage of them.
The easiest way to pass command-line arguments is to write a script in which we substitute the argument number for the parameter name. In our case it will look like this:
$ComputerName=$args[0]
Invoke-Command $ComputerName -Credential $Credential -ScriptBlock {
(...)
In NetCrunch we will modify our Script sensor.
- Open Node Settings, then Script sensor settings
- Expand Script Parameters field
- Click +Add parameter
- In the Name field, please enter your parameter (in our example YOUR_COMPUTER_NAME)
- Save all settings
Parameterized PowerShell script with named parameters
It is also possible to pass named parameters to the PowerShell script. Example of PowerShell script with named parameter:
param(
$ComputerName,
[System.Management.Automation.PSCredential]$Credential
)
Invoke-Command $ComputerName -Credential $Credential -ScriptBlock {
(...)
Let's modify the Script sensor settings in NetCrunch again:
- Open Node Settings, then Script sensor settings
- Expand Script Parameters field
- Change the Name field to '-ComputerName'
- Add YOUR_COMPUTER_NAME to the 'Value' field
- Save all settings
- [18.06.2020]How to monitor failed login attempts in Linux
One of the things administrators often do is check multiple failed login attempts in Linux. Monitoring them allows you to assess whether illegal or unwanted attempts to our network have occurred. In this article, we will show you how to track it automatically using NetCrunch.
- [05.02.2020]Monitoring Backup files
Backups are a vital part of any network infrastructure- they can contain personal data files, configurations or any important data. NetCrunch can help you track if backups are successfully created and if files are present in the designated directory
- [30.01.2020] Monitoring of Windows Shared Cluster
Monitoring Windows Shared Clusters can be achieved in many different ways. This article demonstrates the power of NetCrunch scripts combined with data parsers to show you how easy and fast it is to setup NetCrunch to collect data about free and used space on clusters
- [26.02.2019] Script monitoring with NetCrunch
Despite huge capabilities of monitoring, NetCrunch offers the possibility of executing and parsing any given script, this allows monitoring almost everything with any script on any machine. This article shows a very simple example of how script monitoring can be used to get data in specific conditions.
- [21.02.2019] Monitoring of Windows Updates and Pending Reboots
Updates are essential to keep network infrastructure safe and solid, NetCrunch 10.5 provides new functionality that allows monitoring of update status, missing updates, and failed installations. It can also notify when the system is running for too long without installing updates. This article shows how to check which updates are available and how to set these two essential sensors.
- [10.05.2018]How to use NetCrunch data parsers with REST sensor
See how easy it is to use REST sensor in NetCrunch to get any data, and process it with parsers for counters and statuses. It can be added to any view as a widget for live visibility.