DSCResources/MSFT_EXOAtpPolicyForO365/MSFT_EXOAtpPolicyForO365.psm1
function Get-TargetResource { [CmdletBinding()] [OutputType([System.Collections.Hashtable])] param ( [Parameter(Mandatory = $true)] [ValidateSet('Yes')] [String] $IsSingleInstance, [Parameter()] [System.String] $Identity = 'Default', [Parameter()] [Boolean] $AllowClickThrough = $true, [Parameter()] [System.String[]] $BlockUrls = @(), [Parameter()] [Boolean] $EnableATPForSPOTeamsODB = $false, [Parameter()] [Boolean] $EnableSafeLinksForClients = $false, [Parameter()] [Boolean] $TrackClicks = $true, [Parameter()] [ValidateSet('Present')] [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 AtpPolicyForO365 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 } try { $nullReturn = $PSBoundParameters $nullReturn.Ensure = 'Absent' $AtpPolicies = Get-AtpPolicyForO365 $AtpPolicyForO365 = $AtpPolicies | Where-Object -FilterScript { $_.Identity -eq $Identity } if (-not $AtpPolicyForO365) { Write-Verbose -Message "AtpPolicyForO365 $($Identity) does not exist." return $nullReturn } else { $result = @{ IsSingleInstance = "Yes" Identity = $AtpPolicyForO365.Identity AllowClickThrough = $AtpPolicyForO365.AllowClickThrough BlockUrls = $AtpPolicyForO365.BlockUrls EnableATPForSPOTeamsODB = $AtpPolicyForO365.EnableATPForSPOTeamsODB EnableSafeLinksForClients = $AtpPolicyForO365.EnableSafeLinksForClients TrackClicks = $AtpPolicyForO365.TrackClicks Ensure = 'Present' } Write-Verbose -Message "Found AtpPolicyForO365 $($Identity)" Write-Verbose -Message "Get-TargetResource Result: `n $(Convert-M365DscHashtableToString -Hashtable $result)" return $result } } catch { Write-Warning $_.Exception return $nullReturn } } function Set-TargetResource { [CmdletBinding()] param ( [Parameter(Mandatory = $true)] [ValidateSet('Yes')] [String] $IsSingleInstance, [Parameter()] [System.String] $Identity = 'Default', [Parameter()] [Boolean] $AllowClickThrough = $true, [Parameter()] [System.String[]] $BlockUrls = @(), [Parameter()] [Boolean] $EnableATPForSPOTeamsODB = $false, [Parameter()] [Boolean] $EnableSafeLinksForClients = $false, [Parameter()] [Boolean] $TrackClicks = $true, [Parameter()] [ValidateSet('Present')] [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 "Setting configuration of AtpPolicyForO365 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 ('Default' -ne $Identity) { throw "EXOAtpPolicyForO365 configurations MUST specify Identity value of 'Default'" } $ConnectionMode = New-M365DSCConnection -Platform 'ExchangeOnline' ` -InboundParameters $PSBoundParameters $AtpPolicyParams = $PSBoundParameters $AtpPolicyParams.Remove('Ensure') | Out-Null $AtpPolicyParams.Remove('GlobalAdminAccount') | Out-Null $AtpPolicyParams.Remove('IsSingleInstance') | Out-Null Write-Verbose -Message "Setting AtpPolicyForO365 $Identity with values: $(Convert-M365DscHashtableToString -Hashtable $AtpPolicyParams)" Set-AtpPolicyForO365 @AtpPolicyParams } function Test-TargetResource { [CmdletBinding()] [OutputType([System.Boolean])] param ( [Parameter(Mandatory = $true)] [ValidateSet('Yes')] [String] $IsSingleInstance, [Parameter()] [System.String] $Identity = 'Default', [Parameter()] [Boolean] $AllowClickThrough = $true, [Parameter()] [System.String[]] $BlockUrls = @(), [Parameter()] [Boolean] $EnableATPForSPOTeamsODB = $false, [Parameter()] [Boolean] $EnableSafeLinksForClients = $false, [Parameter()] [Boolean] $TrackClicks = $true, [Parameter()] [ValidateSet('Present')] [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 AtpPolicyForO365 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 $ValuesToCheck.Remove('IsSingleInstance') | Out-Null $ValuesToCheck.Remove('Verbose') | Out-Null $TestResult = Test-Microsoft365DSCParameterState -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 if (Confirm-ImportedCmdletIsAvailable -CmdletName Get-AtpPolicyForO365) { [array]$ATPPolicies = Get-AtpPolicyForO365 $dscContent = "" if ($ATPPolicies.Length -eq 0) { Write-Host $Global:M365DSCEmojiGreenCheckMark } else { Write-Host "`r`n" -NoNewLine } $i = 1 foreach ($atpPolicy in $ATPPolicies) { $Params = @{ IsSingleInstance = 'Yes' Identity = $atpPolicy.Identity GlobalAdminAccount = $GlobalAdminAccount ApplicationId = $ApplicationId TenantId = $TenantId CertificateThumbprint = $CertificateThumbprint CertificatePassword = $CertificatePassword CertificatePath = $CertificatePath } Write-Host " |---[$i/$($ATPPolicies.Length)] $($atpPolicy.Identiy)" -NoNewLine $Results = Get-TargetResource @Params if ($Results.Ensure -eq "Present") { $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++ } } else { Write-Host "`r`n $($Global:M365DSCEmojiYellowCircle) The current tenant is not registered to allow for ATP Policies" } return $dscContent } Export-ModuleMember -Function *-TargetResource |