Skip to content

Commit 15babfd

Browse files
authored
Merge pull request #352 from KelvinTegelaar/dev
[pull] dev from KelvinTegelaar:dev
2 parents 4e1d9bb + 8bdf57c commit 15babfd

File tree

4 files changed

+128
-47
lines changed

4 files changed

+128
-47
lines changed

Modules/CIPPCore/Public/Add-CIPPScheduledTask.ps1

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ function Add-CIPPScheduledTask {
5959
if ($null -eq $Param -or $Param -eq '' -or ($Param | Measure-Object).Count -eq 0) {
6060
continue
6161
}
62-
if ($Param -is [System.Collections.IDictionary] -or $Param.Key) {
62+
if ($Param -is [System.Collections.IDictionary] -and $Param.Key) {
6363
$ht = @{}
6464
foreach ($p in $Param.GetEnumerator()) {
6565
$ht[$p.Key] = $p.Value
@@ -77,6 +77,9 @@ function Add-CIPPScheduledTask {
7777
$Parameters = ($Parameters | ConvertTo-Json -Depth 10 -Compress)
7878
$AdditionalProperties = [System.Collections.Hashtable]@{}
7979
foreach ($Prop in $task.AdditionalProperties) {
80+
if ($null -eq $Prop.Value -or $Prop.Value -eq '' -or ($Prop.Value | Measure-Object).Count -eq 0) {
81+
continue
82+
}
8083
$AdditionalProperties[$Prop.Key] = $Prop.Value
8184
}
8285
$AdditionalProperties = ([PSCustomObject]$AdditionalProperties | ConvertTo-Json -Compress)

Modules/CIPPCore/Public/Entrypoints/HTTP Functions/CIPP/Scheduler/Invoke-ListScheduledItems.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ function Invoke-ListScheduledItems {
109109
# Associate values to output bindings by calling 'Push-OutputBinding'.
110110
Push-OutputBinding -Name Response -Value ([HttpResponseContext]@{
111111
StatusCode = [HttpStatusCode]::OK
112-
Body = @($ScheduledTasks | Sort-Object -Property ExecutedTime -Descending)
112+
Body = @($ScheduledTasks | Sort-Object -Property ScheduledTime, ExecutedTime -Descending)
113113
})
114114

115115
}

Modules/CIPPCore/Public/Entrypoints/HTTP Functions/Tenant/Conditional/Invoke-ExecCAExclusion.ps1

Lines changed: 73 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
using namespace System.Net
22

3-
Function Invoke-ExecCAExclusion {
3+
function Invoke-ExecCAExclusion {
44
<#
55
.FUNCTIONALITY
66
Entrypoint
@@ -13,48 +13,87 @@ Function Invoke-ExecCAExclusion {
1313
$APIName = $Request.Params.CIPPEndpoint
1414
$Headers = $Request.Headers
1515
Write-LogMessage -headers $Headers -API $APIName -message 'Accessed this API' -Sev 'Debug'
16+
try {
17+
#If UserId is a guid, get the user's UPN
18+
$TenantFilter = $Request.Body.tenantFilter
19+
$UserID = $Request.Body.UserID
20+
$Username = $Request.Body.Username
21+
$Users = $Request.Body.Users
22+
$EndDate = $Request.Body.EndDate
23+
$PolicyId = $Request.Body.PolicyId
24+
$ExclusionType = $Request.Body.ExclusionType
1625

17-
#If UserId is a guid, get the user's UPN
18-
$TenantFilter = $Request.Body.tenantFilter
19-
$UserId = $Request.Body.UserID
20-
$EndDate = $Request.Body.EndDate
21-
$PolicyId = $Request.Body.PolicyId
22-
$ExclusionType = $Request.Body.ExclusionType
26+
if ($Users) {
27+
$UserID = $Users.value
28+
$Username = $Users.addedFields.userPrincipalName -join ', '
29+
} else {
30+
if ($UserID -match '^[a-f0-9]{8}-([a-f0-9]{4}-){3}[a-f0-9]{12}$' -and -not $Username) {
31+
$Username = (New-GraphGetRequest -uri "https://graph.microsoft.com/v1.0/users/$($UserID)" -tenantid $TenantFilter).userPrincipalName
32+
}
33+
}
2334

35+
$Policy = New-GraphGetRequest -uri "https://graph.microsoft.com/beta/identity/conditionalAccess/policies/$($PolicyId)?`$select=id,displayName" -tenantid $TenantFilter -asApp $true
2436

25-
if ($UserId -match '^[a-f0-9]{8}-([a-f0-9]{4}-){3}[a-f0-9]{12}$') {
26-
$Username = (New-GraphGetRequest -uri "https://graph.microsoft.com/v1.0/users/$($UserId)" -tenantid $TenantFilter).userPrincipalName
27-
}
28-
if ($Request.Body.vacation -eq 'true') {
29-
$StartDate = $Request.Body.StartDate
30-
$EndDate = $Request.Body.EndDate
31-
$TaskBody = [pscustomobject]@{
32-
TenantFilter = $TenantFilter
33-
Name = "Add CA Exclusion Vacation Mode: $Username - $($TenantFilter)"
34-
Command = @{
35-
value = 'Set-CIPPCAExclusion'
36-
label = 'Set-CIPPCAExclusion'
37-
}
38-
Parameters = [pscustomobject]@{
37+
if (-not $Policy) {
38+
throw "Policy with ID $PolicyId not found in tenant $TenantFilter."
39+
}
40+
41+
$PolicyName = $Policy.displayName
42+
if ($Request.Body.vacation -eq 'true') {
43+
$StartDate = $Request.Body.StartDate
44+
$EndDate = $Request.Body.EndDate
45+
46+
$Parameters = [PSCustomObject]@{
3947
ExclusionType = 'Add'
40-
UserID = $UserID
4148
PolicyId = $PolicyId
42-
UserName = $Username
4349
}
44-
ScheduledTime = $StartDate
50+
51+
if ($Users) {
52+
$Parameters | Add-Member -NotePropertyName Users -NotePropertyValue $Users
53+
} else {
54+
$Parameters | Add-Member -NotePropertyName UserID -NotePropertyValue $UserID
55+
}
56+
57+
$TaskBody = [pscustomobject]@{
58+
TenantFilter = $TenantFilter
59+
Name = "Add CA Exclusion Vacation Mode: $PolicyName"
60+
Command = @{
61+
value = 'Set-CIPPCAExclusion'
62+
label = 'Set-CIPPCAExclusion'
63+
}
64+
Parameters = [pscustomobject]$Parameters
65+
ScheduledTime = $StartDate
66+
}
67+
68+
Write-Information ($TaskBody | ConvertTo-Json -Depth 10)
69+
70+
Add-CIPPScheduledTask -Task $TaskBody -hidden $false
71+
#Removal of the exclusion
72+
$TaskBody.Parameters.ExclusionType = 'Remove'
73+
$TaskBody.Name = "Remove CA Exclusion Vacation Mode: $PolicyName"
74+
$TaskBody.ScheduledTime = $EndDate
75+
Add-CIPPScheduledTask -Task $TaskBody -hidden $false
76+
$body = @{ Results = "Successfully added vacation mode schedule for $Username." }
77+
} else {
78+
$Parameters = @{
79+
ExclusionType = $ExclusionType
80+
PolicyId = $PolicyId
81+
}
82+
if ($Users) {
83+
$Parameters.Users = $Users
84+
} else {
85+
$Parameters.UserID = $UserID
86+
}
87+
88+
Set-CIPPCAExclusion -TenantFilter $TenantFilter -Headers $Headers @Parameters
4589
}
46-
Add-CIPPScheduledTask -Task $TaskBody -hidden $false
47-
#Removal of the exclusion
48-
$TaskBody.Parameters.ExclusionType = 'Remove'
49-
$TaskBody.Name = "Remove CA Exclusion Vacation Mode: $Username - $($TenantFilter)"
50-
$TaskBody.ScheduledTime = $EndDate
51-
Add-CIPPScheduledTask -Task $TaskBody -hidden $false
52-
$body = @{ Results = "Successfully added vacation mode schedule for $Username." }
53-
} else {
54-
Set-CIPPCAExclusion -TenantFilter $TenantFilter -ExclusionType $ExclusionType -UserID $UserID -PolicyId $PolicyId -Headers $Headers -UserName $Username
90+
} catch {
91+
Write-Warning "Failed to perform exclusion for $Username : $($_.Exception.Message)"
92+
Write-Information $_.InvocationInfo.PositionMessage
93+
$body = @{ Results = "Failed to perform exclusion for $Username : $($_.Exception.Message)" }
94+
Write-LogMessage -headers $Headers -API 'Invoke-ExecCAExclusion' -message "Failed to perform exclusion for $Username : $_" -Sev 'Error' -tenant $TenantFilter -LogData (Get-CippException -Exception $_)
5595
}
5696

57-
5897
Push-OutputBinding -Name Response -Value ([HttpResponseContext]@{
5998
StatusCode = [HttpStatusCode]::OK
6099
Body = $Body

Modules/CIPPCore/Public/Set-CIPPCAExclusion.ps1

Lines changed: 50 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,39 +6,78 @@ function Set-CIPPCAExclusion {
66
$UserID,
77
$PolicyId,
88
$Username,
9+
$Users,
910
$Headers
1011
)
1112
try {
12-
$CheckExististing = New-GraphGETRequest -uri "https://graph.microsoft.com/beta/identity/conditionalAccess/policies/$($PolicyId)" -tenantid $TenantFilter -AsApp $true
13+
$CheckExisting = New-GraphGETRequest -uri "https://graph.microsoft.com/beta/identity/conditionalAccess/policies/$($PolicyId)" -tenantid $TenantFilter -AsApp $true
1314
if ($ExclusionType -eq 'add') {
14-
$NewExclusions = [pscustomobject]@{
15-
conditions = [pscustomobject]@{ users = [pscustomobject]@{
16-
excludeUsers = @($CheckExististing.conditions.users.excludeUsers + $UserID)
15+
if ($Users) {
16+
$Username = $Users.addedFields.userPrincipalName
17+
$ExcludeUsers = [System.Collections.Generic.List[string]]::new()
18+
foreach ($User in $CheckExisting.conditions.users.excludeUsers) {
19+
$ExcludeUsers.Add($User)
20+
}
21+
foreach ($User in $Users.value) {
22+
if ($ExcludeUsers -notcontains $User) {
23+
$ExcludeUsers.Add($User)
24+
}
25+
}
26+
$NewExclusions = [pscustomobject]@{
27+
conditions = [pscustomobject]@{ users = [pscustomobject]@{
28+
excludeUsers = $ExcludeUsers
29+
}
30+
}
31+
}
32+
} else {
33+
if ($UserID -match '^[a-f0-9]{8}-([a-f0-9]{4}-){3}[a-f0-9]{12}$') {
34+
$Username = (New-GraphGetRequest -uri "https://graph.microsoft.com/v1.0/users/$($UserID)" -tenantid $TenantFilter).userPrincipalName
35+
}
36+
$NewExclusions = [pscustomobject]@{
37+
conditions = [pscustomobject]@{ users = [pscustomobject]@{
38+
excludeUsers = @($CheckExisting.conditions.users.excludeUsers + $UserID)
39+
}
1740
}
1841
}
1942
}
43+
2044
$RawJson = ConvertTo-Json -Depth 10 -InputObject $NewExclusions
2145
if ($PSCmdlet.ShouldProcess($PolicyId, "Add exclusion for $UserID")) {
22-
New-GraphPOSTRequest -uri "https://graph.microsoft.com/beta/identity/conditionalAccess/policies/$($CheckExististing.id)" -tenantid $tenantfilter -type PATCH -body $RawJSON -AsApp $true
46+
New-GraphPOSTRequest -uri "https://graph.microsoft.com/beta/identity/conditionalAccess/policies/$($CheckExisting.id)" -tenantid $tenantfilter -type PATCH -body $RawJSON -AsApp $true
2347
}
2448
}
2549

2650
if ($ExclusionType -eq 'remove') {
51+
if ($Users) {
52+
$UserID = $Users.value
53+
$Username = $Users.addedFields.userPrincipalName
54+
} else {
55+
if ($UserID -match '^[a-f0-9]{8}-([a-f0-9]{4}-){3}[a-f0-9]{12}$') {
56+
$Username = (New-GraphGetRequest -uri "https://graph.microsoft.com/v1.0/users/$($UserID)" -tenantid $TenantFilter).userPrincipalName
57+
}
58+
$UserID = @($UserID)
59+
}
2760
$NewExclusions = [pscustomobject]@{
2861
conditions = [pscustomobject]@{ users = [pscustomobject]@{
29-
excludeUsers = @($CheckExististing.conditions.users.excludeUsers | Where-Object { $_ -ne $UserID })
62+
excludeUsers = @($CheckExisting.conditions.users.excludeUsers | Where-Object { $UserID -notcontains $_ })
3063
}
3164
}
3265
}
3366
$RawJson = ConvertTo-Json -Depth 10 -InputObject $NewExclusions
3467
if ($PSCmdlet.ShouldProcess($PolicyId, "Remove exclusion for $UserID")) {
35-
New-GraphPOSTRequest -uri "https://graph.microsoft.com/beta/identity/conditionalAccess/policies/$($CheckExististing.id)" -tenantid $tenantfilter -type PATCH -body $RawJSON -AsApp $true
68+
New-GraphPOSTRequest -uri "https://graph.microsoft.com/beta/identity/conditionalAccess/policies/$($CheckExisting.id)" -tenantid $tenantfilter -type PATCH -body $RawJSON -AsApp $true
3669
}
3770
}
38-
"Successfully performed $($ExclusionType) exclusion for $username from policy $($PolicyId)"
39-
Write-LogMessage -headers $Headers -API 'Set-CIPPConditionalAccessExclusion' -message "Successfully performed $($ExclusionType) exclusion for $username from policy $($PolicyId)" -Sev 'Info' -tenant $TenantFilter
71+
72+
foreach ($User in $Username) {
73+
"Successfully performed $($ExclusionType) exclusion for $User from policy $($CheckExisting.displayName)"
74+
Write-LogMessage -headers $Headers -API 'Set-CIPPCAExclusion' -message "Successfully performed $($ExclusionType) exclusion for $User from policy $($CheckExisting.displayName)" -Sev 'Info' -tenant $TenantFilter
75+
}
4076
} catch {
41-
"Failed to $($ExclusionType) user exclusion for $username from policy $($PolicyId): $($_.Exception.Message)"
42-
Write-LogMessage -headers $Headers -API 'Set-CIPPConditionalAccessExclusion' -message "Failed to $($ExclusionType) user exclusion for $username from policy $($PolicyId): $_" -Sev 'Error' -tenant $TenantFilter -LogData (Get-CippException -Exception $_)
77+
foreach ($User in $Username) {
78+
"Failed to $($ExclusionType) user exclusion for $User from policy $($CheckExisting.displayName): $($_.Exception.Message)"
79+
Write-LogMessage -headers $Headers -API 'Set-CIPPCAExclusion' -message "Failed to $($ExclusionType) user exclusion for $User from policy $($CheckExisting.displayName): $_" -Sev 'Error' -tenant $TenantFilter -LogData (Get-CippException -Exception $_)
80+
}
4381
}
4482
}
83+

0 commit comments

Comments
 (0)