DSCResources/MSFT_EXOSharingPolicy/MSFT_EXOSharingPolicy.psm1
function Get-TargetResource { [CmdletBinding()] [OutputType([System.Collections.Hashtable])] param ( [Parameter(Mandatory = $true)] [ValidateLength(1, 64)] [System.String] $Name, [Parameter()] [System.Boolean] $Enabled, [Parameter()] [System.String[]] $Domains, [Parameter()] [System.Boolean] $Default, [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 Sharing Policy configuration for $Name" #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 } $AllSharingPolicies = Get-SharingPolicy $SharingPolicy = $AllSharingPolicies | Where-Object -FilterScript { $_.Name -eq $Name } if ($null -eq $SharingPolicy) { Write-Verbose -Message "Sharing Policy $($Name) does not exist." $nullReturn = @{ Name = $Name Default = $Default Domains = $Domains Enabled = $Enabled Ensure = 'Absent' GlobalAdminAccount = $GlobalAdminAccount } return $nullReturn } else { $result = @{ Name = $SharingPolicy.Name Default = $SharingPolicy.Default Domains = $SharingPolicy.Domains Enabled = $SharingPolicy.Enabled Ensure = 'Present' GlobalAdminAccount = $GlobalAdminAccount } Write-Verbose -Message "Found Sharing Policy $($Name)" return $result } } function Set-TargetResource { [CmdletBinding()] param ( [Parameter(Mandatory = $true)] [ValidateLength(1, 64)] [System.String] $Name, [Parameter()] [System.Boolean] $Enabled, [Parameter()] [System.String[]] $Domains, [Parameter()] [System.Boolean] $Default, [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 "Setting Sharing Policy configuration for $Name" $currentSharingPolicyConfig = Get-TargetResource @PSBoundParameters #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 $NewSharingPolicyParams = @{ Name = $Name Domains = $Domains Enabled = $Enabled Default = $Default Confirm = $false } $SetSharingPolicyParams = @{ Identity = $Name Name = $Name Domains = $Domains Enabled = $Enabled Default = $Default Confirm = $false } # CASE: Sharing Policy doesn't exist but should; if ($Ensure -eq "Present" -and $currentSharingPolicyConfig.Ensure -eq "Absent") { Write-Verbose -Message "Sharing Policy '$($Name)' does not exist but it should. Create and configure it." # Create Sharing Policy New-SharingPolicy @NewSharingPolicyParams } # CASE: Sharing Policy exists but it shouldn't; elseif ($Ensure -eq "Absent" -and $currentSharingPolicyConfig.Ensure -eq "Present") { Write-Verbose -Message "Sharing Policy '$($Name)' exists but it shouldn't. Remove it." Remove-SharingPolicy -Identity $Name -Confirm:$false } # CASE: Sharing Policy exists and it should, but has different values than the desired ones elseif ($Ensure -eq "Present" -and $currentSharingPolicyConfig.Ensure -eq "Present") { Write-Verbose -Message "Sharing Policy '$($Name)' already exists, but needs updating." Write-Verbose -Message "Setting Sharing Policy $($Name) with values: $(Convert-M365DscHashtableToString -Hashtable $SetSharingPolicyParams)" Set-SharingPolicy @SetSharingPolicyParams } } function Test-TargetResource { [CmdletBinding()] [OutputType([System.Boolean])] param ( [Parameter(Mandatory = $true)] [ValidateLength(1, 64)] [System.String] $Name, [Parameter()] [System.Boolean] $Enabled, [Parameter()] [System.String[]] $Domains, [Parameter()] [System.Boolean] $Default, [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 Sharing Policy configuration for $Name" $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 ` -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 [array]$AllSharingPolicies = Get-SharingPolicy $dscContent = "" if ($AllSharingPolicies.Length -eq 0) { Write-Host $Global:M365DSCEmojiGreenCheckMark } else { Write-Host "`r`n" -NoNewLine } $i = 1 foreach ($SharingPolicy in $AllSharingPolicies) { Write-Host " |---[$i/$($AllSharingPolicies.Length)] $($SharingPolicy.Name)" -NoNewLine $Params = @{ Name = $SharingPolicy.Name 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 } Export-ModuleMember -Function *-TargetResource |