Rest API calls issue from PowerShell

vsp 1 Reputation point
2021-07-27T08:55:53.927+00:00

This is the first time using RestAPI from PowerShell and couldn't find much help. Following is the code we are using:

$Success = "Success"
$Failure = "Failure"
$Running = "Running"

Function Update-RestAPI {

[CmdletBinding()]
param (
    [Parameter()]
    [String]$ItInitials,
    [String]$Stepname,
    [String]$Status
)

$body = @{  
    "itinitials" = $ItInitials
    "stepname"   = $Stepname
    "status"     = $Status
}
$body = $body | ConvertTo-Json
Invoke-RestMethod -Uri $APIupdateurl -Method 'Post' -Body $body -ContentType 'application/json'
}

We call this function as following from different functions based on the outcome of that task.

Update-RestAPI -ItInitials $apiuser -Stepname $stepname -Status $Running
Update-RestAPI -ItInitials $apiuser -Stepname $stepname -Status $Success
Update-RestAPI -ItInitials $apiuser -Stepname $stepname -Status $Failure

For the tasks we are automating, every task will have running, success or failure status.

The running status for the API call is first step in the calling function to indicate the task running has started.

Everything is working as expected but "Running" status is not getting posted to the API but Success and Failure are showing up fine. Also, this Running status only shows up for one API call(for a specific task) but for the rest of the tasks, it doesn't show up.

I am really lost here and couldn't find why the same API call function works for other status's but just for Running status.

Any help would be really grateful.

Thanks in advance.

Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,521 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Rich Matheisen 46,711 Reputation points
    2021-07-28T19:40:40.1+00:00

    Is the "$body" variable supposed to contain JSON? What you've coded is a hash.

    # convert HASH to JSON
    $body = @{  
        "itinitials" = $ItInitials
        "stepname"   = $Stepname
        "status"     = $Status
    }
    
    $JSONBody = ConvertTo-JSON -InputObject $body
    
    
    # or provide JSON in the correct format as a string
    $body = @"
    {
        "status":  $ItInitials,
        "itinitials":  $Stepname,
        "stepname":  $Status
    }
    "@
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.