DSCResources/MSFT_EXOMalwareFilterPolicy/MSFT_EXOMalwareFilterPolicy.psm1
function Get-TargetResource { [CmdletBinding()] [OutputType([System.Collections.Hashtable])] param ( [Parameter(Mandatory = $true)] [System.String] $Identity, [Parameter()] [ValidateSet('DeleteMessage', 'DeleteAttachmentAndUseDefaultAlert', 'DeleteAttachmentAndUseCustomAlert')] [System.String] $Action, [Parameter()] [System.String] $AdminDisplayName, [Parameter()] [System.String] $CustomAlertText, [Parameter()] [System.String] $CustomExternalBody, [Parameter()] [System.String] $CustomExternalSubject, [Parameter()] [System.String] $CustomFromAddress, [Parameter()] [System.String] $CustomFromName, [Parameter()] [System.String] $CustomInternalBody, [Parameter()] [System.String] $CustomInternalSubject, [Parameter()] [System.Boolean] $CustomNotifications, [Parameter()] [System.Boolean] $EnableExternalSenderAdminNotifications, [Parameter()] [System.Boolean] $EnableExternalSenderNotifications, [Parameter()] [System.Boolean] $EnableFileFilter, [Parameter()] [System.Boolean] $EnableInternalSenderAdminNotifications, [Parameter()] [System.Boolean] $EnableInternalSenderNotifications, [Parameter()] [System.String] $ExternalSenderAdminAddress, [Parameter()] [System.String[]] $FileTypes = @(), [Parameter()] [System.String] $InternalSenderAdminAddress, [Parameter()] [System.Boolean] $MakeDefault, [Parameter()] [System.Boolean] $ZapEnabled, [Parameter()] [ValidateSet("Present", "Absent")] [System.String] $Ensure = "Present", [Parameter()] [System.Management.Automation.PSCredential] $GlobalAdminAccount, [Parameter()] [System.String] $ApplicationId, [Parameter()] [System.String] $TenantId, [Parameter()] [System.String] $CertificateThumbprint, [Parameter()] [System.String] $CertificatePath, [Parameter()] [System.Management.Automation.PSCredential] $CertificatePassword ) Write-Verbose -Message "Getting configuration of MalwareFilterPolicy for $($Identity)" #region Telemetry $ResourceName = $MyInvocation.MyCommand.ModuleName.Replace("MSFT_", "") $data = [System.Collections.Generic.Dictionary[[String], [String]]]::new() $data.Add("Resource", $ResourceName) $data.Add("Method", $MyInvocation.MyCommand) $data.Add("Principal", $GlobalAdminAccount.UserName) $data.Add("TenantId", $TenantId) Add-M365DSCTelemetryEvent -Data $data #endregion if ($Global:CurrentModeIsExport) { $ConnectionMode = New-M365DSCConnection -Platform 'ExchangeOnline' ` -InboundParameters $PSBoundParameters ` -SkipModuleReload $true } else { $ConnectionMode = New-M365DSCConnection -Platform 'ExchangeOnline' ` -InboundParameters $PSBoundParameters } $nullReturn = $PSBoundParameters $nullReturn.Ensure = 'Absent' try { $MalwareFilterPolicys = Get-MalwareFilterPolicy -ErrorAction Stop $MalwareFilterPolicy = $MalwareFilterPolicys | Where-Object -FilterScript { $_.Identity -eq $Identity } if ($null -eq $MalwareFilterPolicy) { Write-Verbose -Message "MalwareFilterPolicy $($Identity) does not exist." return $nullReturn } else { $ActionValue = $MalwareFilterPolicy.Action if ($ActionValue -eq 'DeleteAttachmentAndUseDefaultAlertText') { $ActionValue = 'DeleteAttachmentAndUseDefaultAlert' } elseif ($ActionValue -eq 'DeleteAttachmentAndUseCustomAlertText') { $ActionValue = 'DeleteAttachmentAndUseCustomAlert' } $result = @{ Identity = $Identity Action = $ActionValue AdminDisplayName = $MalwareFilterPolicy.AdminDisplayName CustomAlertText = $MalwareFilterPolicy.CustomAlertText CustomExternalBody = $MalwareFilterPolicy.CustomExternalBody CustomExternalSubject = $MalwareFilterPolicy.CustomExternalSubject CustomFromAddress = $MalwareFilterPolicy.CustomFromAddress CustomFromName = $MalwareFilterPolicy.CustomFromName CustomInternalBody = $MalwareFilterPolicy.CustomInternalBody CustomInternalSubject = $MalwareFilterPolicy.CustomInternalSubject CustomNotifications = $MalwareFilterPolicy.CustomNotifications EnableExternalSenderAdminNotifications = $MalwareFilterPolicy.EnableExternalSenderAdminNotifications EnableExternalSenderNotifications = $MalwareFilterPolicy.EnableExternalSenderNotifications EnableFileFilter = $MalwareFilterPolicy.EnableFileFilter EnableInternalSenderAdminNotifications = $MalwareFilterPolicy.EnableInternalSenderAdminNotifications EnableInternalSenderNotifications = $MalwareFilterPolicy.EnableInternalSenderNotifications ExternalSenderAdminAddress = $MalwareFilterPolicy.ExternalSenderAdminAddress FileTypes = $MalwareFilterPolicy.FileTypes InternalSenderAdminAddress = $MalwareFilterPolicy.InternalSenderAdminAddress MakeDefault = $MalwareFilterPolicy.MakeDefault ZapEnabled = $MalwareFilterPolicy.ZapEnabled GlobalAdminAccount = $GlobalAdminAccount Ensure = 'Present' } Write-Verbose -Message "Found MalwareFilterPolicy $($Identity)" Write-Verbose -Message "Get-TargetResource Result: `n $(Convert-M365DscHashtableToString -Hashtable $result)" return $result } } catch { Write-Verbose -Message $_ Add-M365DSCEvent -Message $_ -EntryType 'Error' ` -EventID 1 -Source $($MyInvocation.MyCommand.Source) return $nullReturn } } function Set-TargetResource { [CmdletBinding()] param ( [Parameter(Mandatory = $true)] [System.String] $Identity, [Parameter()] [ValidateSet('DeleteMessage', 'DeleteAttachmentAndUseDefaultAlert', 'DeleteAttachmentAndUseCustomAlert')] [System.String] $Action, [Parameter()] [System.String] $AdminDisplayName, [Parameter()] [System.String] $CustomAlertText, [Parameter()] [System.String] $CustomExternalBody, [Parameter()] [System.String] $CustomExternalSubject, [Parameter()] [System.String] $CustomFromAddress, [Parameter()] [System.String] $CustomFromName, [Parameter()] [System.String] $CustomInternalBody, [Parameter()] [System.String] $CustomInternalSubject, [Parameter()] [System.Boolean] $CustomNotifications, [Parameter()] [System.Boolean] $EnableExternalSenderAdminNotifications, [Parameter()] [System.Boolean] $EnableExternalSenderNotifications, [Parameter()] [System.Boolean] $EnableFileFilter, [Parameter()] [System.Boolean] $EnableInternalSenderAdminNotifications, [Parameter()] [System.Boolean] $EnableInternalSenderNotifications, [Parameter()] [System.String] $ExternalSenderAdminAddress, [Parameter()] [System.String[]] $FileTypes = @(), [Parameter()] [System.String] $InternalSenderAdminAddress, [Parameter()] [System.Boolean] $MakeDefault, [Parameter()] [System.Boolean] $ZapEnabled, [Parameter()] [ValidateSet("Present", "Absent")] [System.String] $Ensure = "Present", [Parameter()] [System.Management.Automation.PSCredential] $GlobalAdminAccount, [Parameter()] [System.String] $ApplicationId, [Parameter()] [System.String] $TenantId, [Parameter()] [System.String] $CertificateThumbprint, [Parameter()] [System.String] $CertificatePath, [Parameter()] [System.Management.Automation.PSCredential] $CertificatePassword ) #region Telemetry $ResourceName = $MyInvocation.MyCommand.ModuleName.Replace("MSFT_", "") $data = [System.Collections.Generic.Dictionary[[String], [String]]]::new() $data.Add("Resource", $ResourceName) $data.Add("Method", $MyInvocation.MyCommand) $data.Add("Principal", $GlobalAdminAccount.UserName) $data.Add("TenantId", $TenantId) Add-M365DSCTelemetryEvent -Data $data #endregion Write-Verbose -Message "Setting configuration of MalwareFilterPolicy for $($Identity)" $ConnectionMode = New-M365DSCConnection -Platform 'ExchangeOnline' ` -InboundParameters $PSBoundParameters $MalwareFilterPolicys = Get-MalwareFilterPolicy $MalwareFilterPolicy = $MalwareFilterPolicys | Where-Object -FilterScript { $_.Identity -eq $Identity } $MalwareFilterPolicyParams = $PSBoundParameters $MalwareFilterPolicyParams.Remove('Ensure') | Out-Null $MalwareFilterPolicyParams.Remove('GlobalAdminAccount') | Out-Null if (('Present' -eq $Ensure ) -and ($null -eq $MalwareFilterPolicy)) { Write-Verbose -Message "Creating MalwareFilterPolicy $($Identity)." $MalwareFilterPolicyParams.Add("Name", $Identity) $MalwareFilterPolicyParams.Remove('Identity') | Out-Null $MalwareFilterPolicyParams.Remove('MakeDefault') | Out-Null New-MalwareFilterPolicy @MalwareFilterPolicyParams } elseif (('Present' -eq $Ensure ) -and ($Null -ne $MalwareFilterPolicy)) { Write-Verbose -Message "Setting MalwareFilterPolicy $($Identity) with values: $(Convert-M365DscHashtableToString -Hashtable $MalwareFilterPolicyParams)" Set-MalwareFilterPolicy @MalwareFilterPolicyParams -Confirm:$false } elseif (('Absent' -eq $Ensure ) -and ($null -ne $MalwareFilterPolicy)) { Write-Verbose -Message "Removing MalwareFilterPolicy $($Identity)" Remove-MalwareFilterPolicy -Identity $Identity -Confirm:$false } } function Test-TargetResource { [CmdletBinding()] [OutputType([System.Boolean])] param ( [Parameter(Mandatory = $true)] [System.String] $Identity, [Parameter()] [ValidateSet('DeleteMessage', 'DeleteAttachmentAndUseDefaultAlert', 'DeleteAttachmentAndUseCustomAlert')] [System.String] $Action, [Parameter()] [System.String] $AdminDisplayName, [Parameter()] [System.String] $CustomAlertText, [Parameter()] [System.String] $CustomExternalBody, [Parameter()] [System.String] $CustomExternalSubject, [Parameter()] [System.String] $CustomFromAddress, [Parameter()] [System.String] $CustomFromName, [Parameter()] [System.String] $CustomInternalBody, [Parameter()] [System.String] $CustomInternalSubject, [Parameter()] [System.Boolean] $CustomNotifications, [Parameter()] [System.Boolean] $EnableExternalSenderAdminNotifications, [Parameter()] [System.Boolean] $EnableExternalSenderNotifications, [Parameter()] [System.Boolean] $EnableFileFilter, [Parameter()] [System.Boolean] $EnableInternalSenderAdminNotifications, [Parameter()] [System.Boolean] $EnableInternalSenderNotifications, [Parameter()] [System.String] $ExternalSenderAdminAddress, [Parameter()] [System.String[]] $FileTypes = @(), [Parameter()] [System.String] $InternalSenderAdminAddress, [Parameter()] [System.Boolean] $MakeDefault, [Parameter()] [System.Boolean] $ZapEnabled, [Parameter()] [ValidateSet("Present", "Absent")] [System.String] $Ensure = "Present", [Parameter()] [System.Management.Automation.PSCredential] $GlobalAdminAccount, [Parameter()] [System.String] $ApplicationId, [Parameter()] [System.String] $TenantId, [Parameter()] [System.String] $CertificateThumbprint, [Parameter()] [System.String] $CertificatePath, [Parameter()] [System.Management.Automation.PSCredential] $CertificatePassword ) Write-Verbose -Message "Testing configuration of MalwareFilterPolicy for $($Identity)" $CurrentValues = Get-TargetResource @PSBoundParameters Write-Verbose -Message "Current Values: $(Convert-M365DscHashtableToString -Hashtable $CurrentValues)" Write-Verbose -Message "Target Values: $(Convert-M365DscHashtableToString -Hashtable $PSBoundParameters)" $ValuesToCheck = $PSBoundParameters $ValuesToCheck.Remove('GlobalAdminAccount') | Out-Null $TestResult = Test-M365DSCParameterState -CurrentValues $CurrentValues ` -Source $($MyInvocation.MyCommand.Source) ` -DesiredValues $PSBoundParameters ` -ValuesToCheck $ValuesToCheck.Keys Write-Verbose -Message "Test-TargetResource returned $($TestResult)" return $TestResult } function Export-TargetResource { [CmdletBinding()] [OutputType([System.String])] param ( [Parameter()] [System.Management.Automation.PSCredential] $GlobalAdminAccount, [Parameter()] [System.String] $ApplicationId, [Parameter()] [System.String] $TenantId, [Parameter()] [System.String] $CertificateThumbprint, [Parameter()] [System.String] $CertificatePath, [Parameter()] [System.Management.Automation.PSCredential] $CertificatePassword ) #region Telemetry $ResourceName = $MyInvocation.MyCommand.ModuleName.Replace("MSFT_", "") $data = [System.Collections.Generic.Dictionary[[String], [String]]]::new() $data.Add("Resource", $ResourceName) $data.Add("Method", $MyInvocation.MyCommand) $data.Add("Principal", $GlobalAdminAccount.UserName) $data.Add("TenantId", $TenantId) Add-M365DSCTelemetryEvent -Data $data #endregion $ConnectionMode = New-M365DSCConnection -Platform 'ExchangeOnline' ` -InboundParameters $PSBoundParameters ` -SkipModuleReload $true try { [array]$MalwareFilterPolicys = Get-MalwareFilterPolicy -ErrorAction Stop if ($MalwareFilterPolicys.Length -eq 0) { Write-Host $Global:M365DSCEmojiGreenCheckMark } else { Write-Host "`r`n" -NoNewLine } $dscContent = "" $i = 1 foreach ($MalwareFilterPolicy in $MalwareFilterPolicys) { Write-Host " |---[$i/$($MalwareFilterPolicys.length)] $($MalwareFilterPolicy.Identity)" -NoNewLine $Params = @{ Identity = $MalwareFilterPolicy.Identity GlobalAdminAccount = $GlobalAdminAccount ApplicationId = $ApplicationId TenantId = $TenantId CertificateThumbprint = $CertificateThumbprint CertificatePassword = $CertificatePassword CertificatePath = $CertificatePath } $Results = Get-TargetResource @Params $Results = Update-M365DSCExportAuthenticationResults -ConnectionMode $ConnectionMode ` -Results $Results $dscContent += Get-M365DSCExportContentForResource -ResourceName $ResourceName ` -ConnectionMode $ConnectionMode ` -ModulePath $PSScriptRoot ` -Results $Results ` -GlobalAdminAccount $GlobalAdminAccount Write-Host $Global:M365DSCEmojiGreenCheckMark $i++ } return $dscContent } catch { Write-Verbose -Message $_ Add-M365DSCEvent -Message $_ -EntryType 'Error' ` -EventID 1 -Source $($MyInvocation.MyCommand.Source) return "" } } Export-ModuleMember -Function *-TargetResource |