Documentation forServer & Application Monitor
Monitoring your applications and environment is a key capability of Hybrid Cloud Observability and is also available in a standalone module, Server & Application Monitor (SAM). Hybrid Cloud Observability and SAM are built on the self-hosted SolarWinds Platform.

Use PowerShell code with SAM

This section includes a sample PowerShell script included in SAM and describes some of its elements. You should not need to edit predefined scripts included in SAM unless otherwise directed by documentation or SolarWinds Support.

Depending on how you configure PowerShell in your environment, it can make your system vulnerable to unauthorized access. For details, see Use PowerShell in SAM.

SolarWinds provides customer support for PowerShell scripts and functionality built into SAM, but not for scripting languages or custom scripts. For scripting support from the SolarWinds online IT community, visit THWACK.

Here is an example of a script to track the number of emails received by a specific user during the last month: 

$ErrorActionPreference = "silentlycontinue";
add-pssnapin Microsoft.Exchange.Management.PowerShell.E2010;
add-pssnapin Microsoft.Exchange.Management.PowerShell.Admin;
$address = $args.get(0);
$server = $args.get(1);
$Error.Clear();
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;

Each statistic and message output pair of your script requires a unique identifier. A maximum of 10 output pairs can be monitored per script. If you exceed the maximum allowed, remove the excess output pairs or they will be ignored. For details, see Write scripts for SAM script monitors.

SAM variables start with the "$" symbol, as highlighted below. The following snippet from the above code calculates a numerical value and then stores it in the $stat variable. In the illustration below, the variable's value is reported as 9356.

Text and variables within quotes indicate data 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:

To learn more about working with PowerShell code, see the SAM Custom Application Monitor Template Guide that includes topics such as: