Discussion
Pegasystems Inc.
GB
Last activity: 28 Oct 2019 11:39 EDT
Powershell - Use REST to list Pega Agent Status.
Here's a brief working example of using Microsoft Powershell in combination with the OOTB Pega API (tested on 8.1.5) to retrieve system information in a script.
Note: I am not an expert in PS - so I'm sure this can be coded in a better way ; but it still might be a useful starting point for somebody else to improve.
You'll need to make sure you have enabled Powershell to run scripts (outside of this scope - but see here ).
Code:
function get-pegaNode() {
[CmdletBinding()]
param(
[Parameter()]
[string]$PEGA_HOST=$env:computername,
[string]$PEGA_PORT=443,
[string]$PEGA_PROTOCOL="https",
[System.Management.Automation.PSCredential]$CREDENTIAL=(Get-Credential)
)
$NODE_URL="$($PEGA_PROTOCOL)://$($PEGA_HOST):$($PEGA_PORT)/prweb/api/v1/nodes"
$NODES=Invoke-RestMethod -Credential $CREDENTIAL -Uri $NODE_URL -Method Get
return $NODES
}
function get-pegaAgent() {
[CmdletBinding()]
param(
[Parameter()]
[string]$PEGA_HOST=$env:computername,
[string]$PEGA_PORT=443,
[string]$PEGA_PROTOCOL="https",
[System.Management.Automation.PSCredential]$CREDENTIAL=(Get-Credential)
)
$NODES=(get-pegaNode -PEGA_HOST $PEGA_HOST -PEGA_PORT $PEGA_PORT -PEGA_PROTOCOL $PEGA_PROTOCOL -CREDENTIAL $CREDENTIAL)
$BASE_URL="$($PEGA_PROTOCOL)://$($PEGA_HOST):$($PEGA_PORT)/prweb/api/v1/nodes"
foreach ($NODE in $NODES.data.result.cluster_members) {
$AGENT_URL="$($BASE_URL)/$($NODE.nodeID)/agents"
$RESPONSE=Invoke-RestMethod -Credential $CREDENTIAL $AGENT_URL -Method Get
$RESULTS=$RESULTS + $RESPONSE # is this a valid way of appending to the collection? (not tested).
}
return $RESPONSE.data.result.agent_info.instances|Sort-Object name, status
}
Example usage:
> GET-PegaAgent -PEGA_HOST myprpc.mydomain.com | out-gridview
See the parameter block for more information.
One thing which isn't great: the 'get-PegaNode' function works as a 'helper' function for 'get-pegaAgent' - but it returns a fairly complex object, rather than something you can just easily work with in Powershell. (See how 'get-PegaAgent' actually returns a sub-set of the returned JSON for instance).
Screenshot showing 'out-gridview':
Note - the output of GET-pegaAgent is such that you can use it to do useful Powershell stuff like:
get-pegaAgent|where-object -Property status -eq "Stopped"|format-table
That is: show only Stopped agents etc.