Rally Software

 View Only
  • 1.  Powershell Invoke_RestMethod to WSAPI SSO

    Posted Jun 28, 2019 01:24 PM
    Hi-

    I've been struggling to get a simple powershell script to call the Rally REST APIs to retrieve JSON files.   Any insight/help from the community would be greatly appreciated.

    First let me state that my organization uses SSO for access into rally.    As such, I've created an API key with full access. 

    When I run this script in powershell: 

    [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12


    I get:

    Invoke-RestMethod : The remote server returned an error: (401) Unauthorized.
    At line:2 char:2
    +  Invoke-RestMethod -Method get -Uri "https://rally1.rallydev.com/slm/ ...
    +  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebException
        + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand


    I've tried multiple variants of 'headers' based on google searches but can't get anything to work.


    Note: If i input that same URL in my browser (without the headers option) after I SSO, everything works fine.

    Thanks in advance for any help.
    Bob






    ------------------------------
    Bob H
    ------------------------------


  • 2.  RE: Powershell Invoke_RestMethod to WSAPI SSO

    Broadcom Employee
    Posted Jun 28, 2019 01:27 PM
    Hi Bob,

    Can you try passing the following header:
    ZSESSIONID : <your API key>


  • 3.  RE: Powershell Invoke_RestMethod to WSAPI SSO
    Best Answer

    Posted Jun 28, 2019 04:15 PM
    Good Afternoon,

    I have been doing some similar scripting in Powershell myself and this has been working for me where $url is the API call and $ token is your API key.

    (invoke-restmethod -Method Get -Uri $url -Headers @{ 'zsessionid' = $token } -ContentType 'application/json')

    Hope that helps!

    Thanks,
    Michael


  • 4.  RE: Powershell Invoke_RestMethod to WSAPI SSO

    Posted Jul 09, 2019 04:06 PM
    Thank you - that worked!!


  • 5.  RE: Powershell Invoke_RestMethod to WSAPI SSO

    Posted Jul 09, 2019 04:59 PM
    thanks to you both.   I wonder if either of you have already built PS code that loops when the TotalResultCount value exceeds the 2000 PageSize limit?    That is, i know there are currently 16,000+ records in my in the result set and i'd like to create 8 output files with 2000 records each.  I see the 16,000+ value come back in the TotalResultCount field from the first call, and as such know i have to make 7 more calls (incrementing the StartIndex)  to get the rest of the data.    

    Thanks!


  • 6.  RE: Powershell Invoke_RestMethod to WSAPI SSO

    Posted Jul 09, 2019 07:15 PM
    Edited by Bob Hoerl Jul 09, 2019 07:22 PM
    I figured it out so am posting the code to help others:

    $FileCounter = 1;

    $StartIndex = 1;

    $PageSize = 2000;

    $totalresultcount = $StartIndex;

    $RallyType = "UserStory";

    $FileName = $RallyType+$FileCounter+".json";


    [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12  

    While($StartIndex -le $totalresultcount) {

    Invoke-RestMethod -Method get -Uri "https://rally1.rallydev.com/slm/webservice/v2.0/hierarchicalrequirement?pagesize=$PageSize&start=$StartIndex&fetch=Blocked,BlockedReason,CreationDate,DirectChildrenCount,DisplayColor,FormattedID,HasParent,InProgressDate,LastUpdateDate,Name,ObjectID,PlanEstimate,PlannedDeploymentDate,ProductionDate,Program,Rank,Ready,ScheduleState,TaskActualTotal,TaskEstimateTotal,TaskRemainingTotal,TaskStatus,UserStoryType,Feature,Iteration,Owner,Parent,PortfolioItem,Milestones,Tags,Risks" -headers @{"ZSESSIONID" = '<key here>'} -outfile $FileName

    $content = Get-Content $FileName

    $contentobj = ConvertFrom-Json -InputObject $content

    $totalresultcount = $contentobj.QueryResult.TotalResultCount;

    $StartIndex = $StartIndex + $PageSize;

    $FileCounter = $FileCounter + 1

    $FileName = $RallyType+$FileCounter+".json";

    }

    As an FYI - I take the files produced by this script and load them into an Oracle table by using sqlldr then use the JSON_TABLE command (in oracle) to parse the JSON date into a table.   If anyone would find that code helpful i will post.