首页 文章

使用VSTS任务创建AD应用程序

提问于
浏览
2

我正在尝试创建一个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
}

当我使用服务主体作为服务 endpoints 用户时,我收到错误资源我找不到 .

当我使用自定义AD帐户时,出现错误:运行Login-AzureRmAccount进行登录 .

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

1 回答

  • 0

    如果您不需要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-VstsInput 也没有 Get-VstsEndpoint 可以执行 .

    此外,您没有AzureAD模块cmdlet可以运行 . 您需要获取Nuget-package,将其解压缩到您自己的repo中,并将其作为脚本的一部分,以便稍后在管道任务中使用 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'

    正如您所看到的,扩展可以访问所有三个,但是常规脚本(据我所知)没有 .

    网络中包含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项目设置,管道服务连接以了解更新 .

相关问题