Skip to content
This repository was archived by the owner on Feb 24, 2021. It is now read-only.
This repository was archived by the owner on Feb 24, 2021. It is now read-only.

Add helper function or guidance for creation of Azure SP for test execution #5

Description

To configure the automated testing on a DSC Config, users need to set up an Azure SP and configure the settings produced in the AppVeyor.yml.

This process usually requires a few steps to perform. It might be a good idea to include some helper functions to assist with doing things like this.

A function like this:

<#
  .SYNOPSIS
  New-AzureServicePrincipal

  .DESCRIPTION
  This task creates an Azure Service Principal in Azure AD that will be used for all installation automation.
  This can only be run interactively as the Login-AzureRmAccount will pop up an interactive window for 
  the user to log in with.
  The output of this task can be used to deploy the application in future and should be stored in each contributors AppVeyor account.
#>
[CmdletBinding()]
param
(
  [Parameter()]
  [System.String]
  $Name = 'DSCConfigurationTest',

  [Parameter(Mandatory = $true)]
  [System.String]
  $SubscriptionId,

  [Parameter(Mandatory = $true)]
  [System.String]
  $ADDomain,

  [Parameter(Mandatory = $true)]
  [SecureString]
  $ApplicationPassword
)

if ($SubscriptionId) {
  $account = Login-AzureRmAccount -SubscriptionId $SubscriptionId
} else {
  $account = Login-AzureRmAccount
}

Write-Host -Object "Creating '$Name' Service Principal in Azure AD"
$app = New-AzureRmADApplication `
  -DisplayName $Name `
  -HomePage "https://$ADDomain/$Name" `
  -IdentifierUris "https://$ADDomain/$Name" `
  -Password $ApplicationPassword
Write-Host -Object "Creating Azure AD Service Principal for ApplicationId '$($app.ApplicationId)'"
$null = New-AzureRmADServicePrincipal `
  -ApplicationId $app.ApplicationId
Write-Host -Object "Assigning role Contributor to AD Service Principal for ApplicationId '$($app.ApplicationId)'"

$roleAssignment = $null
$retryCount = 0

while (-not $roleAssignment -and ($retryCount -lt 10)) {
  try {
    $roleAssignment = New-AzureRmRoleAssignment `
      -RoleDefinitionName Contributor `
      -ServicePrincipalName $app.ApplicationId `
      -ErrorAction SilentlyContinue
  } catch {
    Write-Host -Object "Error assigning role Contributor to AD Service Principal for ApplicationId '$($app.ApplicationId)'. Retrying in 5 seconds..."
    Start-Sleep -Seconds 5
    $retryCount++
  }
} # while

if (-not $roleAssignment) {
  Write-Error -Message "Failed assigning role Contributor to AD Service Principal for ApplicationId '$($app.ApplicationId)'."
  return
}

Write-Host -Object "'$Name' service principal has been created."
Write-Host -Object "ApplicationID is '$($app.ApplicationId)'."
Write-Host -Object "SubscriptionID is '$SubscriptionId'."
Write-Host -Object "TenantID of '$($account.Context.Tenant.TenantId)'."

return [PSObject] @{
  ApplicationID  = $app.ApplicationId
  SubscriptionID = $SubscriptionId
  TenantID       = $account.Context.Tenant.TenantId
}

This could possibly be added to TestHelper.psm1 in DscConfiguration.Tests

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions