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

2023-12-04

我正在尝试创建一个 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 帐户时,出现错误:运行 Login-AzureRmAccount 进行登录。

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


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

如果您确实需要 Powershell 脚本,那么事情就会变得棘手。 从以下位置获取 Powershell 代码https://stackoverflow.com/a/51848069/1548275作为基地。区别在于,如果您没有从扩展运行代码,则您没有Get-VstsInput nor Get-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 项目设置、管道的服务连接中以了解更新。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用 VSTS 任务创建 AD 应用程序 的相关文章

随机推荐