Powershell script : example

Here's a basic Microsoft Windows Powershell script which shows how easily you can build a set of scripts that communicate directly with the 'Pega API'.

This basic script will create a new Case - populating 'Hard Drive', 'Memory Size' and 'hostname' properties automatically from the Windows host you are one.

First create a CASE in PRPC - in this example I created 'ComputerAudit' and three properties (unfortunately - just realised I spelled 'hard drive' wrong :-)

Now: assuming a couple of other things:

- You have set up Powershell to run on your Windows machine.

(out of scope for this - but check here for instance.

- You have edited the script to match your system coordinates

- You ensure you are connecting with a PRPC Operator who has Pega API access (check access groups).

Here's the basic script:

  1. $remote_PRPC_URL="https://prpchost.lab.pega.com:443"
  2. $hdd_size=Get-Disk|select-object -Property @{n="HDDSize"; expression={[int]($_.Size/1gb)}}|select HDDSize
  3. $ram_size=(Get-WmiObject win32_physicalmemory|Measure-Object capacity -sum).sum/1gb
  4. $hostname="MyLaptop" # use "$env::COMPUTERNAME" for real hostname
  6. $JSONRecord=@"
  7. {
  8. "caseTypeID": "OKE8TW-Gcs-Work-ComputerAudit",
  9. "processID": "pyStartCase",
  11. "content": { "Hostname": "$($hostname)", "HardriveSize": "$($hdd_size.HDDSize)", "MemorySizeGB": "$($ram_size)" }
  12. }
  13. "@
  15. if ( !( [bool](Get-Variable 'cred' -Scope 'Global' -EA 'Ig') ) ) {
  16. $cred=get-credential
  17. }
  18. else {
  19. write-information -MessageData "Using Cached Credentials (unset 'cred' variable to reset)" -InformationAction Continue
  20. }
  21. $url="{0}/{1}" -F $remote_PRPC_URL, "prweb/api/v1/cases"
  23. $result=Invoke-RestMethod -Method Post -Credential $cred -body $example -Uri $url
  24. write-information -MessageData ("Case Created: {0}" -F $result.ID) -InformationAction Continue
  25. $result | out-gridview

NB: there is no error handling in this script; and there are probably better ways of getting the information (for disks, memory etc) - but it should demonstrate the principle well enough.

Running this should result in an output in a window like this:

As well as text output on the console:

And here's the same case information in a PRPC Portal:


Keep up to date on this post and subscribe to comments

October 27, 2019 - 6:22am

Awesome stuff. 

October 27, 2019 - 6:38am

Just noticed an error (I had edited the script before posting).

The Invoke-RestMethod was posting the body with a variable $example - should be


Also (in comments) - should be $ENV:computername (not double colons "::").

$result=Invoke-RestMethod -Method Post -Credential $cred -body $JSONRecord -Uri $url


October 27, 2019 - 10:52am

Re-wrapped into a function ("Add-PegaCase") with parameters - with basic help.

Once the function is built: run

PS> GET-HELP Add-PegaCase -detailed

Also (for no reason at all, except fun) added Speech Synthesizer output.

Still not properly handling errors. (I believe Powershell will *halt* - even on handled - on exceptions - but I need to check that).

Set variables $PEGAHOST (and optionally) $CRED before calling.

(You'll get an error the first time you run this - those variables aren't set).

  1. function New-PegaCase() {
  2. <#
  4. Invokes Pega REST API to create a CASE.
  6. Note: this is only an example and is currently hardcoded with regard to property names
  7. Error handling is also very shabby.
  10. $PEGAHOST="mypegahost"
  11. $CRED=(get-credential)
  12. $result=New-PegaCase -ComputerName $PEGAHOST -Credential $CRED
  14. #>
  15. [CmdletBinding()]
  16. param(
  17. [Parameter()]
  18. [string]$ComputerName="$($env:Computername)",
  19. [System.Management.Automation.PSCredential]$Credential=(Get-Credential),
  20. [string]$CaseTypeID="OKE8TW-Gcs-Work-ComputerAudit"
  21. )
  23. $Uri="https://$($Computername)/prweb/api/v1/cases"
  25. $hdd_size=Get-Disk|select-object -Property @{n="HDDSize"; expression={[int]($_.Size/1gb)}}|select HDDSize
  26. $ram_size=(Get-WmiObject win32_physicalmemory|Measure-Object capacity -sum).sum/1gb
  27. $hostname="MyLaptop" # use "$env::COMPUTERNAME" for real hostname
  29. $JSONRecord=@"
  30. {
  31. "caseTypeID": $($CaseTypeID),
  32. "processID": "pyStartCase",
  34. "content": { "Hostname": "$($hostname)", "HardriveSize": "$($hdd_size.HDDSize)", "MemorySizeGB": "$($ram_size)" }
  35. }
  36. "@
  38. $result=Invoke-RestMethod -Method Post -Credential $Credential -body $JSONRecord -Uri $Uri -TimeoutSec 5
  39. return $result
  40. }
  42. # Main Block / Testing.
  44. Add-Type -AssemblyName System.Speech
  45. $synthesizer = New-Object -TypeName System.Speech.Synthesis.SpeechSynthesizer
  47. try {
  48. $result=$none
  49. $result=New-PegaCase -ComputerName $PEGAHOST -Credential $CRED
  50. $synthesizer.speak("Created Case: $($result.id.split()[1])")
  51. $result|format-table
  52. }
  53. catch { write-host ($_)
  54. $synthesizer.speak("Uh oh, something went wrong") }


October 28, 2019 - 4:21am


Is it only for the purpose of showing how can we create a case using PowerShell or you have maybe a business case for that?

btw, have you a version for bash script?

October 28, 2019 - 5:40am

Hi Vaspoz,

No real use case in mind - it was just a way for me to test out the REST API using something other than a browser (or Python as I have used in the past).

I don't have BASH version for Linux - maybe that would be good challenge to leave for the community ! :-)

Here's some possible starting points:

  1. awk ' /MemTotal/ { printf("%d\n",$2/(1024*1024)) } ' /proc/meminfo


And using 'curl' (or wget maybe I guess) instead of 'Invoke-WebRequest' / 'Invoke-RestMethod'.