LogicMonitor seeks to disrupt AI landscape with $800M strategic investment at $2.4B valuation to revolutionize data centers.

Learn More

Embedded PowerShell Scripting

Last updated on 08 October, 2024

On Windows Collectors, LogicMonitor supports embedded scripting using PowerShell. Although we typically recommend Groovy for most tasks that required scripting, PowerShell is advantageous when you need to monitor Windows-specific metrics that might be accessible only via PowerShell Cmdlets. With some configuration PowerShell can also be used for “Remoting” which allow one to invoke commands on remote systems, which can be useful for some types of metric collection.

Unlike our embedded Groovy scripting functionality, PowerShell scripts are run by the interpreter provided by the underlying OS. Because the underlying features and capabilities vary across versions of Windows, it may be difficult to guarantee complete portability of a PowerShell script. For broad compatibility, we recommend judicious use of the extended PowerShell feature set.

Embedded PowerShell is supported specifically for Scripted Active Discovery, Scripted Data Collection, Scripted Eventsources, and ConfigSources.

Using Device Properties in PowerShell Scripts

Unlike Groovy, we don’t have any seamless way to pass device properties into a PowerShell script. Instead, we can substitute any property into an embedded PowerShell script that is coded as ##DEVICE.PROPERTY##.

Instance properties are not currently supported in PowerShell.

Example – Using PowerShell for Datasource Active Discovery

The following is sample PowerShell code used to perform Scripted Active Discovery. In scripted Active Discovery, the goal is to return instance data of the form:

instance_id##instance_name##instance_description

by printing one instance per line to stdout. In this example, we’re going to enumerate the DHCP scopes provided by a Windows DHCP server.

# get the hostname against which we need to run the WMI 
$hostname = '##SYSTEM.HOSTNAME##';

# get an array of all dhcp scopes
$scopes_array = (Get-WmiObject `
  -Namespace ROOT\Microsoft\Windows\DHCP `
  -ComputerName $hostname -List | ` 
  Where {$_.Name -eq 'PS_DhcpServerv4Scope'}).Get() | `
  Select-Object -ExpandProperty cmdletOutput;

# iterate through the scopes array
foreach ($scope in $scopes_array) 
{
    # get the interesting information from the scope array
    $scope_id      = $scope.ScopeId;
    $instance_name = $scope.Name + " : " + $scope.Description;
    $instance_desc = "Scope ID: " + $scope_id;

    # write out the instance data
    Write-Host "$scope_id##$instance_name##$instance_desc";
}

# return with a response code that indicates we ran successfully
Exit 0;

Example – Using PowerShell for Datasource Data Collection

In the above example, we illustrate how to identify the DHCP scopes available on a Windows DHCP server. Next, we’ll continue with that same device to get additional data on the IP addresses in each scope.

# get the hostname against which we need to run the WMI 
$hostname = '##SYSTEM.HOSTNAME##';

# get an array of all dhcp scopes
$scopes_array = (Get-WmiObject `
  -Namespace ROOT\Microsoft\Windows\DHCP `
  -ComputerName $hostname -List | ` 
  Where {$_.Name -eq 'PS_DhcpServerv4Scope'}).Get() | `
  Select-Object -ExpandProperty cmdletOutput;

# iterate through the scopes array
foreach ($scope in $scopes_array) 
{
    # get the interesting information from the scope array
    $ScopeId = $scope.ScopeId;
    $AddressesFree = $scope.AddressesFree;
    $AddressesInUse = $scope.AddressesInUse;
    $PendingOffers = $scope.PendingOffers;
    $ReservedAddress = $scope.ReservedAddress;

    # write key-value pairs to stdout
    Write-Host "AddressesFree=${AddressesFree}";
    Write-Host "AddressesInUse=${AddressesInUse}";
    Write-Host "PendingOffers=${PendingOffers}";
    Write-Host "ReservedAddress=${ReservedAddress}";
}

# return with a response code that indicates we ran successfully
Exit 0;

From here we create four datapoints, and use the key-value post-processor to extract the relevant data with keys “AddressesFree”, “AddressesInUse”, “PendingOffers”, and “ReservedAddress”.

See our page on Powershell Tips & Tricks for more examples and sample code.

In This Article