Skip to content

Commit

Permalink
Merge pull request #48 from KelvinTegelaar/dev
Browse files Browse the repository at this point in the history
[pull] dev from KelvinTegelaar:dev
  • Loading branch information
pull[bot] authored Feb 2, 2025
2 parents 7c222e5 + 2ea2c53 commit 52b5b42
Show file tree
Hide file tree
Showing 2 changed files with 124 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,43 +11,52 @@ Function Invoke-EditContact {
param($Request, $TriggerMetadata)

$APIName = $TriggerMetadata.FunctionName
Write-LogMessage -user $request.headers.'x-ms-client-principal' -API $APINAME -message 'Accessed this API' -Sev 'Debug'
$TenantID = $Request.body.tenantID
$ExecutingUser = $Request.headers.'x-ms-client-principal'
Write-LogMessage -user $ExecutingUser -API $APINAME -message 'Accessed this API' -Sev 'Debug'

$contactobj = $Request.body
write-host "This is the contact object: $contactobj"
# Write to the Azure Functions log stream.
Write-Host 'PowerShell HTTP trigger function processed a request.'
try {

$BodyToship = [pscustomobject] @{
'DisplayName' = $contactobj.DisplayName
'WindowsEmailAddress' = $contactobj.mail
'FirstName' = $contactObj.firstName
'LastName' = $contactobj.LastName
"Title" = $contactobj.jobTitle
"StreetAddress" = $contactobj.StreetAddress
"PostalCode" = $contactobj.PostalCode
"City" = $contactobj.City
"CountryOrRegion" = $contactobj.Country
"Company" = $contactobj.companyName
"mobilePhone" = $contactobj.MobilePhone
"phone" = $contactobj.BusinessPhone
'identity' = $contactobj.ContactID
# Extract contact information from the request body
$contactInfo = $Request.body

# Log the received contact object
Write-Host "Received contact object: $($contactInfo | ConvertTo-Json)"

# Prepare the body for the Set-Contact cmdlet
$bodyForSetContact = [pscustomobject] @{
'DisplayName' = $contactInfo.DisplayName
'WindowsEmailAddress' = $contactInfo.mail
'FirstName' = $contactInfo.firstName
'LastName' = $contactInfo.LastName
'Title' = $contactInfo.jobTitle
'StreetAddress' = $contactInfo.StreetAddress
'PostalCode' = $contactInfo.PostalCode
'City' = $contactInfo.City
'CountryOrRegion' = $contactInfo.Country
'Company' = $contactInfo.companyName
'mobilePhone' = $contactInfo.MobilePhone
'phone' = $contactInfo.BusinessPhone
'identity' = $contactInfo.ContactID
}
$EditContact = New-ExoRequest -tenantid $Request.body.tenantID -cmdlet 'Set-Contact' -cmdparams $BodyToship -UseSystemMailbox $true
$Results = [pscustomobject]@{'Results' = "Successfully edited contact $($contactobj.Displayname)" }
Write-LogMessage -user $request.headers.'x-ms-client-principal' -API $APINAME -tenant $($contactobj.tenantid) -message "Created contact $($contactobj.displayname)" -Sev 'Info'

} catch {
Write-LogMessage -user $request.headers.'x-ms-client-principal' -API $APINAME -tenant $($contactobj.tenantid) -message "Contact creation API failed. $($_.Exception.Message)" -Sev 'Error'
$Results = [pscustomobject]@{'Results' = "Failed to edit contact. $($_.Exception.Message)" }
# Call the Set-Contact cmdlet to update the contact
$null = New-ExoRequest -tenantid $TenantID -cmdlet 'Set-Contact' -cmdParams $bodyForSetContact -UseSystemMailbox $true
$Results = "Successfully edited contact $($contactInfo.DisplayName)"
Write-LogMessage -user $ExecutingUser -API $APINAME -tenant $TenantID -message $Results -Sev Info
$StatusCode = [HttpStatusCode]::OK

} catch {
$ErrorMessage = Get-CippException -Exception $_
$Results = "Failed to edit contact. $($ErrorMessage.NormalizedError)"
Write-LogMessage -user $ExecutingUser -API $APINAME -tenant $TenantID -message $Results -Sev Error -LogData $ErrorMessage
$StatusCode = [HttpStatusCode]::InternalServerError
}


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

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
function Invoke-CIPPStandardRetentionPolicyTag {
<#
.FUNCTIONALITY
Internal
.COMPONENT
(APIName) RetentionPolicyTag
#>

param($Tenant, $Settings)
##$Rerun -Type Standard -Tenant $Tenant -Settings $Settings 'RetentionPolicyTag'

$PolicyName = 'CIPP Deleted Items'
$CurrentState = New-ExoRequest -tenantid $Tenant -cmdlet 'Get-RetentionPolicyTag' |
Where-Object -Property Identity -EQ $PolicyName

$PolicyState = New-ExoRequest -tenantid $Tenant -cmdlet 'Get-RetentionPolicy' |
Where-Object -Property Identity -EQ 'Default MRM Policy'

$StateIsCorrect = ($CurrentState.Name -eq $PolicyName) -and
($CurrentState.RetentionEnabled -eq $true) -and
($CurrentState.RetentionAction -eq 'PermanentlyDelete') -and
($CurrentState.AgeLimitForRetention -eq $Settings.AgeLimitForRetention) -and
($CurrentState.Type -eq 'DeletedItems') -and
($PolicyState.RetentionPolicyTagLinks -contains $PolicyName)

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

if ($StateIsCorrect -eq $true) {
Write-LogMessage -API 'Standards' -tenant $Tenant -message 'Retention policy tag already correctly configured' -sev Info
} else {
$cmdparams = @{
RetentionEnabled = $true
AgeLimitForRetention = $Settings.AgeLimitForRetention
RetentionAction = 'PermanentlyDelete'
}

if ($CurrentState.Name -eq $PolicyName) {
try {
$cmdparams.Add('Identity', $PolicyName)
New-ExoRequest -tenantid $Tenant -cmdlet 'Set-RetentionPolicyTag' -cmdparams $cmdparams -UseSystemMailbox $true
Write-LogMessage -API 'Standards' -tenant $Tenant -message "Updated Retention policy tag $PolicyName." -sev Info
} catch {
Write-LogMessage -API 'Standards' -tenant $Tenant -message "Failed to update Retention policy tag $PolicyName." -sev Error -LogData $_
}
} else {
try {
$cmdparams.Add('Name', $PolicyName)
$cmdparams.Add('Type', 'DeletedItems')
New-ExoRequest -tenantid $Tenant -cmdlet 'New-RetentionPolicyTag' -cmdparams $cmdparams -UseSystemMailbox $true
Write-LogMessage -API 'Standards' -tenant $Tenant -message "Created Retention policy tag $PolicyName." -sev Info
} catch {
Write-LogMessage -API 'Standards' -tenant $Tenant -message "Failed to create Retention policy tag $PolicyName." -sev Error -LogData $_
}
}

if ($PolicyState.RetentionPolicyTagLinks -notcontains $PolicyName) {
try {
$cmdparams = @{
Identity = 'Default MRM Policy'
RetentionPolicyTagLinks = @($PolicyState.RetentionPolicyTagLinks + $PolicyName)
}
New-ExoRequest -tenantid $Tenant -cmdlet 'Set-RetentionPolicy' -cmdparams $cmdparams -UseSystemMailbox $true
Write-LogMessage -API 'Standards' -tenant $Tenant -message "Added $PolicyName Retention tag to $($PolicyState.Identity)." -sev Info
} catch {
Write-LogMessage -API 'Standards' -tenant $Tenant -message "Failed to add $PolicyName Retention tag to $($PolicyState.Identity)." -sev Error -LogData $_.Exception.Message
}
}

}

}

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

if ($StateIsCorrect -eq $true) {
Write-LogMessage -API 'Standards' -tenant $Tenant -message 'Retention Policy is enabled' -sev Info
} else {
Write-LogMessage -API 'Standards' -tenant $Tenant -message 'Retention Policy is not enabled' -sev Alert
}
}

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

}

0 comments on commit 52b5b42

Please sign in to comment.