Skip to content

Commit

Permalink
Merge branch 'dev' of https://github.com/johnduprey/CIPP-API into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnDuprey committed Nov 13, 2024
2 parents 5c63df3 + 8a04b30 commit 6362279
Show file tree
Hide file tree
Showing 20 changed files with 318 additions and 159 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ function Get-CIPPAlertSharepointQuota {
}
$UsedStoragePercentage = [int](($sharepointQuota.GeoUsedStorageMB / $sharepointQuota.TenantStorageMB) * 100)
if ($UsedStoragePercentage -gt $Value) {
$AlertData = "SharePoint Storage is at $($UsedStoragePercentage)%. Your alert threshold is $($Value)%"
$AlertData = "SharePoint Storage is at $($UsedStoragePercentage)% [$([math]::Round($sharepointQuota.GeoUsedStorageMB / 1024, 2)) GB/$([math]::Round($sharepointQuota.TenantStorageMB / 1024, 2)) GB]. Your alert threshold is $($Value)%"
Write-AlertTrace -cmdletName $MyInvocation.MyCommand -tenantFilter $TenantFilter -data $AlertData
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Function Invoke-AddRoomMailbox {

$APIName = $TriggerMetadata.FunctionName
$User = $request.headers.'x-ms-client-principal'

Write-LogMessage -user $User -API $APINAME -message 'Accessed this API' -Sev 'Debug'

# Write to the Azure Functions log stream.
Expand All @@ -20,6 +21,7 @@ Function Invoke-AddRoomMailbox {

$Results = [System.Collections.Generic.List[Object]]::new()
$MailboxObject = $Request.body
$Tenant = $MailboxObject.tenantid
$AddRoomParams = [pscustomobject]@{
Name = $MailboxObject.username
DisplayName = $MailboxObject.displayName
Expand All @@ -30,30 +32,30 @@ Function Invoke-AddRoomMailbox {
}
# Interact with query parameters or the body of the request.
try {
$AddRoomRequest = New-ExoRequest -tenantid $($MailboxObject.tenantid) -cmdlet 'New-Mailbox' -cmdparams $AddRoomParams
$AddRoomRequest = New-ExoRequest -tenantid $Tenant -cmdlet 'New-Mailbox' -cmdparams $AddRoomParams
$Results.Add("Successfully created room: $($MailboxObject.DisplayName).")
Write-LogMessage -user $User -API $APINAME -tenant $($MailboxObject.tenantid) -message "Created room $($MailboxObject.DisplayName) with id $($AddRoomRequest.id)" -Sev 'Info'
Write-LogMessage -user $User -API $APINAME -tenant $Tenant -message "Created room $($MailboxObject.DisplayName) with id $($AddRoomRequest.id)" -Sev 'Info'

# Block sign-in for the mailbox
try {
$Request = Set-CIPPSignInState -userid $AddRoomRequest.ExternalDirectoryObjectId -TenantFilter $($MailboxObject.tenantid) -APIName $APINAME -ExecutingUser $User -AccountEnabled $false
$Request = Set-CIPPSignInState -userid $AddRoomRequest.ExternalDirectoryObjectId -TenantFilter $Tenant -APIName $APINAME -ExecutingUser $User -AccountEnabled $false
$Results.add("Blocked sign-in for Room mailbox; $($MailboxObject.userPrincipalName)")
} catch {
$ErrorMessage = Get-NormalizedError -Message $_.Exception.Message
$Results.add("Failed to block sign-in for Room mailbox: $($MailboxObject.userPrincipalName). Error: $ErrorMessage")
$ErrorMessage = Get-CippException -Exception $_
$Results.add("Failed to block sign-in for Room mailbox: $($MailboxObject.userPrincipalName). Error: $($ErrorMessage.NormalizedError)")
}

$StatusCode = [HttpStatusCode]::OK
} catch {
$ErrorMessage = Get-CippException -Exception $_
Write-LogMessage -user $User -API $APINAME -tenant $($MailboxObject.tenantid) -message "Failed to create room: $($MailboxObject.DisplayName). Error: $($ErrorMessage.NormalizedError)" -Sev 'Error' -LogData $ErrorMessage
Write-LogMessage -user $User -API $APINAME -tenant $Tenant -message "Failed to create room: $($MailboxObject.DisplayName). Error: $($ErrorMessage.NormalizedError)" -Sev 'Error' -LogData $ErrorMessage
$Results.Add("Failed to create Room mailbox $($MailboxObject.userPrincipalName). $($ErrorMessage.NormalizedError)")
$StatusCode = [HttpStatusCode]::Forbidden
}


$Body = [pscustomobject] @{ 'Results' = @($Results) }
# Associate values to output bindings by calling 'Push-OutputBinding'.
Push-OutputBinding -Name Response -Value ([HttpResponseContext]@{
StatusCode = [HttpStatusCode]::OK
StatusCode = $StatusCode
Body = $Body
})
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@ Function Invoke-AddSharedMailbox {

Write-LogMessage -user $User -API $APINAME -message 'Accessed this API' -Sev 'Debug'

# Write to the Azure Functions log stream.
Write-Host 'PowerShell HTTP trigger function processed a request.'

$Results = [System.Collections.ArrayList]@()
$MailboxObject = $Request.body
$Tenant = $MailboxObject.tenantid
$Aliases = $MailboxObject.addedAliases -Split '\n'

# Write to the Azure Functions log stream.
Write-Host 'PowerShell HTTP trigger function processed a request.'
try {

$Email = "$($MailboxObject.username)@$($MailboxObject.domain)"
Expand All @@ -30,48 +32,51 @@ Function Invoke-AddSharedMailbox {
'primarySMTPAddress' = $Email
Shared = $true
}
$AddSharedRequest = New-ExoRequest -tenantid $MailboxObject.tenantid -cmdlet 'New-Mailbox' -cmdparams $BodyToShip
$AddSharedRequest = New-ExoRequest -tenantid $Tenant -cmdlet 'New-Mailbox' -cmdparams $BodyToShip
$Body = $Results.add("Successfully created shared mailbox: $Email.")
Write-LogMessage -user $User -API $APINAME -tenant $($MailboxObject.tenantid) -message "Created shared mailbox $($MailboxObject.displayname) with email $Email" -Sev 'Info'
Write-LogMessage -user $User -API $APINAME -tenant $Tenant -message "Created shared mailbox $($MailboxObject.displayname) with email $Email" -Sev 'Info'

# Block sign-in for the mailbox
try {
$null = Set-CIPPSignInState -userid $AddSharedRequest.ExternalDirectoryObjectId -TenantFilter $($MailboxObject.tenantid) -APIName $APINAME -ExecutingUser $User -AccountEnabled $false
$null = Set-CIPPSignInState -userid $AddSharedRequest.ExternalDirectoryObjectId -TenantFilter $Tenant -APIName $APINAME -ExecutingUser $User -AccountEnabled $false
$Body = $Results.add("Blocked sign-in for shared mailbox $Email")
} catch {
$ErrorMessage = Get-NormalizedError -Message $_.Exception.Message
$Body = $Results.add("Failed to block sign-in for shared mailbox $Email. Error: $ErrorMessage")
$ErrorMessage = Get-CippException -Exception $_
Write-LogMessage -user $User -API $APINAME -tenant $Tenant -message "Failed to block sign-in for shared mailbox $Email. Error: $($ErrorMessage.NormalizedError)" -Sev 'Error' -LogData $ErrorMessage
$Body = $Results.add("Failed to block sign-in for shared mailbox $Email. Error: $($ErrorMessage.NormalizedError)")
}

} catch {
$ErrorMessage = Get-NormalizedError -Message $_.Exception.Message
Write-LogMessage -user $User -API $APINAME -tenant $($MailboxObject.tenantid) -message "Failed to create shared mailbox. Error: $ErrorMessage" -Sev 'Error'
$Body = $Results.add("Failed to create Shared Mailbox. $ErrorMessage")
}
# Add aliases to the mailbox if any are provided
if ($Aliases) {
try {
Start-Sleep 3 # Sleep since there is apparently a race condition with the mailbox creation if we don't delay for a lil bit
$AliasBodyToShip = [pscustomobject] @{
'Identity' = $AddSharedRequest.Guid
'EmailAddresses' = @{'@odata.type' = '#Exchange.GenericHashTable'; Add = $Aliases }
}
$null = New-ExoRequest -tenantid $Tenant -cmdlet 'Set-Mailbox' -cmdparams $AliasBodyToShip -UseSystemMailbox $true
Write-LogMessage -user $User -API $APINAME -tenant $Tenant -message "Added aliases to $Email : $($Aliases -join ',')" -Sev 'Info'
$Body = $results.add("Added Aliases to $Email : $($Aliases -join ',')")

# Add aliases to the mailbox if any are provided
if ($Aliases) {
try {
Start-Sleep 3 # Sleep since there is apparently a race condition with the mailbox creation if we don't delay for a lil bit
$AliasBodyToShip = [pscustomobject] @{
'Identity' = $AddSharedRequest.Guid
'EmailAddresses' = @{'@odata.type' = '#Exchange.GenericHashTable'; Add = $Aliases }
} catch {
$ErrorMessage = Get-CippException -Exception $_
Write-LogMessage -user $User -API $APINAME -tenant $Tenant -message "Failed to add aliases to $Email : $($ErrorMessage.NormalizedError)" -Sev 'Error' -LogData $ErrorMessage
$Body = $results.add("ERROR: Failed to add aliases to $Email : $($ErrorMessage.NormalizedError)")
}
$null = New-ExoRequest -tenantid $MailboxObject.tenantid -cmdlet 'Set-Mailbox' -cmdparams $AliasBodyToShip -UseSystemMailbox $true
Write-LogMessage -user $User -API $APINAME -tenant $($MailboxObject.tenantid) -message "Added aliases to $Email : $($Aliases -join ',')" -Sev 'Info'
$Body = $results.add("Added Aliases to $Email : $($Aliases -join ',')")

} catch {
$ErrorMessage = Get-NormalizedError -Message $_.Exception.Message
Write-LogMessage -user $User -API $APINAME -tenant $($MailboxObject.tenantid) -message "Failed to add aliases to $Email : $ErrorMessage" -Sev 'Error'
$Body = $results.add("ERROR: Failed to add aliases to $Email : $ErrorMessage")
}
$StatusCode = [HttpStatusCode]::OK
} catch {
$ErrorMessage = Get-CippException -Exception $_
Write-LogMessage -user $User -API $APINAME -tenant $Tenant -message "Failed to create shared mailbox. Error: $($ErrorMessage.NormalizedError)" -Sev 'Error' -LogData $ErrorMessage
$Body = $Results.add("Failed to create Shared Mailbox. $($ErrorMessage.NormalizedError)")
$StatusCode = [HttpStatusCode]::Forbidden
}


$Body = [pscustomobject] @{ 'Results' = @($results) }
# Associate values to output bindings by calling 'Push-OutputBinding'.
Push-OutputBinding -Name Response -Value ([HttpResponseContext]@{
StatusCode = [HttpStatusCode]::OK
StatusCode = $StatusCode
Body = $Body
})

Expand Down
2 changes: 1 addition & 1 deletion Modules/CIPPCore/Public/GraphHelper/New-ExoRequest.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ function New-ExoRequest {
Method = 'POST'
Body = $ExoBody
Headers = $Headers
ContentType = 'application/json'
ContentType = 'application/json; charset=utf-8'
}

$Return = Invoke-RestMethod @ExoRequestParams -ResponseHeadersVariable ResponseHeaders
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,14 @@ function Invoke-CIPPStandardActivityBasedTimeout {
# Backwards compatibility for v5.7.0 and older
if ($null -eq $Settings.timeout ) { $Settings.timeout = '01:00:00' }

$State = New-GraphGetRequest -Uri 'https://graph.microsoft.com/beta/policies/activityBasedTimeoutPolicies' -tenantid $tenant
$StateIsCorrect = $State.definition -like "*$($Settings.timeout)*"
$CurrentState = New-GraphGetRequest -Uri 'https://graph.microsoft.com/beta/policies/activityBasedTimeoutPolicies' -tenantid $tenant
$StateIsCorrect = if ($CurrentState.definition -like "*$($Settings.timeout)*") { $true } else { $false }

If ($Settings.remediate -eq $true) {
try {
if (!$StateIsCorrect) {
if ($StateIsCorrect -eq $true) {
Write-LogMessage -API 'Standards' -tenant $tenant -message "Activity Based Timeout is already enabled and set to $($Settings.timeout)" -sev Info
} else {
$PolicyTemplate = @{
displayName = 'DefaultTimeoutPolicy'
isOrganizationDefault = $true
Expand All @@ -58,27 +60,24 @@ function Invoke-CIPPStandardActivityBasedTimeout {
$body = ConvertTo-Json -InputObject $PolicyTemplate -Depth 10 -Compress

# Switch between parameter sets if the policy already exists
if ($null -eq $State.id) {
if ($null -eq $CurrentState.id) {
$RequestType = 'POST'
$URI = 'https://graph.microsoft.com/beta/policies/activityBasedTimeoutPolicies'
} else {
$RequestType = 'PATCH'
$URI = "https://graph.microsoft.com/beta/policies/activityBasedTimeoutPolicies/$($State.id)"
$URI = "https://graph.microsoft.com/beta/policies/activityBasedTimeoutPolicies/$($CurrentState.id)"
}
New-GraphPostRequest -tenantid $tenant -Uri $URI -Type $RequestType -Body $body -ContentType 'application/json'
Write-LogMessage -API 'Standards' -tenant $tenant -message "Enabled Activity Based Timeout with a value of $($Settings.timeout)" -sev Info
} else {
Write-LogMessage -API 'Standards' -tenant $tenant -message "Activity Based Timeout is already enabled and set to $($Settings.timeout)" -sev Info
}
} catch {
$ErrorMessage = Get-NormalizedError -Message $_.Exception.Message
Write-LogMessage -API 'Standards' -tenant $tenant -message "Failed to enable Activity Based Timeout a value of $($Settings.timeout). Error: $ErrorMessage" -sev Error
Write-LogMessage -API 'Standards' -tenant $tenant -message "Failed to enable Activity Based Timeout a value of $($Settings.timeout)." -sev Error -LogData $_
}
}

if ($Settings.alert -eq $true) {

if ($StateIsCorrect) {
if ($StateIsCorrect -eq $true) {
Write-LogMessage -API 'Standards' -tenant $tenant -message "Activity Based Timeout is enabled and set to $($Settings.timeout)" -sev Info
} else {
Write-LogMessage -API 'Standards' -tenant $tenant -message "Activity Based Timeout is not set to $($Settings.timeout)" -sev Alert
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,26 +29,26 @@ function Invoke-CIPPStandardDisableEmail {
param($Tenant, $Settings)
##$Rerun -Type Standard -Tenant $Tenant -Settings $Settings 'DisableEmail'

$CurrentInfo = New-GraphGetRequest -Uri 'https://graph.microsoft.com/beta/policies/authenticationmethodspolicy/authenticationMethodConfigurations/Email' -tenantid $Tenant
$State = if ($CurrentInfo.state -eq 'enabled') { $true } else { $false }
$CurrentState = New-GraphGetRequest -Uri 'https://graph.microsoft.com/beta/policies/authenticationmethodspolicy/authenticationMethodConfigurations/Email' -tenantid $Tenant
$StateIsCorrect = ($CurrentState.state -eq 'disabled')

If ($Settings.remediate -eq $true) {
if ($State) {
Set-CIPPAuthenticationPolicy -Tenant $tenant -APIName 'Standards' -AuthenticationMethodId 'Email' -Enabled $false
} else {
if ($StateIsCorrect -eq $true) {
Write-LogMessage -API 'Standards' -tenant $tenant -message 'Email authentication method is already disabled.' -sev Info
} else {
Set-CIPPAuthenticationPolicy -Tenant $tenant -APIName 'Standards' -AuthenticationMethodId 'Email' -Enabled $false
}
}

if ($Settings.alert -eq $true) {
if ($State) {
if ($StateIsCorrect -eq $true) {
Write-LogMessage -API 'Standards' -tenant $tenant -message 'Email authentication method is enabled' -sev Alert
} else {
Write-LogMessage -API 'Standards' -tenant $tenant -message 'Email authentication method is not enabled' -sev Info
}
}

if ($Settings.report -eq $true) {
Add-CIPPBPAField -FieldName 'DisableEmail' -FieldValue $State -StoreAs bool -Tenant $tenant
Add-CIPPBPAField -FieldName 'DisableEmail' -FieldValue $StateIsCorrect -StoreAs bool -Tenant $tenant
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,26 +29,26 @@ function Invoke-CIPPStandardDisableSMS {
param($Tenant, $Settings)
##$Rerun -Type Standard -Tenant $Tenant -Settings $Settings 'DisableSMS'

$CurrentInfo = New-GraphGetRequest -Uri 'https://graph.microsoft.com/beta/policies/authenticationmethodspolicy/authenticationMethodConfigurations/SMS' -tenantid $Tenant
$State = if ($CurrentInfo.state -eq 'enabled') { $true } else { $false }
$CurrentState = New-GraphGetRequest -Uri 'https://graph.microsoft.com/beta/policies/authenticationmethodspolicy/authenticationMethodConfigurations/SMS' -tenantid $Tenant
$StateIsCorrect = ($CurrentState.state -eq 'disabled')

If ($Settings.remediate -eq $true) {
if ($State) {
Set-CIPPAuthenticationPolicy -Tenant $tenant -APIName 'Standards' -AuthenticationMethodId 'SMS' -Enabled $false
} else {
if ($StateIsCorrect -eq $true) {
Write-LogMessage -API 'Standards' -tenant $tenant -message 'SMS authentication method is already disabled.' -sev Info
} else {
Set-CIPPAuthenticationPolicy -Tenant $tenant -APIName 'Standards' -AuthenticationMethodId 'SMS' -Enabled $false
}
}

if ($Settings.alert -eq $true) {
if ($State) {
if ($StateIsCorrect -eq $true) {
Write-LogMessage -API 'Standards' -tenant $tenant -message 'SMS authentication method is enabled' -sev Alert
} else {
Write-LogMessage -API 'Standards' -tenant $tenant -message 'SMS authentication method is not enabled' -sev Info
}
}

if ($Settings.report -eq $true) {
Add-CIPPBPAField -FieldName 'DisableSMS' -FieldValue $State -StoreAs bool -Tenant $tenant
Add-CIPPBPAField -FieldName 'DisableSMS' -FieldValue $StateIsCorrect -StoreAs bool -Tenant $tenant
}
}
Loading

0 comments on commit 6362279

Please sign in to comment.