References > Aliases > PowerShell Code with SAM

PowerShell Code with SAM

SolarWinds does not provide customer support for any scripting language. For scripting support with SAM, the SolarWinds public community on may be of assistance.

Note: Unless otherwise directed by the documentation, you should not need to edit predefined scripts.

To edit the default script for the Number of items received by specific user during last month monitor, click the Edit button for that monitor. Below is the default PowerShell script for this component monitor:

Note: Each statistic and message output pair of your script requires a unique identifier. A maximum of 10 output pairs can be monitored per script.

$ErrorActionPreference = "silentlycontinue";
add-pssnapin Microsoft.Exchange.Management.PowerShell.E2010;
add-pssnapin Microsoft.Exchange.Management.PowerShell.Admin;
$address = $args.get(0);
$server = $args.get(1);
if ( !$address )
   Write-Host "Message: Can't find "user_mailbox" argument. Check documentation.";
   exit 1;
if ( !$server )
   Write-Host "Message: Can't find "server" argument. Check documentation.";
   exit 1;
$t1 = Get-Date;
$t2 = $t1.AddMonths(-1);
$stat = (Get-MessageTrackingLog -Server $server -Recipients $address -EventID "Receive" -ResultSize "Unlimited" -Start $t2 -End $t1 | Measure-Object).Count;
if ($Error.Count -eq 0) {
   Write-Host "Message: User $address received: $stat items during last month";
   Write-Host "Statistic: $stat";
   Exit 0;
Write-Host "Message: $($Error[0])";
Exit 1;

Variables are used for storing information. In SAM, variables are prefixed with "$", as highlighted below. The following code snippet from the above code calculates a numerical value and then stores it in the variable $stat. In the illustration below, the variable's value is reported as 9356, as highlighted in the Statistic column's output.

Text and variables within quotes indicate information that may be visible to the user. When made visible, the variables in the message will be replaced with the values the variables store. The variables are highlighted below in both the code and the output:

With these same lines of code, Message: and Statistic: refer to the columns where the information will be placed:

Scripts Must Report Status Through Exit Codes

Scripts must report their status by exiting with the appropriate exit code. The exit code is used to report the status of the monitor, which is seen by the user through the interface. The following table explains the exit codes and their values:

Exit Code










Any other value



The following code snippet shows proper usage of exit codes.

The two exit codes in this example are conditional, meaning either one or the other will be triggered based on a certain outcome. When Exit 0; (status of Up) is reported, the message and statistic are displayed and the monitor shows a status of Up. When Exit 1; (status of Down) is reported, the message and statistic are not displayed and a status of Down is reported.

If you want to inform SolarWinds SAM that a PowerShell script reports an Up status, you would exit the script using Exit 0;

Scripts with Text Output

Scripts report additional details by sending text to the script’s standard output. SAM supports multiple values returned by a script using the following format. There is a limit of 10 Statistic and Message pairs for the script. These can be placed anywhere in the script output. The Statistic and Message names you give must contain valid letters and/or numbers.

Detail Type Required Meaning



A numeric value used to determine how the monitor compares to its set thresholds. This must be an integer value, (negative numbers are supported).

Statistic.Name1: 123Statistic.Name2: 456



An error or information message to be displayed in the monitor status details. Note: Multi-line messages are supported. To use this functionality, print each line using a separate command. For example:

Message.Name1: abc

Message.Name2: def


For more information, refer to the following sections: