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

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

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

    # Prompts for server and credential information with default values
    $ServerName = Read-Host "Enter SDM Server Name [Default: localhost]"
    if ([string]::IsNullOrWhiteSpace($ServerName)) { $ServerName = "localhost" }

    $Port = Read-Host "Enter Port Number [Default: 8050]"
    if ([string]::IsNullOrWhiteSpace($Port)) { $Port = "8050" }

    $User = Read-Host "Enter UserID [Default: ServiceDesk]"
    if ([string]::IsNullOrWhiteSpace($User)) { $User = "ServiceDesk" }

    $PWord = Read-Host "Enter Password" -AsSecureString
    $Cred = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $User, $PWord

    # This is the base URI
    $URI_base = "http://$($ServerName):$($Port)/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

    # Invocation to DELETE REST Web Access
    $URI_rest_access_DEL = $URI_rest_access+"/"+$result_rest_access.rest_access.id
    $result_rest_access_DEL = Invoke-RestMethod -Method 'DELETE' -Uri $URI_rest_access_DEL -ContentType "application/xml" -Headers $Getheaders

    # END SCRIPT


    The above test script assumes a non-SSL based connection to REST is available. Ideally, the script should be executed on the local SDM Server and can be accessed over http through localhost.  SSL access is not included as part of the script functionality.

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


  • The script will prompt for the SDM Server name, port number, userid, and password.  Except for the password, pressing enter on each prompt will default the values as shown.  

  • The following result should appear.  In this example, the script was executed on the direct SDM Server, with the default values used.
    PS C:\PSTest> .\test-sdm-rest.ps1
    Enter SDM Server Name [Default: localhost]:
    Enter Port Number [Default: 8050]:
    Enter UserID [Default: ServiceDesk]:
    Enter Password: *************
    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 REST debug logging enabled in SDM, 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).   We have made every effort to ensure that the script operates as designed and does not perform any permanent changes or updates

This document assumes you have some knowledge of Windows PowerShell and the CA Service Desk Manager REST Web Services API.  Assistance with using Powershell itself is outside the scope of Broadcom Support. 

Broadcom Support is not permitted to advise or collaborate on the above script, including any further modifications or changes.  However, we can assist with any queries related to the Service Desk REST based Web Services API.

The usage of Powershell with SOAP/CXF web services is not recommended.  This is due to the use of MTOM, which Powershell's New-WebServiceProxy function does not support.