@@ -41,11 +41,24 @@ function Invoke-CIPPStandardMalwareFilterPolicy {
4141 param ($Tenant , $Settings )
4242 # #$Rerun -Type Standard -Tenant $Tenant -Settings $Settings 'MalwareFilterPolicy'
4343
44- $PolicyName = ' Default Malware Policy'
44+ $PolicyList = @ (' CIPP Default Malware Policy' , ' Default Malware Policy' )
45+ $ExistingPolicy = New-ExoRequest - tenantid $Tenant - cmdlet ' Get-MalwareFilterPolicy' | Where-Object - Property Name -In $PolicyList
46+ if ($null -eq $ExistingPolicy.Name ) {
47+ $PolicyName = $PolicyList [0 ]
48+ } else {
49+ $PolicyName = $ExistingPolicy.Name
50+ }
51+ $RuleList = @ ( ' CIPP Default Malware Rule' , ' CIPP Default Malware Policy' )
52+ $ExistingRule = New-ExoRequest - tenantid $Tenant - cmdlet ' Get-MalwareFilterRule' | Where-Object - Property Name -In $RuleList
53+ if ($null -eq $ExistingRule.Name ) {
54+ $RuleName = $RuleList [0 ]
55+ } else {
56+ $RuleName = $ExistingRule.Name
57+ }
4558
4659 $CurrentState = New-ExoRequest - tenantid $Tenant - cmdlet ' Get-MalwareFilterPolicy' |
47- Where-Object - Property Name -EQ $PolicyName |
48- Select-Object Name, EnableFileFilter, FileTypeAction, FileTypes, ZapEnabled, QuarantineTag, EnableInternalSenderAdminNotifications, InternalSenderAdminAddress, EnableExternalSenderAdminNotifications, ExternalSenderAdminAddress
60+ Where-Object - Property Name -EQ $PolicyName |
61+ Select-Object Name, EnableFileFilter, FileTypeAction, FileTypes, ZapEnabled, QuarantineTag, EnableInternalSenderAdminNotifications, InternalSenderAdminAddress, EnableExternalSenderAdminNotifications, ExternalSenderAdminAddress
4962
5063 $DefaultFileTypes = @ (' ace' , ' ani' , ' apk' , ' app' , ' appx' , ' arj' , ' bat' , ' cab' , ' cmd' , ' com' , ' deb' , ' dex' , ' dll' , ' docm' , ' elf' , ' exe' , ' hta' , ' img' , ' iso' , ' jar' , ' jnlp' , ' kext' , ' lha' , ' lib' , ' library' , ' lnk' , ' lzh' , ' macho' , ' msc' , ' msi' , ' msix' , ' msp' , ' mst' , ' pif' , ' ppa' , ' ppam' , ' reg' , ' rev' , ' scf' , ' scr' , ' sct' , ' sys' , ' uif' , ' vb' , ' vbe' , ' vbs' , ' vxd' , ' wsc' , ' wsf' , ' wsh' , ' xll' , ' xz' , ' z' )
5164
@@ -69,10 +82,10 @@ function Invoke-CIPPStandardMalwareFilterPolicy {
6982 $AcceptedDomains = New-ExoRequest - tenantid $Tenant - cmdlet ' Get-AcceptedDomain'
7083
7184 $RuleState = New-ExoRequest - tenantid $Tenant - cmdlet ' Get-MalwareFilterRule' |
72- Where-Object - Property Name -EQ " CIPP $PolicyName " |
73- Select-Object Name, MalwareFilterPolicy, Priority, RecipientDomainIs
85+ Where-Object - Property Name -EQ $RuleName |
86+ Select-Object Name, MalwareFilterPolicy, Priority, RecipientDomainIs
7487
75- $RuleStateIsCorrect = ($RuleState.Name -eq " CIPP $PolicyName " ) -and
88+ $RuleStateIsCorrect = ($RuleState.Name -eq $RuleName ) -and
7689 ($RuleState.MalwareFilterPolicy -eq $PolicyName ) -and
7790 ($RuleState.Priority -eq 0 ) -and
7891 (! (Compare-Object - ReferenceObject $RuleState.RecipientDomainIs - DifferenceObject $AcceptedDomains.Name ))
@@ -98,47 +111,46 @@ function Invoke-CIPPStandardMalwareFilterPolicy {
98111 try {
99112 $cmdparams.Add (' Identity' , $PolicyName )
100113 New-ExoRequest - tenantid $Tenant - cmdlet ' Set-MalwareFilterPolicy' - cmdparams $cmdparams - UseSystemMailbox $true
101- Write-LogMessage - API ' Standards' - tenant $Tenant - message ' Updated Malware Filter Policy ' - sev Info
114+ Write-LogMessage - API ' Standards' - tenant $Tenant - message " Updated Malware Filter policy $PolicyName . " - sev Info
102115 } catch {
103- $ErrorMessage = Get-NormalizedError - Message $_.Exception.Message
104- Write-LogMessage - API ' Standards' - tenant $Tenant - message " Failed to update Malware Filter Policy. Error: $ErrorMessage " - sev Error
116+ Write-LogMessage - API ' Standards' - tenant $Tenant - message " Failed to update Malware Filter policy $PolicyName ." - sev Error - LogData $_
105117 }
106118 } else {
107119 try {
108120 $cmdparams.Add (' Name' , $PolicyName )
109121 New-ExoRequest - tenantid $Tenant - cmdlet ' New-MalwareFilterPolicy' - cmdparams $cmdparams - UseSystemMailbox $true
110- Write-LogMessage - API ' Standards' - tenant $Tenant - message ' Created Malware Filter Policy ' - sev Info
122+ Write-LogMessage - API ' Standards' - tenant $Tenant - message " Created Malware Filter policy $PolicyName . " - sev Info
111123 } catch {
112- $ErrorMessage = Get-NormalizedError - Message $_.Exception.Message
113- Write-LogMessage - API ' Standards' - tenant $Tenant - message " Failed to create Malware Filter Policy. Error: $ErrorMessage " - sev Error
124+ Write-LogMessage - API ' Standards' - tenant $Tenant - message " Failed to create Malware Filter policy $PolicyName ." - sev Error - LogData $_
114125 }
115126 }
116127 }
117128
118129 if ($RuleStateIsCorrect -eq $false ) {
119130 $cmdparams = @ {
120- MalwareFilterPolicy = $PolicyName
121131 Priority = 0
122132 RecipientDomainIs = $AcceptedDomains.Name
123133 }
124134
125- if ($RuleState.Name -eq " CIPP $PolicyName " ) {
135+ if ($RuleState.MalwareFilterPolicy -ne $PolicyName ) {
136+ $cmdparams.Add (' MalwareFilterPolicy' , $PolicyName )
137+ }
138+
139+ if ($RuleState.Name -eq $RuleName ) {
126140 try {
127- $cmdparams.Add (' Identity' , " CIPP $PolicyName " )
141+ $cmdparams.Add (' Identity' , $RuleName )
128142 New-ExoRequest - tenantid $Tenant - cmdlet ' Set-MalwareFilterRule' - cmdparams $cmdparams - UseSystemMailbox $true
129- Write-LogMessage - API ' Standards' - tenant $Tenant - message ' Updated Malware Filter Rule ' - sev Info
143+ Write-LogMessage - API ' Standards' - tenant $Tenant - message " Updated Malware Filter rule $RuleName . " - sev Info
130144 } catch {
131- $ErrorMessage = Get-NormalizedError - Message $_.Exception.Message
132- Write-LogMessage - API ' Standards' - tenant $Tenant - message " Failed to update Malware Filter Rule. Error: $ErrorMessage " - sev Error
145+ Write-LogMessage - API ' Standards' - tenant $Tenant - message " Failed to update Malware Filter Rule $RuleName ." - sev Error - LogData $_
133146 }
134147 } else {
135148 try {
136- $cmdparams.Add (' Name' , " CIPP $PolicyName " )
149+ $cmdparams.Add (' Name' , $RuleName )
137150 New-ExoRequest - tenantid $Tenant - cmdlet ' New-MalwareFilterRule' - cmdparams $cmdparams - UseSystemMailbox $true
138- Write-LogMessage - API ' Standards' - tenant $Tenant - message ' Created Malware Filter Rule ' - sev Info
151+ Write-LogMessage - API ' Standards' - tenant $Tenant - message " Created Malware Filter rule $RuleName . " - sev Info
139152 } catch {
140- $ErrorMessage = Get-NormalizedError - Message $_.Exception.Message
141- Write-LogMessage - API ' Standards' - tenant $Tenant - message " Failed to create Malware Filter Rule. Error: $ErrorMessage " - sev Error
153+ Write-LogMessage - API ' Standards' - tenant $Tenant - message " Failed to create Malware Filter rule $RuleName ." - sev Error - LogData $_
142154 }
143155 }
144156 }
0 commit comments