powershell - 从 VSTS 版本访问 Azure 服务主体详细信息

标签 powershell security azure azure-pipelines-release-pipeline azure-devops-rest-api

虽然我们能够在 VSTS 中使用 Powershell Azure 任务进行发布,但有时我们也会运行 F# 脚本进行发布,作为其中的一部分,我们希望使用服务主体将 Assets 部署到 Azure。我们已经在 VSTS 中注册了 SP,Powershell 可以访问它 - 但是有没有办法,例如原始命令行等来获取租户 ID 等,以便我们可以手动使用它们?例如作为环境变量?

我们唯一的其他选择是手动将租户 ID 等作为构建变量复制到版本中,但这对我来说似乎是一个解决方法。

最佳答案

是的,您可以在自定义构建/发布步骤/任务中获取相关信息(例如租户 ID)。

更多关于构建扩展的信息,您可以引用:Add a build task .

如果您不知道如何实现,可以引用这些步骤来获取Azure PowerShell步骤/任务的所有源代码。

  1. 设置本地构建代理:Deploy an agent on Windows
  2. 创建构建/发布定义
  3. 添加 Azure PowerShell 步骤/任务并进行配置
  4. 将此构建/发布排队
  5. 登录构建代理计算机,检查 [代理文件夹]\tasks\AzurePowerShell 中的 Azure PowerShell 步骤/任务

简单的构建/发布步骤/任务扩展:

文件:

 AzureCustomTask

    Ps_modules (can be found in the Azure PowerShell step/task folder, refer to previous steps)

    Test.ps1

    Icon.png

    Task.json

Test.ps1代码:

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

 Write-Host $serviceName
        if (!$serviceName) {
            # Let the task SDK throw an error message if the input isn't defined.
            Get-VstsInput -Name $serviceNameInput -Require
        }

        $endpoint = Get-VstsEndpoint -Name $serviceName -Require

        Write-Host $endpoint.Auth.Parameters.TenantId

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."
    },
....

关于powershell - 从 VSTS 版本访问 Azure 服务主体详细信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42852179/

相关文章:

如果之后抛出错误,则数组的 Powershell 写输出丢失

arrays - 对于数组中的每个匹配,加一

powershell - 如何将动态参数传递给 powershell 脚本并迭代列表?

c# - 如何使用 Microsoft.Azure.Management.Fluent 列出资源组中的资源?

PowerShell 在同一窗口中执行外部命令

android - 保护来自 Android 应用程序的 API 调用

java - 这是允许通配符搜索用户的正确方法吗?

python - 使用质数生成随 secret 码

c# - Azure 服务总线主题 多个订阅者

azure - 使用适用于 Xamarin Android 的 Azure 移动服务从 Google Api 获取用户信息