Discussion

58
Views
JOHNPW_GCS Member since 2009 17 posts
PEGA
Posted: 9 months ago
Last activity: 9 months 2 weeks ago

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.

System Administration Developer Knowledge Share
Share this page LinkedIn