CMUtils.psm1

function Set-CMDistributionPointProperty {
    [CmdletBinding()]
    param (
        [string]$SiteCode,
        [switch]$EnableOnDemandDistribution,
        [switch]$EnableContentValidation,
        [datetime]$ValidationScheduleStartDate = (Get-Date -Year 2019 -Month 07 -Day 06 -Hour 21 -Minute 0 -Second 0),
        [ValidateSet("Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday")]
        [string]$ValidationScheduleDay = "Saturday",
        $ValidationSchedule = (New-CMSchedule -Start $ValidationScheduleStartDate -DayOfWeek $ValidationScheduleDay)
        
    )
    
    begin
    {
        # import cm module
        Import-Module ($Env:SMS_ADMIN_UI_PATH.Substring(0,$Env:SMS_ADMIN_UI_PATH.Length-5) + '\ConfigurationManager.psd1')
        
        If ($SiteCode)
        {
            Try
            {
                Set-Location $SiteCode
            }
            Catch
            {
                Write-Error "Could not change to the specified Site Code"
                Exit 10
            }
        }
        Else
        {
            # change to the cm site drive
            $PSD = Get-PSDrive -PSProvider CMSite
            Set-Location "$($PSD):"
            $DPs = Get-CMDistributionPoint -AllSite
        }
    }
    
    process
    {
        ForEach ($DP in $DPs)
        {
            $ServerName = If (($DP | Get-CMDistributionPointInfo).ServerName -ne '')
            {
                ($DP | Get-CMDistributionPointInfo).ServerName
            }
            Else
            {
                ($DP | Get-CMDistributionPointInfo).Name
            }
            
            $DPProperties = $DP.EmbeddedProperties

            #Region EnableOnDemandDistribution
            If ($EnableOnDemandDistribution)
            {
                If ($DP.EmbeddedProperties.ContainsKey("DistributeOnDemand") )
                {
                    If (($DPProperties["DistributeOnDemand"].Value) -eq '1')
                    {
                        Write-Information "DistributeOnDemand has already been set to 1 on $ServerName" -Tags "DistributeOnDemand,AlreadyEnabled"
                    }
                    Else
                    {
                        Try
                        {
                            Write-Information "Setting DistributeOnDemand to 1 on $ServerName" -Tags "DistributeOnDemand,PerformingChange"
                            $DPProperties["DistributeOnDemand"].Value = 1
                            $DP.EmbeddedProperties = $DPProperties
                            $DP.put()
                            Write-Information "DistributeOnDemand has been set to 1 on $ServerName" -Tags "DistributeOnDemand,ChangePerformed"
                        }
                        Catch
                        {
                            Write-Error "Failed to set DistributeOnDemand to 1 on $ServerName"
                        }
                    }  
                }
                Else
                {
                    Try
                    {
                        Write-Information "Adding DistributeOnDemand property and setting it to 1 on $ServerName" -Tags "DistributeOnDemand,PerformingChange"
                        $embeddedProperty = New-CMEmbeddedProperty -PropertyName "DistributeOnDemand" -Value 1
                        $DPProperties["DistributeOnDemand"] = $embeddedProperty
                        $DP.EmbeddedProperties = $DPProperties
                        $DP.put()
                        Write-Information "DistributeOnDemand has been added and set to 1 on $ServerName" -Tags "DistributeOnDemand,ChangePerformed"
                    }
                    Catch
                    {
                        Write-Error "Failed to add property DistributeOnDemand and set it to 1 on $ServerName"
                    }
                }
            }#Endregion EnableOnDemandDistribution

            #Region EnableContentValidation
            If ($EnableContentValidation)
            {
                If ((($DP | Get-CMDistributionPointInfo).HealthCheckEnabled) -like 'False')
                {

                    $DP |Set-CMDistributionPoint -EnableValidateContent $true -ContentValidationSchedule $ValidationSchedule
                }
                Else
                {
                    Write-Information "Content validation for $ServerName is already enabled"  -Tags "ContentValidation,AlreadyEnabled"
                }
                
            }#Endregion EnableContentValidation
        }
    }
    
    end
    {
    }
}