Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ticket bugfixes #1136

Merged
merged 3 commits into from
Oct 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Modules/CIPPCore/Public/Invoke-CIPPOffboardingJob.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ function Invoke-CIPPOffboardingJob {
}
}
{ $_.'RemoveLicenses' -eq 'true' } {
Remove-CIPPLicense -userid $userid -username $Username -tenantFilter $Tenantfilter -ExecutingUser $ExecutingUser -APIName $APIName
Remove-CIPPLicense -userid $userid -username $Username -tenantFilter $Tenantfilter -ExecutingUser $ExecutingUser -APIName $APIName -Schedule
}

{ $_.'Deleteuser' -eq 'true' } {
Expand Down
69 changes: 47 additions & 22 deletions Modules/CIPPCore/Public/Remove-CIPPLicense.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,57 @@ function Remove-CIPPLicense {
$userid,
$username,
$APIName = 'Remove License',
$TenantFilter
$TenantFilter,
[switch]$Schedule
)

try {
$ConvertTable = Import-Csv ConversionTable.csv
$User = New-GraphGetRequest -uri "https://graph.microsoft.com/beta/users/$($userid)" -tenantid $tenantFilter
if (!$username) { $username = $User.userPrincipalName }
$CurrentLicenses = $User.assignedlicenses.skuid
$ConvertedLicense = $(($ConvertTable | Where-Object { $_.guid -in $CurrentLicenses }).'Product_Display_Name' | Sort-Object -Unique) -join ', '
if ($CurrentLicenses) {
$LicensePayload = [PSCustomObject]@{
addLicenses = @()
removeLicenses = @($CurrentLicenses)
if ($Schedule.IsPresent) {
$ScheduledTask = @{
TenantFilter = $TenantFilter
Name = "Remove License: $Username"
Command = @{
value = 'Remove-CIPPLicense'
}
if ($PSCmdlet.ShouldProcess($userid, "Remove licenses: $ConvertedLicense")) {
$null = New-GraphPostRequest -uri "https://graph.microsoft.com/beta/users/$($userid)/assignlicense" -tenantid $tenantFilter -type POST -body (ConvertTo-Json -InputObject $LicensePayload -Compress -Depth 5) -verbose
Write-LogMessage -user $ExecutingUser -API $APIName -message "Removed licenses for $($username): $ConvertedLicense" -Sev 'Info' -tenant $TenantFilter
Parameters = [pscustomobject]@{
userid = $userid
username = $username
APIName = 'Scheduled License Removal'
ExecutingUser = $ExecutingUser
}
return "Removed licenses for $($Username): $ConvertedLicense"
} else {
Write-LogMessage -user $ExecutingUser -API $APIName -message "No licenses to remove for $username" -Sev 'Info' -tenant $TenantFilter
return "No licenses to remove for $username"
ScheduledTime = [int64](([datetime]::UtcNow).AddMinutes(5) - (Get-Date '1/1/1970')).TotalSeconds
PostExecution = @{
Webhook = $false
Email = $false
PSA = $false
}
}
Add-CIPPScheduledTask -Task $ScheduledTask -hidden $false
return "Scheduled license removal for $username"
} else {
try {
$ConvertTable = Import-Csv ConversionTable.csv
$User = New-GraphGetRequest -uri "https://graph.microsoft.com/beta/users/$($userid)" -tenantid $tenantFilter
if (!$username) { $username = $User.userPrincipalName }
$CurrentLicenses = $User.assignedlicenses.skuid
$ConvertedLicense = $(($ConvertTable | Where-Object { $_.guid -in $CurrentLicenses }).'Product_Display_Name' | Sort-Object -Unique) -join ', '
if ($CurrentLicenses) {
$LicensePayload = [PSCustomObject]@{
addLicenses = @()
removeLicenses = @($CurrentLicenses)
}
if ($PSCmdlet.ShouldProcess($userid, "Remove licenses: $ConvertedLicense")) {
$null = New-GraphPostRequest -uri "https://graph.microsoft.com/beta/users/$($userid)/assignlicense" -tenantid $tenantFilter -type POST -body (ConvertTo-Json -InputObject $LicensePayload -Compress -Depth 5) -verbose
Write-LogMessage -user $ExecutingUser -API $APIName -message "Removed licenses for $($username): $ConvertedLicense" -Sev 'Info' -tenant $TenantFilter
}
return "Removed licenses for $($Username): $ConvertedLicense"
} else {
Write-LogMessage -user $ExecutingUser -API $APIName -message "No licenses to remove for $username" -Sev 'Info' -tenant $TenantFilter
return "No licenses to remove for $username"
}
} catch {
$ErrorMessage = Get-CippException -Exception $_
Write-LogMessage -user $ExecutingUser -API $APIName -message "Could not remove license for $username. Error: $($ErrorMessage.NormalizedError)" -Sev 'Error' -tenant $TenantFilter -LogData $ErrorMessage
return "Could not remove license for $($username). Error: $($ErrorMessage.NormalizedError)"
}
} catch {
$ErrorMessage = Get-CippException -Exception $_
Write-LogMessage -user $ExecutingUser -API $APIName -message "Could not remove license for $username. Error: $($ErrorMessage.NormalizedError)" -Sev 'Error' -tenant $TenantFilter -LogData $ErrorMessage
return "Could not remove license for $($username). Error: $($ErrorMessage.NormalizedError)"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,11 @@ function Invoke-CIPPStandardAntiPhishPolicy {
param($Tenant, $Settings)
##$Rerun -Type Standard -Tenant $Tenant -Settings $Settings 'AntiPhishPolicy'

$PolicyName = 'Default Anti-Phishing Policy'
$PolicyName = @('Default Anti-Phishing Policy', 'Office365 AntiPhish Default (Default)')

$CurrentState = New-ExoRequest -tenantid $Tenant -cmdlet 'Get-AntiPhishPolicy' |
Where-Object -Property Name -EQ $PolicyName |
Select-Object Name, Enabled, PhishThresholdLevel, EnableMailboxIntelligence, EnableMailboxIntelligenceProtection, EnableSpoofIntelligence, EnableFirstContactSafetyTips, EnableSimilarUsersSafetyTips, EnableSimilarDomainsSafetyTips, EnableUnusualCharactersSafetyTips, EnableUnauthenticatedSender, EnableViaTag, AuthenticationFailAction, SpoofQuarantineTag, MailboxIntelligenceProtectionAction, MailboxIntelligenceQuarantineTag, TargetedUserProtectionAction, TargetedUserQuarantineTag, TargetedDomainProtectionAction, TargetedDomainQuarantineTag, EnableOrganizationDomainsProtection
Where-Object -Property Name -In $PolicyName |
Select-Object Name, Enabled, PhishThresholdLevel, EnableMailboxIntelligence, EnableMailboxIntelligenceProtection, EnableSpoofIntelligence, EnableFirstContactSafetyTips, EnableSimilarUsersSafetyTips, EnableSimilarDomainsSafetyTips, EnableUnusualCharactersSafetyTips, EnableUnauthenticatedSender, EnableViaTag, AuthenticationFailAction, SpoofQuarantineTag, MailboxIntelligenceProtectionAction, MailboxIntelligenceQuarantineTag, TargetedUserProtectionAction, TargetedUserQuarantineTag, TargetedDomainProtectionAction, TargetedDomainQuarantineTag, EnableOrganizationDomainsProtection

$StateIsCorrect = ($CurrentState.Name -eq $PolicyName) -and
($CurrentState.Enabled -eq $true) -and
Expand All @@ -82,8 +82,8 @@ function Invoke-CIPPStandardAntiPhishPolicy {
$AcceptedDomains = New-ExoRequest -tenantid $Tenant -cmdlet 'Get-AcceptedDomain'

$RuleState = New-ExoRequest -tenantid $Tenant -cmdlet 'Get-AntiPhishRule' |
Where-Object -Property Name -EQ "CIPP $PolicyName" |
Select-Object Name, AntiPhishPolicy, Priority, RecipientDomainIs
Where-Object -Property Name -EQ "CIPP $PolicyName" |
Select-Object Name, AntiPhishPolicy, Priority, RecipientDomainIs

$RuleStateIsCorrect = ($RuleState.Name -eq "CIPP $PolicyName") -and
($RuleState.AntiPhishPolicy -eq $PolicyName) -and
Expand Down