2

I'm trying to use powershell to send json to DevOps API. I can't seem to figure out how to properly format this so powershell will take it. I keep getting this error. Any advice? I'm able to use the same json in Postman without any issues. Thanks

$URI= "https://vsaex.dev.azure.com/$ClientOrg/_apis/userentitlements?api-version=5.1-preview.2" $AzureDevOpsAuthenicationHeader = @{Authorization = 'Basic ' + [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(":$($PAT)")) }

Invoke-RestMethod -uri $URI -Method POST -Headers $AzureDevOpsAuthenicationHeader -Body $a -ContentType "application/json"


$a= ConvertFrom-JSON @'
{
  "accessLevel": {
      "licensingSource": "msdn",
      "accountLicenseType": "enterprise",
      "msdnLicenseType": "enterprise"
  },
  "extensions": [
    {
      "id": "ms.feed" 
    }
  ],
  "user": {
    "principalName": "[email protected]",
    "subjectKind": "user"
  },
  "projectEntitlements": [
    {
      "group": {
        "groupType": "projectAdministrator"
      },
      "projectRef": {
        "id": "0685a10e"
      }
    }
   ]
  }
'@



Invoke-RestMethod : {"$id":"1","innerException":null,"message":"Value cannot be null.\r\nParameter name: userEntitlement","typeName":"System.ArgumentNullException, 
mscorlib","typeKey":"ArgumentNullException","errorCode":0,"eventId":0}
At line:4 char:1
+ Invoke-RestMethod -uri $URI -Method POST -Headers $AzureDevOpsAutheni ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebException
    + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand

1 Answer 1

1

Your body should be the JSON string itself and not an object built from the JSON.

$a= @'
{
  "accessLevel": {
      "licensingSource": "msdn",
      "accountLicenseType": "enterprise",
      "msdnLicenseType": "enterprise"
  },
  "extensions": [
    {
      "id": "ms.feed" 
    }
  ],
  "user": {
    "principalName": "[email protected]",
    "subjectKind": "user"
  },
  "projectEntitlements": [
    {
      "group": {
        "groupType": "projectAdministrator"
      },
      "projectRef": {
        "id": "0685a10e"
      }
    }
   ]
  }
'@

Invoke-RestMethod -uri $URI -Method POST -Headers $AzureDevOpsAuthenicationHeader -Body $a -ContentType "application/json"
Sign up to request clarification or add additional context in comments.

6 Comments

I'am affraid it was the good result ! You got a JSON which is not what you expected, but which seems to be what retrun the WS.
I'm not sure it was. It says is success false and the user was not created. If I paste the same thing in Postman it creates the user no issue.
Something wrong with your Authentication, or your parameters. Can you edit your answer with the content of $uri
Done. I just tested the header with another json I know works from powershell. It seems to not be an issue.
I tested the code in my environment too and it works fine, @JPBlanc is right, the ConvertFrom-Json Converts a JSON-formatted string to a custom object or a hash table which is not needed in the script. You can accept it as the answer if the problem has been resolved. Thanks!
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.