Azure 开发运营 (TFS) |自动创建到 Linux 服务器的 SSH 服务连接

Azure DevOps (TFS) | create SSH service connection automatically to Linux Server

是否有人拥有创建 SSH 服务连接到 Linux 服务器并授予对所有管道的访问权限的 PowerShell 脚本?

或者,如果您可以提供清晰的 TFS CLI 程序来创建一个服务连接,这也会有所帮助,或者如果您可以将服务端点配置文件示例共享给 Linux

您可以编写一个脚本来调用 REAT API (Endpoints - Create for Azure DevOps Services) to create the service connection. For on-premiers Azure DevOps server : Endpoints - Create

然后调用 REST API 为端点授予管道权限。

https://dev.azure.com/{org}/{Project}/_apis/pipelines/pipelinePermissions/endpoint/{endpointID}?api-version=5.1-preview.1

以下PowerShell脚本供您参考:

Param(
   [string]$collectionurl = "https://dev.azure.com/{org}", #Azure DevOps services organization or on-premiers Azure DevOps Server Collection URL
   [string]$project = "ProjectName",
   [string]$user = "",
   [string]$token = "PAT"
)

# Base64-encodes the Personal Access Token (PAT) appropriately
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $user,$token)))
# Set your parameters and SSH service connection name

function CreateJsonBody
{

    $value = @"
    {
        "administratorsGroup": null,
        "authorization": {
            "scheme": "UsernamePassword",
            "parameters": {
                "username": "test",
                "password": "test"
            }
        },
        "createdBy": null,
        "data": {
            "Host": "172.17.16.69",
            "Port": "22",
            "PrivateKey": "xxxx"
        },
        "description": "SSH Test6",
        "groupScopeId": null,
        "name": "SSH Test6",
        "operationStatus": null,
        "readersGroup": null,
        "serviceEndpointProjectReferences": [
            {
                "description": "SSH Test",
                "name": "SSH Test",
                "projectReference": {
                    "id": "901f7f54-f0b1-40cb-8b6c-f82d590b0838",
                    "name": "Test0924"
                }
            }
        ],
        "type": "ssh",
        "url": "",
        "isShared": false,
        "owner": "library"
    }
"@

 return $value
}

$json = CreateJsonBody


# Create service connection 
$sshurl = "$collectionurl/$project/_apis/serviceendpoint/endpoints?api-version=5.1-preview.2" 
$result = Invoke-RestMethod -Uri $sshurl -Method POST -Body $json -ContentType "application/json" -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)}

$endpointid = $result.id

#Grant pipeline permissions

function CreateJsonBody
{

    $value = @"
    {
        "resource": {
            "id": "$endpointid",
            "type": "endpoint",
            "name": ""
        },
        "pipelines": [],
        "allPipelines": {
            "authorized": true,
            "authorizedBy": null,
            "authorizedOn": null
        }
    }
"@

 return $value
}

$json = CreateJsonBody
 
$permissionurl = "$collectionurl/$project/_apis/pipelines/pipelinePermissions/endpoint/$($endpointid)?api-version=5.1-preview.1" 
Write-Host "permissionurl:"$permission
Invoke-RestMethod -Uri $permissionurl -Method PATCH -Body $json -ContentType "application/json" -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)}