First I call all iterations that I have in my project – and it works fine and then I want to create a new one via the API. , but the method doesn’t work, I get the error:
The remote server returned an error: (400) Invalid request.
Is there an issue with this not really working properly or am I doing something wrong?
# *the Powershell code:*
# Variables
$organization = "x"
$project = "y"
$pat = "z"
$baseUrl = "https://dev.azure.com/$organization/$project/_apis/work/teamsettings/iterations?api-version=6.0"
# authentication
$headers = @{
Authorization = "Basic " + [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(":$($pat)"))
}
# HTTP GET
$response = Invoke-RestMethod -Uri $baseUrl -Method Get -Headers $headers
# list iterations
$response.value | ForEach-Object {
Write-Output "Iteration Name: $($_.name)"
Write-Output "Start Date: $($_.attributes.startDate)"
Write-Output "End Date: $($_.attributes.finishDate)"
Write-Output "Path: $($_.path)"
Write-Output "----------------------------------"
}
# data for new iteration
$body = @{
name = "blabla"
attributes = @{
startDate = (Get-Date).AddDays(1).ToString("yyyy-MM-ddTHH:mm:ssZ")
finishDate = (Get-Date).AddDays(14).ToString("yyyy-MM-ddTHH:mm:ssZ")
}
} | ConvertTo-Json
# HTTP POST
function Add-Iteration {
param (
[string]$url,
[hashtable]$headers,
[string]$body
)
try {
$response = Invoke-RestMethod -Uri $url -Method Post -Headers $headers -Body $body
Write-Output "Iteration successfully added:"
Write-Output $response
}
catch {
Write-Output "Error adding iteration:"
Write-Output $_.Exception.Message
}
}
# add iteration
Add-Iteration -url $baseUrl -headers $headers -body $iterationData