Skip to content

Commit 2ff906f

Browse files
Merge pull request #6414 from NikCharlebois/AADServicePrincipalFix
AADServicePrincipal Fix
2 parents a710a43 + 3821c2b commit 2ff906f

File tree

2 files changed

+37
-10
lines changed

2 files changed

+37
-10
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
* AADApplication
66
* Fixed an issue where not all properties were exported.
7+
* AADServicePrincipal
8+
* Fixes for retrieving instance by name.
79
* AADUser
810
* Added support for Mail attribute.
911
* EXOOrganizationConfig

Modules/Microsoft365DSC/DSCResources/MSFT_AADServicePrincipal/MSFT_AADServicePrincipal.psm1

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -156,12 +156,13 @@ function Get-TargetResource
156156
$nullReturn = $PSBoundParameters
157157
$nullReturn.Ensure = 'Absent'
158158

159+
$AADServicePrincipal = $null
159160
try
160161
{
161162
if (-not [System.String]::IsNullOrEmpty($ObjectID))
162163
{
163164
$AADServicePrincipal = Get-MgServicePrincipal -ServicePrincipalId $ObjectId `
164-
-Property $Script:PropertiesToExport
165+
-Property $Script:PropertiesToExport `
165166
-Expand 'AppRoleAssignedTo' `
166167
-ErrorAction Stop
167168
}
@@ -176,12 +177,12 @@ function Get-TargetResource
176177
$ObjectGuid = [System.Guid]::empty
177178
if (-not [System.Guid]::TryParse($AppId, [System.Management.Automation.PSReference]$ObjectGuid))
178179
{
179-
$appInstance = Get-MgApplication -Filter "DisplayName eq '$($AppId -replace "'", "''")'"
180-
if ($appInstance)
181-
{
182-
$AADServicePrincipal = Get-MgServicePrincipal -Filter "AppID eq '$($appInstance.AppId)'" `
180+
$AADServicePrincipal = [Array](Get-MgServicePrincipal -Filter "DisplayName eq '$($AppId -replace "'", "''")'" `
183181
-Property $Script:PropertiesToExport `
184-
-Expand 'AppRoleAssignedTo'
182+
-Expand 'AppRoleAssignedTo')
183+
if ($null -ne $AADServicePrincipal -and $AADServicePrincipal.Count -gt 1)
184+
{
185+
Throw "Multiple Service Principal with the DisplayName $($AppId) exist in the tenant."
185186
}
186187
}
187188
else
@@ -341,12 +342,36 @@ function Get-TargetResource
341342
$appIdToExport = $AADServicePrincipal.AppId
342343
}
343344

345+
$tagsValue = @()
346+
if ($null -ne $AADServicePrincipal.Tags)
347+
{
348+
$tagsValue = [Array]($AADServicePrincipal.Tags)
349+
}
350+
351+
$alternativeNamesValue = @()
352+
if ($null -ne $AADServicePrincipal.AlternativeNames)
353+
{
354+
$alternativeNamesValue = [Array]($AADServicePrincipal.AlternativeNames)
355+
}
356+
357+
$replyUrlsValue = @()
358+
if ($null -ne $AADServicePrincipal.ReplyURLs)
359+
{
360+
$replyUrlsValue = [Array]($AADServicePrincipal.ReplyURLs)
361+
}
362+
363+
$servicePrincipalNamesValue = @()
364+
if ($null -ne $AADServicePrincipal.ServicePrincipalNames)
365+
{
366+
$servicePrincipalNamesValue = [Array]($AADServicePrincipal.ServicePrincipalNames)
367+
}
368+
344369
$result = @{
345370
AppId = $appIdToExport
346371
AppRoleAssignedTo = $AppRoleAssignedToValues
347372
ObjectID = $AADServicePrincipal.Id
348373
DisplayName = $AADServicePrincipal.DisplayName
349-
AlternativeNames = $AADServicePrincipal.AlternativeNames
374+
AlternativeNames = $alternativeNamesValue
350375
AccountEnabled = [boolean]$AADServicePrincipal.AccountEnabled
351376
AppRoleAssignmentRequired = $AADServicePrincipal.AppRoleAssignmentRequired
352377
CustomSecurityAttributes = $complexCustomSecurityAttributes
@@ -358,11 +383,11 @@ function Get-TargetResource
358383
Owners = $ownersValues
359384
PreferredSingleSignOnMode = $AADServicePrincipal.PreferredSingleSignOnMode
360385
PublisherName = $AADServicePrincipal.PublisherName
361-
ReplyURLs = $AADServicePrincipal.ReplyURLs
386+
ReplyURLs = $replyUrlsValue
362387
SamlMetadataURL = $AADServicePrincipal.SamlMetadataURL
363-
ServicePrincipalNames = $AADServicePrincipal.ServicePrincipalNames
388+
ServicePrincipalNames = $servicePrincipalNamesValue
364389
ServicePrincipalType = $AADServicePrincipal.ServicePrincipalType
365-
Tags = $AADServicePrincipal.Tags
390+
Tags = $tagsValue
366391
KeyCredentials = $complexKeyCredentials
367392
PasswordCredentials = $complexPasswordCredentials
368393
Ensure = 'Present'

0 commit comments

Comments
 (0)