Discussion

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:

  1. function get-pegaNode() {
  2. [CmdletBinding()]
  3. param(
  4. [Parameter()]
  5. [string]$PEGA_HOST=$env:computername,
  6. [string]$PEGA_PORT=443,
  7. [string]$PEGA_PROTOCOL="https",
  8. [System.Management.Automation.PSCredential]$CREDENTIAL=(Get-Credential)
  9. )
  10. $NODE_URL="$($PEGA_PROTOCOL)://$($PEGA_HOST):$($PEGA_PORT)/prweb/api/v1/nodes"
  11. $NODES=Invoke-RestMethod -Credential $CREDENTIAL -Uri $NODE_URL -Method Get
  12. return $NODES
  13. }
  14.  
  15. function get-pegaAgent() {
  16. [CmdletBinding()]
  17. param(
  18. [Parameter()]
  19. [string]$PEGA_HOST=$env:computername,
  20. [string]$PEGA_PORT=443,
  21. [string]$PEGA_PROTOCOL="https",
  22. [System.Management.Automation.PSCredential]$CREDENTIAL=(Get-Credential)
  23. )
  24.  
  25.  
  26. $NODES=(get-pegaNode -PEGA_HOST $PEGA_HOST -PEGA_PORT $PEGA_PORT -PEGA_PROTOCOL $PEGA_PROTOCOL -CREDENTIAL $CREDENTIAL)
  27.  
  28. $BASE_URL="$($PEGA_PROTOCOL)://$($PEGA_HOST):$($PEGA_PORT)/prweb/api/v1/nodes"
  29. foreach ($NODE in $NODES.data.result.cluster_members) {
  30. $AGENT_URL="$($BASE_URL)/$($NODE.nodeID)/agents"
  31. $RESPONSE=Invoke-RestMethod -Credential $CREDENTIAL $AGENT_URL -Method Get
  32. $RESULTS=$RESULTS + $RESPONSE # is this a valid way of appending to the collection? (not tested).
  33. }
  34. return $RESPONSE.data.result.agent_info.instances|Sort-Object name, status
  35.  
  36. }

Example usage:

  1. > 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.

Comments

Keep up to date on this post and subscribe to comments