Skip to content

Commit

Permalink
added backups
Browse files Browse the repository at this point in the history
  • Loading branch information
KelvinTegelaar committed Jul 4, 2024
1 parent bd91705 commit d633ad7
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ Function Invoke-ListScheduledItems {
$HiddenTasks = $true
}
$Tasks = Get-CIPPAzDataTableEntity @Table -Filter "PartitionKey eq 'ScheduledTask'" | Where-Object { $_.Hidden -ne $HiddenTasks }
if ($Request.Query.Type) {
$tasks.Command
$Tasks = $Tasks | Where-Object { $_.command -eq $Request.Query.Type }
}

$AllowedTenants = Test-CIPPAccess -Request $Request -TenantList
if ($AllowedTenants -notcontains 'AllTenants') {
$Tasks = $Tasks | Where-Object -Property TenantId -In $AllowedTenants
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Function Invoke-ListAlertsQueue {
$WebhookRules = Get-CIPPAzDataTableEntity @WebhookTable

$ScheduledTasks = Get-CIPPTable -TableName 'ScheduledTasks'
$ScheduledTasks = Get-CIPPAzDataTableEntity @ScheduledTasks | Where-Object { $_.hidden -eq $true }
$ScheduledTasks = Get-CIPPAzDataTableEntity @ScheduledTasks | Where-Object { $_.hidden -eq $true -and $_.command -like 'Get-CippAlert*' }

$AllowedTenants = Test-CIPPAccess -Request $Request -TenantList
$TenantList = Get-Tenants -IncludeErrors
Expand Down
52 changes: 22 additions & 30 deletions Modules/CIPPCore/Public/New-CIPPBackup.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ function New-CIPPBackup {
$backupType,
$StorageOutput = 'default',
$TenantFilter,
$ScheduledBackupValues,
$APIName = 'CIPP Backup',
$ExecutingUser
)
Expand Down Expand Up @@ -50,36 +51,27 @@ function New-CIPPBackup {
}

#If Backup type is ConditionalAccess, create Conditional Access backup.
'ConditionalAccess' {
$ConditionalAccessPolicyOutput = New-GraphGetRequest -uri 'https://graph.microsoft.com/beta/identity/conditionalAccess/policies' -tenantid $tenantfilter
$AllNamedLocations = New-GraphGetRequest -uri 'https://graph.microsoft.com/beta/identity/conditionalAccess/namedLocations' -tenantid $tenantfilter
switch ($StorageOutput) {
'default' {
[PSCustomObject]@{
ConditionalAccessPolicies = $ConditionalAccessPolicyOutput
NamedLocations = $AllNamedLocations
}
}
'table' {
#Store output in tablestorage for Recovery
$RowKey = $TenantFilter + '_' + (Get-Date).ToString('yyyy-MM-dd-HHmm')
$entity = [PSCustomObject]@{
PartitionKey = 'ConditionalAccessBackup'
RowKey = $RowKey
TenantFilter = $TenantFilter
Policies = [string]($ConditionalAccessPolicyOutput | ConvertTo-Json -Compress -Depth 10)
NamedLocations = [string]($AllNamedLocations | ConvertTo-Json -Compress -Depth 10)
}
$Table = Get-CippTable -tablename 'ConditionalAccessBackup'
try {
$Result = Add-CIPPAzDataTableEntity @Table -entity $entity -Force
Write-LogMessage -user $request.headers.'x-ms-client-principal' -API $APINAME -message 'Created backup for Conditional Access Policies' -Sev 'Debug'
$Result
} catch {
Write-LogMessage -user $request.headers.'x-ms-client-principal' -API $APINAME -message "Failed to create backup for Conditional Access Policies: $($_.Exception.Message)" -Sev 'Error'
[pscustomobject]@{'Results' = "Backup Creation failed: $($_.Exception.Message)" }
}
}
'Scheduled' {
#Do a sub switch here based on the ScheduledBackupValues?
#Store output in tablestorage for Recovery
$RowKey = $TenantFilter + '_' + (Get-Date).ToString('yyyy-MM-dd-HHmm')
$entity = @{
PartitionKey = 'ScheduledBackup'
RowKey = $RowKey
TenantFilter = $TenantFilter
}
foreach ($ScheduledBackup in $ScheduledBackupValues.psobject.Properties.Name) {
$entity[$ScheduledBackup] = New-CIPPBackupTask -Task $ScheduledBackup -TenantFilter $TenantFilter
}

$Table = Get-CippTable -tablename 'ScheduledBackup'
try {
$Result = Add-CIPPAzDataTableEntity @Table -entity $entity -Force
Write-LogMessage -user $request.headers.'x-ms-client-principal' -API $APINAME -message 'Created backup for Conditional Access Policies' -Sev 'Debug'
$Result
} catch {
Write-LogMessage -user $request.headers.'x-ms-client-principal' -API $APINAME -message "Failed to create backup for Conditional Access Policies: $($_.Exception.Message)" -Sev 'Error'
[pscustomobject]@{'Results' = "Backup Creation failed: $($_.Exception.Message)" }
}
}

Expand Down
48 changes: 48 additions & 0 deletions Modules/CIPPCore/Public/New-CIPPBackupTask.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
function New-CIPPBackupTask {
[CmdletBinding()]
param (
$ScheduledBackup,
$TenantFilter
)

$BackupData = switch ($ScheduledBackup) {
'users' {
New-GraphGetRequest -uri 'https://graph.microsoft.com/beta/users?$top=999' -tenantid $TenantFilter
}
'groups' {
New-GraphGetRequest -uri 'https://graph.microsoft.com/beta/groups?$top=999' -tenantid $TenantFilter
}
'ca' {
New-GraphGetRequest -uri 'https://graph.microsoft.com/beta/conditionalAccess/policies?$top=999' -tenantid $TenantFilter
}
'namedlocations' {
New-GraphGetRequest -uri 'https://graph.microsoft.com/beta/conditionalAccess/namedLocations?$top=999' -tenantid $TenantFilter
}
'authstrengths' {
New-GraphGetRequest -uri 'https://graph.microsoft.com/beta/conditionalAccess/authenticationStrength/policies' -tenantid $TenantFilter
}
'intuneconfig' {
#alert
}
'intunecompliance' {}

'intuneprotection' {}

'CippWebhookAlerts' {
$WebhookTable = Get-CIPPTable -TableName 'WebhookRules'
Get-CIPPAzDataTableEntity @WebhookTable | Where-Object { $TenantFilter -in ($_.Tenants | ConvertFrom-Json).fullvalue.defaultDomainName }
}
'CippScriptedAlerts' {
$ScheduledTasks = Get-CIPPTable -TableName 'ScheduledTasks'
Get-CIPPAzDataTableEntity @ScheduledTasks | Where-Object { $_.hidden -eq $true -and $_.command -like 'Get-CippAlert*' -and $TenantFilter -in $_.Tenant }
}
'CippStandards' {
$Table = Get-CippTable -tablename 'standards'
$Filter = "PartitionKey eq 'standards' and RowKey eq '$($TenantFilter)'"
(Get-CIPPAzDataTableEntity @Table -Filter $Filter)
}

}
return $BackupData
}

0 comments on commit d633ad7

Please sign in to comment.