无论我做什么,Invoke-RestMethod 都会返回 400 错误

无论我做什么,Invoke-RestMethod 都会返回 400 错误

所以我正在开发一个帮助台自动化管理API并且需要更新票证的“状态”。我已阅读文档,但没有看到任何有关更新票证状态的内容。我的代码如下:

    import-module dbatools

# Import API Config from user's profile
$config = Get-Content ~\.samanage\creds.json | ConvertFrom-Json


#Dictionary of headers
$headers = @{
    'Accept' = 'application/vnd.samanage.v2.1+json'
    'Content-Type' = 'application/json'
    'X-Samanage-Authorization'= "Bearer $($config.api_key)"
}
 
#Now how do we find incidences assigned to this user?
$all_alfreds_tickets = Invoke-RestMethod -Method "GET" -URI "https://api.samanage.com/incidents.json?assigned_to=6671937" -Headers $headers

# loop through all the tickets that came back from our API Call
$all_alfreds_tickets| foreach-object -process {

    $ticket = $_

    # Do thing based on ticket category
    switch ($_.subcategory[0].name){

        # How does Alfred handle a Workstation exception request?
        ('Alfred-Workstation Exceptions'){

            #
            # Check Ticket status, if state != Request work the ticket
            #

            # Move the computer into the workstation exceptions out 
            Get-ADComputer $ticket.name | Move-ADObject -TargetPath <Path to OU>

            
            # Respond to the tick letting the world know what's been done.
            Invoke-RestMethod -Method POST -URI "https://api.samanage.com/incidents/$($ticket.id)/comments.json" -headers $headers -Body '{"body": "Hello, I have moved this computer to the Workstation Exceptions OU", "State": "In-Progress",}'
            
            # Update the ticket status to "In-Progress"
            Invoke-RestMethod -Method Put -URI "https://api.samanage.com/incidents/$($ticket.id).json -headers $headers -body '{"state":"In-Progress"}'## Heading ##


            # Add the ticket to the database for monitoring.
        
        }
    }
}

我的问题是无论我做什么,我都无法让 Invoke-RestMethod 成功运行。我开始想也许它只是不受支持,但我的经理让它工作了,但并没有真正提供指导或建议,所以有一种方法可以做到这一点,我只是不知道它是什么。

我能说的最好的是,我认为问题可能出在我的身体上,但我不确定为什么会这样,因为我正在用上面的一行代码做同样的事情,而且它工作得很好。

我尝试了几种不同的方法:

放入:Invoke-RestMethod 返回“400,错误请求”

发布:Invoke-RestMethod 返回“404,未找到”

补丁:Invoke-RestMethod 返回“403,禁止”

我也尝试过改变身体的组合方式。

任何帮助或指导都将非常感激。

答案1

我应该读得慢一点儿文档。

您必须将事件实例作为 JSON 对象发送,而不仅仅是您想要从事件中更新的属性。

答案2

以您的一个“Post”调用为例,我预先准备了调用的参数。

$url = "https://api.samanage.com/incidents/$($ticket.id)/comments.json"
$body = @{
  "body" = "Hello, I have moved this computer to the Workstation Exceptions OU"
  "State" = "In-Progress"
} | ConvertTo-Json

Invoke-RestMethod -Method POST -URI $url -headers $headers -Body $body -ContentType "application/json"

答案3

在第一次调用中,您的 JSON 无效(尾随逗号),并且您无法在同一调用中添加注释并更改状态。因此,您的 JSON 需要如下所示:

{
    "comment":  {
                    "body":  "Hello, I have moved this computer to the Workstation Exceptions OU",
                    "is_private":  "false"
                }
}

POST 到https://api.samanage.com/incidents/$($ticket.id)/comments.json端点

要更新状态,您需要一个像这样的 JSON 负载

{
    "incident": {
        "state":  "In-Progress"
    }
}

您可以将其 PUT 到https://api.samanage.com/incidents/$($ticket.id).json端点

相关内容