2

I am using PowerShell to remove a user from a Team in a project .Below is the code.

$AzureDevOpsPAT = "XXXXXXXXXXXXXXXXX"
$groupid = "vssgp.UyXXXXXXXXXXXXXXUxMjkzxx"
$memberid = "tester.deer@dmain.com"
$AzureDevOpsAuthenicationHeader = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(":$($AzureDevOpsPAT)"))

$saasheaders = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$saasheaders.Add("Authorization", "Basic $AzureDevOpsAuthenicationHeader")
$saasheaders.Add("Accept", "*/*")
$saasheaders.Add("Cache-Control", "no-cache")
$saasheaders.Add("Accept-Encoding", "gzip, deflate")
$saasheaders.Add("Content-Type", "application/json")

$result = Invoke-RestMethod "https://vsaex.dev.azure.com/mycollectionname/_apis/GroupEntitlements/$groupid/members/$memberid`?api-version=5.1-preview.1" -Method 'DELETE' -Headers $saasheaders 
Write-Host "value is $result"

Below is the error I get

Invoke-RestMethod : {"count":1,"value":{"Message":"The request is invalid."}}
+ ... result = Invoke-RestMethod "https://vsaex.dev.azure.com/myorganization/_apis ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebException
    + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand

I am following the documentation from this link

https://learn.microsoft.com/en-us/rest/api/azure/devops/memberentitlementmanagement/members/remove-member-from-group?view=azure-devops-rest-5.1#uri-parameters

Any suggestions what am I doing wrong ?

Joe_12345
  • 589
  • 2
  • 7
  • 19
  • 1
    Could you try to list the group entitlements once, before removing members by using this [REST API](https://learn.microsoft.com/en-us/rest/api/azure/devops/memberentitlementmanagement/group-entitlements/list?view=azure-devops-rest-5.1&tabs=HTTP) call? – Sridevi Jul 29 '23 at 05:01
  • @Sridevi - you are correct, I get no entitlements when using the above REST API link you referred. Wondering how I can remove the users from the Teams using REST API or Azure CLI. just an fyi - When I use the Azure Cli to remove a user , it hangs – Joe_12345 Aug 02 '23 at 13:32
  • Could you include CLI command you are running to remove user by editing your question? – Sridevi Aug 02 '23 at 13:39
  • @Sridevi - I have posted the Azure CLI command error in a different post, didnt want to derail this conversation - https://stackoverflow.com/questions/76820577/azure-cli-azure-devops-hangs-when-trying-to-remove-user-from-a-security-group – Joe_12345 Aug 02 '23 at 13:53

0 Answers0