Skip to content

Commit 1ab4a65

Browse files
authored
Merge pull request #592 from KelvinTegelaar/dev
[pull] dev from KelvinTegelaar:dev
2 parents a28a50e + bc2981d commit 1ab4a65

File tree

6 files changed

+126
-32
lines changed

6 files changed

+126
-32
lines changed

.github/workflows/dev_api.yml

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,41 @@ jobs:
2626
with:
2727
persist-credentials: false
2828

29+
- name: Setup PowerShell module cache
30+
id: cacher
31+
uses: actions/cache@v3
32+
with:
33+
path: "~/.local/share/powershell/Modules"
34+
key: ${{ runner.os }}-ModuleBuilder
35+
36+
- name: Install ModuleBuilder
37+
if: steps.cacher.outputs.cache-hit != 'true'
38+
shell: pwsh
39+
run: |
40+
Set-PSRepository PSGallery -InstallationPolicy Trusted
41+
Install-Module ModuleBuilder -AllowClobber -Force
42+
43+
- name: Build CIPPCore Module
44+
shell: pwsh
45+
run: |
46+
$ModulePath = Join-Path $env:GITHUB_WORKSPACE "Modules/CIPPCore"
47+
$OutputPath = Join-Path $env:GITHUB_WORKSPACE "Output"
48+
49+
Write-Host "Building module from: $ModulePath"
50+
Write-Host "Output directory: $OutputPath"
51+
52+
# Build the module using ModuleBuilder
53+
Build-Module -SourcePath $ModulePath -OutputDirectory $OutputPath -Verbose
54+
55+
# Replace the source module with the built module
56+
Remove-Item -Path $ModulePath -Recurse -Force
57+
Copy-Item -Path (Join-Path $OutputPath "CIPPCore") -Destination $ModulePath -Recurse -Force
58+
59+
Write-Host "Module built and replaced successfully"
60+
61+
# Clean up output directory
62+
Remove-Item -Path $OutputPath -Recurse -Force
63+
2964
- name: Login to Azure
3065
uses: azure/login@v2
3166
with:

Modules/CIPPCore/CIPPCore.psm1

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
1-
$Public = @(Get-ChildItem -Path (Join-Path $PSScriptRoot "Public\*.ps1") -Recurse -ErrorAction SilentlyContinue)
2-
$Private = @(Get-ChildItem -Path (Join-Path $PSScriptRoot "Private\*.ps1") -Recurse -ErrorAction SilentlyContinue)
3-
$Functions = $Public + $Private
4-
foreach ($import in @($Functions)) {
5-
try {
6-
. $import.FullName
7-
} catch {
8-
Write-Error -Message "Failed to import function $($import.FullName): $_"
1+
# ModuleBuilder will concatenate all function files into this module
2+
# This block is only used when running from source (not built)
3+
if (Test-Path (Join-Path $PSScriptRoot 'Public')) {
4+
$Public = @(Get-ChildItem -Path (Join-Path $PSScriptRoot 'Public\*.ps1') -Recurse -ErrorAction SilentlyContinue)
5+
$Private = @(Get-ChildItem -Path (Join-Path $PSScriptRoot 'Private\*.ps1') -Recurse -ErrorAction SilentlyContinue)
6+
$Functions = $Public + $Private
7+
foreach ($import in @($Functions)) {
8+
try {
9+
. $import.FullName
10+
} catch {
11+
Write-Error -Message "Failed to import function $($import.FullName): $_"
12+
}
913
}
10-
}
1114

12-
Export-ModuleMember -Function $Public.BaseName -Alias *
15+
Export-ModuleMember -Function $Public.BaseName
16+
}

Modules/CIPPCore/CippCore.psd1

-914 Bytes
Binary file not shown.

Modules/CIPPCore/Public/New-CIPPBackupTask.ps1

Lines changed: 62 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,13 @@ function New-CIPPBackupTask {
55
$TenantFilter
66
)
77

8+
$FunctionStopwatch = [System.Diagnostics.Stopwatch]::StartNew()
9+
Write-Host "CIPPBACKUP: Starting backup task: $Task for tenant: $TenantFilter"
10+
811
$BackupData = switch ($Task) {
912
'CippCustomVariables' {
10-
Write-Host "Backing up Custom Variables for $TenantFilter"
13+
$TaskStopwatch = [System.Diagnostics.Stopwatch]::StartNew()
14+
Write-Host "CIPPBACKUP: Backing up Custom Variables for $TenantFilter"
1115
$ReplaceTable = Get-CIPPTable -tablename 'CippReplacemap'
1216

1317
# Get tenant-specific variables
@@ -20,40 +24,56 @@ function New-CIPPBackupTask {
2024
if ($TenantFilter -eq 'AllTenants') {
2125
$GlobalVariables = Get-CIPPAzDataTableEntity @ReplaceTable -Filter "PartitionKey eq 'AllTenants'"
2226
$AllVariables = @($TenantVariables) + @($GlobalVariables)
27+
$TaskStopwatch.Stop()
28+
Write-Host "CIPPBACKUP: CippCustomVariables backup completed in $($TaskStopwatch.Elapsed.TotalSeconds) seconds"
2329
$AllVariables
2430
} else {
31+
$TaskStopwatch.Stop()
32+
Write-Host "CIPPBACKUP: CippCustomVariables backup completed in $($TaskStopwatch.Elapsed.TotalSeconds) seconds"
2533
$TenantVariables
2634
}
2735
}
2836
'users' {
29-
Write-Host "Backup users for $TenantFilter"
37+
$TaskStopwatch = [System.Diagnostics.Stopwatch]::StartNew()
38+
Write-Host "CIPPBACKUP: Backup users for $TenantFilter"
3039
$Users = New-GraphGetRequest -uri 'https://graph.microsoft.com/beta/users?$top=999' -tenantid $TenantFilter | Select-Object * -ExcludeProperty mail, provisionedPlans, onPrem*, *passwordProfile*, *serviceProvisioningErrors*, isLicenseReconciliationNeeded, isManagementRestricted, isResourceAccount, *date*, *external*, identities, deletedDateTime, isSipEnabled, assignedPlans, cloudRealtimeCommunicationInfo, deviceKeys, provisionedPlan, securityIdentifier
3140
#remove the property if the value is $null
32-
$Users | ForEach-Object {
41+
$users = $Users | ForEach-Object {
3342
$_.psobject.properties | Where-Object { $null -eq $_.Value } | ForEach-Object {
3443
$_.psobject.properties.Remove($_.Name)
3544
}
3645
}
46+
$TaskStopwatch.Stop()
47+
Write-Host "CIPPBACKUP: Users backup completed in $($TaskStopwatch.Elapsed.TotalSeconds) seconds"
3748
$Users
49+
3850
}
3951
'groups' {
40-
Write-Host "Backup groups for $TenantFilter"
41-
New-GraphGetRequest -uri 'https://graph.microsoft.com/beta/groups?$top=999' -tenantid $TenantFilter
52+
$TaskStopwatch = [System.Diagnostics.Stopwatch]::StartNew()
53+
Write-Host "CIPPBACKUP: Backup groups for $TenantFilter"
54+
$Groups = New-GraphGetRequest -uri 'https://graph.microsoft.com/beta/groups?$top=999' -tenantid $TenantFilter
55+
$TaskStopwatch.Stop()
56+
Write-Host "CIPPBACKUP: Groups backup completed in $($TaskStopwatch.Elapsed.TotalSeconds) seconds"
57+
$Groups
4258
}
4359
'ca' {
44-
Write-Host "Backup Conditional Access Policies for $TenantFilter"
60+
$TaskStopwatch = [System.Diagnostics.Stopwatch]::StartNew()
61+
Write-Host "CIPPBACKUP: Backup Conditional Access Policies for $TenantFilter"
4562
$Policies = New-GraphGetRequest -uri 'https://graph.microsoft.com/beta/conditionalAccess/policies?$top=999' -tenantid $TenantFilter -AsApp $true
46-
Write-Host 'Creating templates for found Conditional Access Policies'
63+
Write-Host 'CIPPBACKUP: Creating templates for found Conditional Access Policies'
4764
foreach ($policy in $policies) {
4865
try {
4966
New-CIPPCATemplate -TenantFilter $TenantFilter -JSON $policy
5067
} catch {
5168
"Failed to create a template of the Conditional Access Policy with ID: $($policy.id). Error: $($_.Exception.Message)"
5269
}
5370
}
71+
$TaskStopwatch.Stop()
72+
Write-Host "CIPPBACKUP: Conditional Access backup completed in $($TaskStopwatch.Elapsed.TotalSeconds) seconds"
5473
}
5574
'intuneconfig' {
56-
Write-Host "Backup Intune Configuration Policies for $TenantFilter"
75+
$TaskStopwatch = [System.Diagnostics.Stopwatch]::StartNew()
76+
Write-Host "CIPPBACKUP: Backup Intune Configuration Policies for $TenantFilter"
5777
$GraphURLS = @("https://graph.microsoft.com/beta/deviceManagement/deviceConfigurations?`$select=id,displayName,lastModifiedDateTime,roleScopeTagIds,microsoft.graph.unsupportedDeviceConfiguration/originalEntityTypeName&`$expand=assignments&top=1000"
5878
'https://graph.microsoft.com/beta/deviceManagement/windowsDriverUpdateProfiles'
5979
"https://graph.microsoft.com/beta/deviceManagement/groupPolicyConfigurations?`$expand=assignments&top=999"
@@ -77,28 +97,37 @@ function New-CIPPBackupTask {
7797
}
7898
}
7999
} catch {
80-
Write-Host "Failed to backup $url"
100+
Write-Host "CIPPBACKUP: Failed to backup $url"
81101
}
82102
}
103+
$TaskStopwatch.Stop()
104+
Write-Host "CIPPBACKUP: Intune Configuration backup completed in $($TaskStopwatch.Elapsed.TotalSeconds) seconds"
83105
}
84106
'intunecompliance' {
85-
Write-Host "Backup Intune Configuration Policies for $TenantFilter"
107+
$TaskStopwatch = [System.Diagnostics.Stopwatch]::StartNew()
108+
Write-Host "CIPPBACKUP: Backup Intune Configuration Policies for $TenantFilter"
86109

87110
New-GraphGetRequest -uri 'https://graph.microsoft.com/beta/deviceManagement/deviceCompliancePolicies?$top=999' -tenantid $TenantFilter | ForEach-Object {
88111
New-CIPPIntuneTemplate -TenantFilter $TenantFilter -URLName 'deviceCompliancePolicies' -ID $_.ID
89112
}
113+
$TaskStopwatch.Stop()
114+
Write-Host "CIPPBACKUP: Intune Compliance backup completed in $($TaskStopwatch.Elapsed.TotalSeconds) seconds"
90115
}
91116

92117
'intuneprotection' {
93-
Write-Host "Backup Intune Configuration Policies for $TenantFilter"
118+
$TaskStopwatch = [System.Diagnostics.Stopwatch]::StartNew()
119+
Write-Host "CIPPBACKUP: Backup Intune Configuration Policies for $TenantFilter"
94120

95121
New-GraphGetRequest -uri 'https://graph.microsoft.com/beta/deviceAppManagement/managedAppPolicies?$top=999' -tenantid $TenantFilter | ForEach-Object {
96122
New-CIPPIntuneTemplate -TenantFilter $TenantFilter -URLName 'managedAppPolicies' -ID $_.ID
97123
}
124+
$TaskStopwatch.Stop()
125+
Write-Host "CIPPBACKUP: Intune Protection backup completed in $($TaskStopwatch.Elapsed.TotalSeconds) seconds"
98126
}
99127

100128
'antispam' {
101-
Write-Host "Backup Anti-Spam Policies for $TenantFilter"
129+
$TaskStopwatch = [System.Diagnostics.Stopwatch]::StartNew()
130+
Write-Host "CIPPBACKUP: Backup Anti-Spam Policies for $TenantFilter"
102131

103132
$Policies = New-ExoRequest -tenantid $Tenantfilter -cmdlet 'Get-HostedContentFilterPolicy' | Select-Object * -ExcludeProperty *odata*, *data.type*
104133
$Rules = New-ExoRequest -tenantid $Tenantfilter -cmdlet 'Get-HostedContentFilterRule' | Select-Object * -ExcludeProperty *odata*, *data.type*
@@ -116,11 +145,14 @@ function New-CIPPBackupTask {
116145
}
117146

118147
$JSON = @{ policies = $Policies; rules = $Rules } | ConvertTo-Json -Depth 10
148+
$TaskStopwatch.Stop()
149+
Write-Host "CIPPBACKUP: Anti-Spam backup completed in $($TaskStopwatch.Elapsed.TotalSeconds) seconds"
119150
$JSON
120151
}
121152

122153
'antiphishing' {
123-
Write-Host "Backup Anti-Phishing Policies for $TenantFilter"
154+
$TaskStopwatch = [System.Diagnostics.Stopwatch]::StartNew()
155+
Write-Host "CIPPBACKUP: Backup Anti-Phishing Policies for $TenantFilter"
124156

125157
$Policies = New-ExoRequest -tenantid $Tenantfilter -cmdlet 'Get-AntiPhishPolicy' | Select-Object * -ExcludeProperty *odata*, *data.type*
126158
$Rules = New-ExoRequest -tenantid $Tenantfilter -cmdlet 'Get-AntiPhishRule' | Select-Object * -ExcludeProperty *odata*, *data.type*
@@ -138,20 +170,33 @@ function New-CIPPBackupTask {
138170
}
139171

140172
$JSON = @{ policies = $Policies; rules = $Rules } | ConvertTo-Json -Depth 10
173+
$TaskStopwatch.Stop()
174+
Write-Host "CIPPBACKUP: Anti-Phishing backup completed in $($TaskStopwatch.Elapsed.TotalSeconds) seconds"
141175
$JSON
142176
}
143177

144178
'CippWebhookAlerts' {
145-
Write-Host "Backup Webhook Alerts for $TenantFilter"
179+
$TaskStopwatch = [System.Diagnostics.Stopwatch]::StartNew()
180+
Write-Host "CIPPBACKUP: Backup Webhook Alerts for $TenantFilter"
146181
$WebhookTable = Get-CIPPTable -TableName 'WebhookRules'
147-
Get-CIPPAzDataTableEntity @WebhookTable | Where-Object { $TenantFilter -in ($_.Tenants | ConvertFrom-Json).value }
182+
$WebhookData = Get-CIPPAzDataTableEntity @WebhookTable | Where-Object { $TenantFilter -in ($_.Tenants | ConvertFrom-Json).value }
183+
$TaskStopwatch.Stop()
184+
Write-Host "CIPPBACKUP: Webhook Alerts backup completed in $($TaskStopwatch.Elapsed.TotalSeconds) seconds"
185+
$WebhookData
148186
}
149187
'CippScriptedAlerts' {
150-
Write-Host "Backup Scripted Alerts for $TenantFilter"
188+
$TaskStopwatch = [System.Diagnostics.Stopwatch]::StartNew()
189+
Write-Host "CIPPBACKUP: Backup Scripted Alerts for $TenantFilter"
151190
$ScheduledTasks = Get-CIPPTable -TableName 'ScheduledTasks'
152-
Get-CIPPAzDataTableEntity @ScheduledTasks | Where-Object { $_.hidden -eq $true -and $_.command -like 'Get-CippAlert*' -and $TenantFilter -in $_.Tenant }
191+
$ScriptedAlerts = Get-CIPPAzDataTableEntity @ScheduledTasks | Where-Object { $_.hidden -eq $true -and $_.command -like 'Get-CippAlert*' -and $TenantFilter -in $_.Tenant }
192+
$TaskStopwatch.Stop()
193+
Write-Host "CIPPBACKUP: Scripted Alerts backup completed in $($TaskStopwatch.Elapsed.TotalSeconds) seconds"
194+
$ScriptedAlerts
153195
}
154196
}
197+
198+
$FunctionStopwatch.Stop()
199+
Write-Host "CIPPBACKUP: Total backup task completed in $($FunctionStopwatch.Elapsed.TotalSeconds) seconds"
155200
return $BackupData
156201
}
157202

Modules/CIPPCore/Public/New-CIPPCATemplate.ps1

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ function New-CIPPCATemplate {
1111
$NonEmptyProperties = $_.psobject.Properties | Where-Object { $null -ne $_.Value } | Select-Object -ExpandProperty Name
1212
$_ | Select-Object -Property $NonEmptyProperties
1313
}
14-
14+
$users = (New-GraphGetRequest -uri "https://graph.microsoft.com/beta/users?`$top=999&`$select=displayName,id" -tenantid $TenantFilter)
15+
$groups = (New-GraphGetRequest -uri "https://graph.microsoft.com/beta/groups?`$top=999&`$select=displayName,id" -tenantid $TenantFilter)
1516
$includelocations = New-Object System.Collections.ArrayList
1617
$IncludeJSON = foreach ($Location in $JSON.conditions.locations.includeLocations) {
1718
$locationinfo = New-GraphGetRequest -uri 'https://graph.microsoft.com/beta/identity/conditionalAccess/namedLocations' -tenantid $TenantFilter | Where-Object -Property id -EQ $location | Select-Object * -ExcludeProperty id, *time*
@@ -34,7 +35,7 @@ function New-CIPPCATemplate {
3435
$originalID = $_
3536
if ($_ -in 'All', 'None', 'GuestOrExternalUsers') { return $_ }
3637
try {
37-
(New-GraphGetRequest -uri "https://graph.microsoft.com/beta/users/$($_)" -tenantid $TenantFilter).displayName
38+
($users | Where-Object { $_.id -eq $_ }).displayName
3839
} catch {
3940
return $originalID
4041
}
@@ -47,7 +48,7 @@ function New-CIPPCATemplate {
4748
$originalID = $_
4849

4950
try {
50-
(New-GraphGetRequest -uri "https://graph.microsoft.com/beta/users/$($_)" -tenantid $TenantFilter).displayName
51+
($users | Where-Object { $_.id -eq $_ }).displayName
5152
} catch {
5253
return $originalID
5354
}
@@ -64,7 +65,7 @@ function New-CIPPCATemplate {
6465
$originalID = $_
6566
if ($_ -in 'All', 'None', 'GuestOrExternalUsers' -or -not (Test-IsGuid $_)) { return $_ }
6667
try {
67-
(New-GraphGetRequest -uri "https://graph.microsoft.com/beta/groups/$($_)" -tenantid $TenantFilter).displayName
68+
($groups | Where-Object { $_.id -eq $_ }).displayName
6869
} catch {
6970
return $originalID
7071
}
@@ -76,7 +77,7 @@ function New-CIPPCATemplate {
7677

7778
if ($_ -in 'All', 'None', 'GuestOrExternalUsers' -or -not (Test-IsGuid $_)) { return $_ }
7879
try {
79-
(New-GraphGetRequest -uri "https://graph.microsoft.com/beta/groups/$($_)" -tenantid $TenantFilter).displayName
80+
($groups | Where-Object { $_.id -eq $_ }).displayName
8081
} catch {
8182
return $originalID
8283

Modules/CIPPCore/build.psd1

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
@{
2+
Path = 'CIPPCore.psd1'
3+
OutputDirectory = '../../Output'
4+
VersionedOutputDirectory = $false
5+
CopyPaths = @()
6+
Encoding = 'UTF8'
7+
Prefix = $null
8+
Suffix = $null
9+
}

0 commit comments

Comments
 (0)