Discussion

5
Replies
244
Views
JOHNPW_GCS Member since 2009 18 posts
PEGA
Posted: 10 months ago
Last activity: 10 months 4 weeks ago

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:

$remote_PRPC_URL="https://prpchost.lab.pega.com:443"
$hdd_size=Get-Disk|select-object -Property @{n="HDDSize"; expression={[int]($_.Size/1gb)}}|select HDDSize 
$ram_size=(Get-WmiObject win32_physicalmemory|Measure-Object capacity -sum).sum/1gb
$hostname="MyLaptop" # use "$env::COMPUTERNAME" for real hostname

$JSONRecord=@"
{
"caseTypeID": "OKE8TW-Gcs-Work-ComputerAudit",
"processID": "pyStartCase",

"content": { "Hostname": "$($hostname)", "HardriveSize": "$($hdd_size.HDDSize)", "MemorySizeGB": "$($ram_size)" }
}
"@

if ( !( [bool](Get-Variable 'cred' -Scope 'Global' -EA 'Ig') ) ) {
    $cred=get-credential
}
else {
    write-information -MessageData "Using Cached Credentials (unset 'cred' variable to reset)" -InformationAction Continue
}
$url="{0}/{1}" -F $remote_PRPC_URL, "prweb/api/v1/cases"

$result=Invoke-RestMethod -Method Post -Credential $cred -body $example -Uri $url
write-information -MessageData ("Case Created: {0}" -F $result.ID) -InformationAction Continue
$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:

System Administration Developer Knowledge Share
Share this page LinkedIn