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(Mandatory = $true)]
        [System.Management.Automation.PSCredential]
        $GlobalAdminAccount
    )

    Write-Verbose -Message "Getting configuration of MalwareFilterPolicy for $($Identity)"
    #region Telemetry
    $data = [System.Collections.Generic.Dictionary[[String], [String]]]::new()
    $data.Add("Resource", $MyInvocation.MyCommand.ModuleName)
    $data.Add("Method", $MyInvocation.MyCommand)
    Add-M365DSCTelemetryEvent -Data $data
    #endregion

    Test-MSCloudLogin -CloudCredential $GlobalAdminAccount `
        -Platform ExchangeOnline

    $MalwareFilterPolicys = Get-MalwareFilterPolicy

    $MalwareFilterPolicy = $MalwareFilterPolicys | Where-Object -FilterScript { $_.Identity -eq $Identity }
    if ($null -eq $MalwareFilterPolicy)
    {
        Write-Verbose -Message "MalwareFilterPolicy $($Identity) does not exist."
        $result = $PSBoundParameters
        $result.Ensure = 'Absent'
        return $result
    }
    else
    {
        $result = @{
            Identity                               = $Identity
            Action                                 = $MalwareFilterPolicy.Action
            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
    }
}
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(Mandatory = $true)]
        [System.Management.Automation.PSCredential]
        $GlobalAdminAccount
    )

    Write-Verbose -Message "Setting configuration of MalwareFilterPolicy for $($Identity)"

    Test-MSCloudLogin -CloudCredential $GlobalAdminAccount `
        -Platform ExchangeOnline

    $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(Mandatory = $true)]
        [System.Management.Automation.PSCredential]
        $GlobalAdminAccount
    )


    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-Microsoft365DSCParameterState -CurrentValues $CurrentValues `
        -DesiredValues $PSBoundParameters `
        -ValuesToCheck $ValuesToCheck.Keys

    Write-Verbose -Message "Test-TargetResource returned $($TestResult)"

    return $TestResult
}

function Export-TargetResource
{
    [CmdletBinding()]
    [OutputType([System.String])]
    param
    (
        [Parameter(Mandatory = $true)]
        [System.Management.Automation.PSCredential]
        $GlobalAdminAccount
    )

    $InformationPreference = "Continue"
    Test-MSCloudLogin -CloudCredential $GlobalAdminAccount `
        -Platform ExchangeOnline `
        -ErrorAction SilentlyContinue

    [array]$MalwareFilterPolicys = Get-MalwareFilterPolicy
    $content = ""
    $i = 1
    foreach ($MalwareFilterPolicy in $MalwareFilterPolicys)
    {
        Write-Information " [$i/$($MalwareFilterPolicys.length)] $($MalwareFilterPolicy.Identity)"

        $Params = @{
            Identity           = $MalwareFilterPolicy.Identity
            GlobalAdminAccount = $GlobalAdminAccount
        }

        $result = Get-TargetResource @Params
        $result.GlobalAdminAccount = Resolve-Credentials -UserName "globaladmin"
        $content += " EXOMalwareFilterPolicy " + (New-GUID).ToString() + "`r`n"
        $content += " {`r`n"
        $currentDSCBlock = Get-DSCBlock -Params $result -ModulePath $PSScriptRoot
        $content += Convert-DSCStringParamToVariable -DSCBlock $currentDSCBlock -ParameterName "GlobalAdminAccount"
        $content += " }`r`n"
        $i++
    }
    return $content
}
Export-ModuleMember -Function *-TargetResource