PowerShell script to test interaction with REST Web Services
search cancel

PowerShell script to test interaction with REST Web Services

book

Article ID: 281217

calendar_today

Updated On:

Products

CA Service Desk Manager CA Service Management - Service Desk Manager

Issue/Introduction

The following article describes a complete script one can leverage to test Service Desk REST access via Powershell.  With all values entered correctly, based on an existing, operational SDM installation with REST web services deployed, it will perform a login and obtain a list of priority table content.

Environment

Release 17.3 or higher
CA Service Desk Manager

Resolution

This document assumes you have some knowledge of Windows PowerShell and the CA Service Desk Manager REST Web Services API.

Broadcom Support provides this document as an example with no guarantees. Broadcom Support will not assist you with PowerShell. However, we can assist with any queries related to the Service Desk REST based Web Services API.

  • Create a ps1 file called "test-sdm-rest.ps1" in a text editor

  • Enter the following text into the file:
    # BEGIN SCRIPT

    # This will setup login credentials
    $User = "<USERID>"
    $PWord = ConvertTo-SecureString -String "<PASSWORD>" -AsPlainText -Force
    $Cred = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $User, $PWord

    # This is the base URI
    $URI_base = "http://<SDM-SERVER>:8050/caisd-rest/"

    # This is the content needed for rest access
    $URI_rest_access = $URI_base + "rest_access"
    $Body_rest_access = "<rest_access/>"

    # Invocation to establish login to REST Web Access
    $result_rest_access = Invoke-RestMethod -Method 'POST' -Uri $URI_rest_access -Credential $Cred -ContentType "application/xml" -Body $Body_rest_access

    "REST Access Key:  " + $result_rest_access.rest_access.access_key

    # Establish headers needed to obtain request data
    $Getheaders.clear
    $Getheaders =  @{}
    $Getheaders.Add("X-AccessKey", $result_rest_access.rest_access.access_key)

    # This is the URI needed for obtaining pri (Priority table) data
    $URI_pri = $URI_base + "pri"

    # Invocation to acquire Priority Table content
    $result_pri = Invoke-RestMethod -Method 'GET' -Uri $URI_pri -ContentType "application/xml" -Headers $Getheaders

    # display Priority result set
    $result_pri.collection_pri.pri

    # END SCRIPT


    Replace all highlighted sections with values that pertain to your environment

  • In Powershell, invoke the above script. 
    cd to the location of the file, "test-sdm-rest.ps1", then run ".\test-sdm-rest.ps1"

  • The following result should appear.  
    PS C:\PSTest> .\test-sdm-rest.ps1
    Rest Access Key:  1015563409

    id  REL_ATTR COMMON_NAME link
    --  -------- ----------- ----
    500 1        5           link
    501 2        4           link
    502 3        3           link
    503 4        2           link
    504 5        1           link
    505 0        None        link

If you have debug logging enabled in SDM per KB Article 120004, you can search for the above access key to review activity.  Debug content is written to the NX_ROOT\log\jrest.log

03/26 20:05:01.892 [http-nio-8050-exec-2] DEBUG DAL_JDBC 132 Statement.executeQuery (0ms): SELECT contact FROM rest_access WHERE access_key = 1015563409

03/26 20:05:01.892 [http-nio-8050-exec-2] DEBUG RoleHandler 84 Calling change_rest_role() for sessionId (1015563409) cntId (U'<UUID DATA>') to roleId -1

03/26 20:05:01.892 [http-nio-8050-exec-2] DEBUG LoggingConnectionFactory 57 Creating DAL connection. SessionContext(sessionId=1015563409, userToken=TESTUSER, contextTenantId=null, filterQuery=false, filterTenantUUIDList=null, parentTenantUUIDList=null)

03/26 20:05:01.892 [http-nio-8050-exec-2] DEBUG priService -1 -----> GET/getObjects() for collection of resources (pri)

03/26 20:05:01.892 [http-nio-8050-exec-2] DEBUG SDMCRUDServiceImpl 1712 GetObjects URI (http://<SDM-SERVER>:8050/caisd-rest/pri)

03/26 20:05:01.908 [http-nio-8050-exec-2] DEBUG DalUtilities 897 getObjectsCountQuery return: SELECT COUNT(*) FROM pri

Additional Information

The above is only a sample script that demonstrates basic REST access via Powershell and is provided "as is".  Broadcom Support does not warrant the above script for functionality and bears no responsibility for its usage in any environment.  Please take all necessary precautions before running the above script (test in a non-prod environment).  

Broadcom Support is also not permitted to advise or collaborate on the above script, including any further modifications or changes.

A counterpart document for SOAP Web Services and Powershell testing is available in KB Article 49239.