Spartan87
Spartan87

Reputation: 119

Pass JSON in powershell script

I want to tag a commit in azure devops git repo using the below POST call (in PowerShell script). API documentation here:

https://learn.microsoft.com/en-us/rest/api/azure/devops/git/annotated-tags/create?view=azure-devops-rest-6.0

{
  "name": "v0.1-beta",
  "taggedObject": {
    "objectId": "c60be62ebf0e86b5aa01dbb98657b4b7e5905234"
  },
  "message": "First beta release"
} 

So far I have written the below PowerShell script:

$Azuretoken = "xxxxxxxxxxxxxxxxxxxxxxxx"
$OrganizationName = "myorg"
$ProjectName = "myproj"
$AuthenicationHeader = @{Authorization = 'Basic ' + [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(":$($Azuretoken)")) }


$uri_tag = "https://dev.azure.com/$($OrganizationName)/$($ProjectName)/_apis/git/repositories/b27ea2df-a72f-45gdf/annotatedtags?api-version=6.0-preview.1"



$body = @(
  "name"= "v0.1-beta",
  "taggedObject" @{
    "objectId" = "2fa9486683e3d3003fd58dbb9345aae3cdd21013"
  },
  "message" = "First beta release"
)

$uri = Invoke-RestMethod -Uri $uri_tag -Method POST -ContentType "application/json" -Body $body -Headers $AuthenicationHeader

Getting errors due to the format of the JSON. Please help.

Upvotes: 0

Views: 426

Answers (1)

GenericUser
GenericUser

Reputation: 3230

Looks like $body isn't formatted correctly. Check out about_Hash_Tables.

$Azuretoken = "xxxxxxxxxxxxxxxxxxxxxxxx"
$OrganizationName = "myorg"
$ProjectName = "myproj"
$AuthenicationHeader = @{Authorization = 'Basic ' + [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(":$($Azuretoken)")) }


$uri_tag = "https://dev.azure.com/$($OrganizationName)/$($ProjectName)/_apis/git/repositories/b27ea2df-a72f-45gdf/annotatedtags?api-version=6.0-preview.1"

$body = @{
    "name" = "v0.1-beta";
    "taggedObject" = @{
        "objectId" = "2fa9486683e3d3003fd58dbb9345aae3cdd21013"
    };
    "message" = "First beta release";
}

$uri = Invoke-RestMethod -Uri $uri_tag -Method POST -ContentType "application/json" -Body $body -Headers $AuthenicationHeader

Upvotes: 2

Related Questions