CMUtils.psm1

<#
.Synopsis
   This module contains functions related to Microsoft System Center Configuration Manager (Current Branch)
.DESCRIPTION
   You can use it to modify properties of multiple Distribution Points at once
.EXAMPLE
   Set-CMDistributionPointProperty -EnableOnDemandDistribution
.EXAMPLE
    Set-CMDistributionPointProperty -SiteCode 'ABC' -EnableContentValidation
    Set-CMDistributionPointProperty -EnableContentValidation
.FUNCTIONALITY
   You can use it to modify properties of multiple Distribution Points at once
.CHANGELOG
    1.0 - 09.07.2019 - Set-CMDistributionPointProperty with EnableOnDemandDistribution
    1.1 - 09.07.2019 - Added EnableContentValidation to Set-CMDistributionPointProperty, first public release
    1.1.1 - 10.07.2019 - Added ContentMonitoringPriority parameter to Set-CMDistributionPointProperty, this changelog and basic description
    1.2.0 - 10.07.2019 - Added EnableBranchCache to Set-CMDistributionPointProperty
    1.3.0 - 10.07.2019 - Added EnableLEDBAT to Set-CMDistributionPointProperty
#>


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,
        [ValidateSet("Lowest","Low","Medium","High","Highest")]
        [string]$ContentMonitoringPriority="Lowest",
        [switch]$EnableBranchCache,
        [switch]$EnableLEDBAT
        
    )
    
    begin
    {
        # import cm module
        Import-Module ($Env:SMS_ADMIN_UI_PATH.Substring(0,$Env:SMS_ADMIN_UI_PATH.Length-5) + '\ConfigurationManager.psd1')
        
        # change to the cm site drive
        $PSD = Get-PSDrive -PSProvider CMSite
        Set-Location "$($PSD):"
        If ($SiteCode)
        {
            Try
            {
                $DPs = Get-CMDistributionPoint -SiteCode $SiteCode
            }
            Catch
            {
                Write-Error "Could not get distribution points for the specified Site Code"
            }
        }
        Else
        {
            $DPs = Get-CMDistributionPoint -AllSite
        }
    }
    
    process
    {
        ForEach ($DP in $DPs)
        {
            $DPInfo = $DP | Get-CMDistributionPointInfo
            $DPProperties = $DP.EmbeddedProperties
            $ServerName = If (($DPInfo).ServerName -ne '')
            {
                ($DPInfo).ServerName
            }
            Else
            {
                ($DPInfo).Name
            }

            #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 (!($ValidationSchedule))
                {
                    $ValidationSchedule = (New-CMSchedule -Start $ValidationScheduleStartDate -DayOfWeek $ValidationScheduleDay)
                }

                If ((($DPInfo).HealthCheckEnabled) -like 'False')
                {
                    Write-Information "Enabling Content Validation on $ServerName" -Tags "ContentValidation,PerformingChange"
                    $DP |Set-CMDistributionPoint -EnableValidateContent $true -ContentValidationSchedule $ValidationSchedule -ContentMonitoringPriority $ContentMonitoringPriority
                }
                Else
                {
                    Write-Information "Content validation for $ServerName is already enabled"  -Tags "ContentValidation,AlreadyEnabled"
                }
                
            }#Endregion EnableContentValidation

            #Region EnableBranchCache
            If ($EnableBranchCache)
            {
                If (($DPProperties["UpdateBranchCacheKey"].Value) -eq '0')
                {
                    Write-Information "Enabling BranchCache on $ServerName" -Tags "EnableBranchCache,PerformingChange"
                    $DP |Set-CMDistributionPoint -EnableBranchCache $true
                }
                Else
                {
                    Write-Information "BranchCache for $ServerName is already enabled"  -Tags "EnableBranchCache,AlreadyEnabled"
                }
            }#Endregion EnableBranchCache

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

        }                   
    }
    end
    {
    }
}