如何使用 json/csv 文件中的数据创建 Azure DevOps 库变量组?

How to create Azure DevOps Library Variable Group using the data from json/csv file?

[1]有人可以帮助我了解如何使用 PowerShell 在 Azure DevOps 库中创建变量组吗?该文件位于 .json/.csv 文件中。这是我到目前为止所拥有的。我是 PowerShell 的新手,所以请原谅任何错误。

$file = get-content = C:\test\Variables.json
ForEach {
$name = $_.Variable;
$Issecret = $_.IsSecret;
$Value = $_.Value;

az pipelines variable-group create --name test -p $ProjectName --org $orgUrl --authorize --variables -$name -$issecret -value
}

我正在尝试将所有值导入 Azure DevOps 库。

Name                        Is Secret    Value
--------------------------  -----------  -------------------------------------------------------------------------------
Test1                       False        https://test1.com
Test2                       False        https://test2.com

您可以通过以下脚本从csv文件中获取数据:

$body=@{
  "variables"= @{};
  "type"= "Vsts";
  "name"= "TestVariableGroup1";
  "description"= "A test variable group"
}   

$employee_list = Import-Csv "D:\test\data.csv"
foreach ($employee in $employee_list){

$body.variables[$employee.name]=@{value=$setting.value; isSecret=$employee.isSecret}

}    

$body | ConvertTo-Json

然后可以使用rest api创建变量组。

请求url:

POST https://dev.azure.com/{organization}/_apis/distributedtask/variablegroups?api-version=6.0-preview.2

请求正文:

{
    "description": "xxxx",
    "name": "xxx",
    "providerData": null,
    "type": "Vsts",
    "variables": {"test1": {
        "isSecret": true,
        "value": "fortest1"
    },
    "test2": {
        "isSecret": true,
        "value": "fortest2"
    }}, 
    "variableGroupProjectReferences": [{
        "description": "xxxx",
        "name": "xxx",
        "projectReference": {
            "id": "projectId",
            "name": ""
        }
    }]
}

示例脚本:

$token = "PAT token"

$url = "https://dev.azure.com/{organization}/_apis/distributedtask/variablegroups?api-version=6.0-preview.2"

$token = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($token)"))

$JSON = @'
request body
'@

$response = Invoke-RestMethod -Uri $url -Headers @{Authorization = "Basic $token"} -Method Post -ContentType application/json -body $JSON