Encountering an error while attempting to create an Azure DevOps service connection using PowerShell script(REST API).

Murarisetty Yamuna 80 Reputation points
2024-04-03T08:39:41.3266667+00:00

Currently working on creation of service connection of authentication type workload Identity federation using Azure DevOps REST API by using PS script.

Below is the script

$organization = "Organization"

$pat = "PersonalAccessToken"

$url = "https://dev.azure.com/$organization/_apis/serviceendpoint/endpoints?api-version=7.2-preview.4"

$jsonPayload = @{

type = "AzureRM"

name = "CONNECTION_NAME"

description = "CONNECTION_DESCRIPTION"

serviceEndpointProjectReferences = @(

    @{

        name = "CONNECTION_NAME"

        projectReference = @{

            id = "ADO_PROJECT_ID"

            name = "ADO_PROJECT_NAME"

        }

    }

)

data = @{

    environment = "AzureCloud"

    scopeLevel = "Subscription"

    creationMode = "Manual"

    subscriptionId = "AZ_SUBSCRIPTION_ID"

    subscriptionName = "AZ_SUBSCRIPTION_NAME"

}

authorization = @{

    scheme = "WorkloadIdentityFederation"

    parameters = @{

        serviceprincipalid = "CLIENT_ID"

        tenantid = "TENANT_ID"

    }

}
```} | ConvertTo-Json

$response = Invoke-RestMethod -Uri $url -Method Post -Headers @{

```powershell
Authorization = "Basic " + [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(":$pat"))

"Content-Type" = "application/json"
```} -Body $jsonPayload

Write-Output $response

Getting the following error:

Invoke-RestMethod : {"$id":"1","innerException":null,"message":"The guid specified for parameter projectId must not be Guid.Empty.\r\nParameter name: projectId","typeName":"System.ArgumentException, mscorlib","typeKey":"ArgumentException","errorCode":0,"eventId":0} At line:39 char:13 + $response = Invoke-RestMethod -Uri $url -Method Post -Headers @{ + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebException + FullyQualifiedErrorId :

The parameter projectID which is assigned correctly but still it is showing **The guid specified for parameter projectId must not be Guid.Empty.**

When I try to print the jsonPayload to know about what value is assigning to the projectID after it is convertToJson . So this is output   
"serviceEndpointProjectReferences":

 [ 

{ **"projectReference": "System.Collections.Hashtable",**  // here the projectID is not assigning 

"name": "managedI-sc" } 

],

So can you please help me with the resolution

Not Monitored
Not Monitored
Tag not monitored by Microsoft.
37,798 questions
{count} votes