【问题标题】:Create AD application with VSTS task使用 VSTS 任务创建 AD 应用程序
【发布时间】:2017-06-07 19:31:09
【问题描述】:

我正在尝试创建一个 VSTS 任务,它应该创建一个 AD 应用程序。 以 DeployAzureResouceGroup 为例,我创建了以下脚本:

[CmdletBinding()]
param()

Trace-VstsEnteringInvocation $MyInvocation
Import-VstsLocStrings "$PSScriptRoot\Task.json"
$connectedServiceNameSelector = Get-VstsInput -Name "connectedServiceNameSelector" -Require
$connectedServiceName = Get-VstsInput -Name "connectedServiceName"
$connectedServiceNameClassic = Get-VstsInput -Name "connectedServiceNameClassic"
$domains = (Get-VstsInput -Name "domains").Split(";")
$appName = Get-VstsInput -Name "appName"

if($connectedServiceNameSelector -eq "ConnectedServiceNameClassic")
{
    $connectedServiceName = $connectedServiceNameClassic
    $action = $actionClassic
    $resourceGroupName = $cloudService
}

Import-Module $PSScriptRoot\ps_modules\VstsAzureHelpers_
Initialize-Azure

# Import the loc strings.
Import-VstsLocStrings -LiteralPath $PSScriptRoot/Task.json

# Import all the dlls and modules which have cmdlets we need
Import-Module "$PSScriptRoot\DeploymentUtilities\Microsoft.TeamFoundation.DistributedTask.Task.Deployment.Internal.psm1"
Import-Module "$PSScriptRoot\DeploymentUtilities\Microsoft.TeamFoundation.DistributedTask.Task.Deployment.dll"

# Load all dependent files for execution
. "$PSScriptRoot\Utility.ps1"


try
{
    Validate-AzurePowerShellVersion
    $azureUtility = Get-AzureUtility "$connectedServiceName"
    Write-Verbose "Loading $azureUtility"
    . "$PSScriptRoot\$azureUtility"
    Write-Output "test"
    Write-Output "Creating a new Application in AAD (App URI -)" -Verbose
    $azureAdApplication = New-AzureRmADApplication -DisplayName "test" -IdentifierUris "https://app.com" -HomePage "https://app.com"
    $appId = $azureAdApplication.ApplicationId
    Write-Output "Azure AAD Application creation completed successfully (Application Id: $appId)" -Verbose

    Write-Verbose "Completing Azure Resource Group Deployment Task" -Verbose
}
catch
{
    Write-TaskSpecificTelemetry "UNKNOWNDEP_Error"
    throw
}

当我将服务主体用作服务端点用户时,我收到错误资源我未找到。

当我使用自定义 AD 帐户时,出现错误:Run Login-AzureRmAccount to login。

我做错了什么?我怎样才能让这个脚本工作?

【问题讨论】:

标签: azure-powershell azure-pipelines-build-task


【解决方案1】:

如果您不需要 Powershell 脚本,请从 https://marketplace.visualstudio.com/items?itemName=RalphJansen.Azure-AD-Application-Management 安装 Azure AD 应用程序管理 扩展 您可以从管道 GUI 添加新任务以管理 AD 应用程序。

如果您确实需要 Powershell 脚本,那么事情就会变得棘手。 从https://stackoverflow.com/a/51848069/1548275 获取 Powershell 代码作为基础。不同之处在于,如果您没有从扩展程序运行代码,您将无法执行 Get-VstsInputGet-VstsEndpoint

此外,您没有要运行的 AzureAD 模块 cmdlet。您需要获取 Nuget 包,将其解压缩到您自己的存储库中,并将其作为脚本的一部分,以便稍后在管道任务中使用 Import-Module

最后,您需要一个用于 Graph API 的身份验证令牌。如扩展代码所示,您将需要 3 个变量:

  • $tenantId = (Get-AzureRmSubscription).TenantId
  • $clientId = (Get-AzureRmADServicePrincipal -DisplayName "Your Project Service Connection name from Azure AD App Registrations").ApplicationId.Guid
  • $clientSecret = 'hard-coded, reset SPN password'

如您所见,扩展程序可以访问所有三个,但常规脚本(据我所知)没有。

The Net 中介绍了 SPN 密码重置。简而言之,它是这样的:

$clientId = (Get-AzureRmADServicePrincipal -DisplayName "Your Project Service Connection name from Azure AD App Registrations").Id.Guid
$password = ConvertTo-SecureString –asplaintext –force "oh, this is very secret!"
New-AzureRmADSpCredential -ObjectId $clientId -Password $password

另外:将明文密码更新到 Azure DevOps 项目设置、管道服务连接以了解更新。

【讨论】:

  • 不错的脚本。您确实需要这 3 个属性。如果您创建一个新秘密,请指定结束日期,否则默认情况下(我认为)一年有效。另外,为了清楚起见,不要忘记删除过期的凭据。
  • 考虑到这个主题的复杂性,我写了一篇详细的博客文章我是如何从 Azure DevOps 管道任务中设置我的 AD 应用程序的,请阅读这里的文章blog.hqcodeshop.fi/archives/…
猜你喜欢
  • 2017-07-26
  • 1970-01-01
  • 2018-10-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-06-04
  • 2019-01-18
  • 1970-01-01
相关资源
最近更新 更多