简体   繁体   English

Azure DevOps-自定义任务-具有Azure身份验证的PowerShell

[英]Azure DevOps - Custom Task - PowerShell with Azure Authentification

So far I used the Azure PowerShell task to execute PowerShell scripts in an Azure Context ( https://docs.microsoft.com/en-us/azure/devops/pipelines/tasks/deploy/azure-powershell?view=vsts ). 到目前为止,我已经使用Azure PowerShell任务在Azure上下文中执行PowerShell脚本( https://docs.microsoft.com/en-us/azure/devops/pipelines/tasks/deploy/azure-powershell?view=vsts )。 Due to generalization efforts I want now to create a custom task (see eg http://www.donovanbrown.com/post/how-do-i-upload-a-custom-task-for-build ) that runs a PowerShell script in an Azure Context, ie that authenticates against a connection endpoint in Azure DevOps. 由于一般化的努力,我现在想创建一个运行PowerShell脚本的自定义任务(请参见例如http://www.donovanbrown.com/post/how-do-i-upload-a-custom-task-for-build )在Azure上下文中,即针对Azure DevOps中的连接终结点进行身份验证。

How can I achieve this? 我该如何实现?

First of all you need a service principal (see eg https://docs.microsoft.com/en-us/powershell/azure/create-azure-service-principal-azureps?view=azps-1.1.0 ) and a service connection (see eg https://docs.microsoft.com/en-us/azure/devops/pipelines/library/connect-to-azure?view=vsts ). 首先,您需要一个服务主体(请参见例如https://docs.microsoft.com/zh-cn/powershell/azure/create-azure-service-principal-azureps?view=azps-1.1.0 )和一个服务连接(请参见例如https://docs.microsoft.com/zh-cn/azure/devops/pipelines/library/connect-to-azure?view=vsts )。

In the custom task in task.json add an input to be able to select the service connection: task.json的自定义任务中,添加输入以能够选择服务连接:

"inputs": [
        {
            "name": "ConnectedServiceName",
            "type": "connectedService:AzureRM",
            "label": "Azure RM Subscription",
            "defaultValue": "",
            "required": true,
            "helpMarkDown": "Select the Azure Resource Manager subscription for the deployment."
        }
]

In the task (the powershell script) you get this input via 在任务(powershell脚本)中,您可以通过以下方式获得此输入

$serviceNameInput = Get-VstsInput -Name ConnectedServiceNameSelector -Default 'ConnectedServiceName'
$serviceName = Get-VstsInput -Name $serviceNameInput -Default (Get-VstsInput -Name DeploymentEnvironmentName)

Then authenticate: 然后验证:

try {
    $endpoint = Get-VstsEndpoint -Name $serviceName -Require
    if (!$endpoint) {
        throw "Endpoint not found..."
    }
    $subscriptionId = $endpoint.Data.SubscriptionId
    $tenantId = $endpoint.Auth.Parameters.TenantId
    $servicePrincipalId = $endpoint.Auth.Parameters.servicePrincipalId
    $servicePrincipalKey = $endpoint.Auth.Parameters.servicePrincipalKey

    $spnKey = ConvertTo-SecureString $servicePrincipalKey -AsPlainText -Force
    $credentials = New-Object System.Management.Automation.PSCredential($servicePrincipalId,$spnKey)

    Add-AzureRmAccount -ServicePrincipal -TenantId $tenantId -Credential $credentials
    Select-AzureRmSubscription -SubscriptionId $subscriptionId -Tenant $tenantId

    $ctx = Get-AzureRmContext
    Write-Host "Connected to subscription '$($ctx.Subscription)' and tenant '$($ctx.Tenant)'..."
} catch {
    Write-Host "Authentication failed: $($_.Exception.Message)..." 
}

Edit: 编辑:

It is useful to clear the context at the beginning respectively the end of the script. 清除脚本开头和结尾处的上下文很有用。 You can do that via 您可以通过

Clear-AzureRmContext -Scope Process
Disable-AzureRmContextAutosave

at the beginning and 在开始和

Disconnect-AzureRmAccount -Scope Process
Clear-AzureRmContext -Scope Process

at the end. 在末尾。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Azure powershell 任务中的 AzureRM 命令:Azure DevOps - AzureRM commands in Azure powershell Task: Azure DevOps 在 Azure powershell 任务中添加/获取 Azure Keyvault 的秘密 - Add/get secret to Azure Keyvault with Azure powershell task in Azure devops 如何在 Azure DevOps Azure Powershell 任务中启用 PowerShell 交互模式? - How to enable PowerShell Interactive mode in Azure DevOps Azure Powershell Task? PowerShell任务脚本路径Azure DevOps发布 - Script path of PowerShell task Azure DevOps release Azure Devops 任务版本 - Azure Devops task versions 在 Azure DevOps Powershell 管道任务中获取自己的服务主体名称 - Get own Service Principal Name in an Azure DevOps Powershell pipeline task 无法在 Azure Devops 中使用 PowerShell 任务获取令牌 - Unable to acquire token using PowerShell task in Azure Devops Azure Devops Powershell 任务:生成变量的编程访问 - Azure Devops Powershell task: Programmatic Access of Build Variables 是否可以在遵循幂等原则的 Azure DevOps 中创建 PowerShell 任务 - Is it possible to create PowerShell task in Azure DevOps which follow the principle of idempotent 是否可以在 Azure DevOps 的 Azure Powershell 任务中设置变量并将它们传递给另一个任务? - Is it possible to set variables in the Azure Powershell task in Azure DevOps and pass them to another task?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM