Skip to content

Commit

Permalink
Merge pull request #3711 from NikCharlebois/AADApplication-Soft-Delet…
Browse files Browse the repository at this point in the history
…e-Support

AADApplication Soft Delete Support
  • Loading branch information
NikCharlebois authored Sep 22, 2023
2 parents 04fc80c + 812ea21 commit 9c84069
Show file tree
Hide file tree
Showing 4 changed files with 595 additions and 139 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@

# UNRELEASED

* AADApplication
* Added support for restoring soft deleted instances.
* O365OrgSettings
* Changes to how ToDo discrepencies are being fixed in the SET method.
* DEPENDENCIES
* Updated Microsoft.Graph to version 2.6.1.


# 1.23.920.2

* DEPENDENCIES
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,44 @@ function Set-TargetResource
$currentParameters.Remove('LogoutURL') | Out-Null
$currentParameters.Remove('Homepage') | Out-Null

$skipToUpdate = $false
$AppIdValue = $null
if ($Ensure -eq 'Present' -and $currentAADApp.Ensure -eq 'Absent')
{
# Before attempting to create a new instance, let's first check to see if there is already an existing instance that is soft deleted
if (-not [System.String]::IsNullOrEmpty($AppId))
{
Write-Verbose "Trying to retrieve existing deleted Applications from soft delete by Id {$AppId}."
[Array]$deletedApp = Get-MgBetaDirectoryDeletedItemAsApplication -DirectoryObjectId $AppId -ErrorAction SilentlyContinue
}

if ($null -eq $deletedApp)
{
Write-Verbose "Trying to retrieve existing deleted Applications from soft delete by DisplayName {$DisplayName}."
[Array]$deletedApp = Get-MgBetaDirectoryDeletedItemAsApplication -Filter "DisplayName eq '$DisplayName'" -ErrorAction SilentlyContinue
}

if ($null -ne $deletedApp -and $deletedApp.Length -eq 1)
{
$deletedSinceInDays = [System.DateTime]::Now.Subtract($deletedApp[0].DeletedDateTime).Days
if ($deletedSinceInDays -le 30)
{
Write-Verbose -Message "Found existing deleted instance of {$DisplayName}. Restoring it instead of creating a new one. This could take a few minutes to complete."
Restore-MgBetaDirectoryDeletedItem -DirectoryObjectId $deletedApp.Id
$skipToUpdate = $true
$AppIdValue = $deletedApp.Id
}
else
{
Write-Verbose -Message "Found existing deleted instance of {$DisplayName}. However, the deleted date was over days ago and it cannot be restored. Will recreate a new instance instead."
}
}
elseif ($deletedApp.Length -gt 1)
{
Write-Verbose -Message "Multiple instances of a deleted application with name {$DisplayName} wehre found. Creating a new instance since we can't determine what instance to restore."
}
}
if ($Ensure -eq 'Present' -and $currentAADApp.Ensure -eq 'Absent' -and -not $skipToUpdate)
{
Write-Verbose -Message "Creating New AzureAD Application {$DisplayName} with values:`r`n$($currentParameters | Out-String)"
$currentParameters.Remove('ObjectId') | Out-Null
Expand All @@ -441,14 +478,18 @@ function Set-TargetResource

}
# App should exist and will be configured to desired state
if ($Ensure -eq 'Present' -and $currentAADApp.Ensure -eq 'Present')
elseif (($Ensure -eq 'Present' -and $currentAADApp.Ensure -eq 'Present') -or $skipToUpdate)
{
$currentParameters.Remove('ObjectId') | Out-Null

$currentParameters.Add('ApplicationId', $currentAADApp.ObjectId)
if (-not $skipToUpdate)
{
$AppIdValue = $currentAADApp.ObjectId
}
$currentParameters.Add('ApplicationId', $AppIdValue)
Write-Verbose -Message "Updating existing AzureAD Application {$DisplayName} with values:`r`n$($currentParameters | Out-String)"
Update-MgApplication @currentParameters
$currentAADApp.Add('ID', $currentAADApp.ObjectId)
$currentAADApp.Add('ID', $AppIdValue)
$needToUpdatePermissions = $true
}
# App exists but should not
Expand Down Expand Up @@ -815,7 +856,7 @@ function Export-TargetResource

$ConnectionMode = New-M365DSCConnection -Workload 'MicrosoftGraph' `
-InboundParameters $PSBoundParameters

$dscContent = [System.Text.StringBuilder]::new()
$i = 1
Write-Host "`r`n" -NoNewline
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ Describe -Name $Global:DscHelper.DescribeHeader -Fixture {
Mock -CommandName Remove-MgApplication -MockWith {
}

Mock -CommandName MgBetaDirectoryDeletedItemAsApplication -MockWith {
}

Mock -CommandName New-MgApplication -MockWith {
return @{
ID = '12345-12345-12345-12345-12345'
Expand Down
Loading

0 comments on commit 9c84069

Please sign in to comment.