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:

  1. Top Menu -> Monitoring -> Data Parsers
  2. In the Data Parsers window add a new parser
  3. Select Type = JavaScript
  4. Open Test Data tab
  5. Paste JSON copied before
  6. 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'))
  1. Check Result tab

In the Result tab, the Status and the ServerName should be displayed.

Data Parser

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:

  1. Open Node Settings on a node, when the script will be executed
  2. Add the Script sensor
  3. Select Script Type = PowerShell
  4. Mark Pass credentials through PSCredential object checkbox
  5. Script Output Data Format: select parser created before
  6. 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
  7. Save all settings

Your sensor will now run the PowerShell script on the machine defined in step 1, and will inform about Status changes.

Alerts History

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.

  1. Open Node Settings, then Script sensor settings
  2. Expand Script Parameters field
  3. Click +Add parameter
  4. In the Name field, please enter your parameter (in our example YOUR_COMPUTER_NAME)
  5. 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:

  1. Open Node Settings, then Script sensor settings
  2. Expand Script Parameters field
  3. Change the Name field to '-ComputerName'
  4. Add YOUR_COMPUTER_NAME to the 'Value' field
  5. Save all settings

powershellscriptwindows

NetCrunch Network Monitoring

Network Maps, Dashboards, and Alerts.
Monitor anything. Network, cloud, config.