Public/Enable-PiHoleBlocking.ps1

function Enable-PiHoleBlocking {
    <#
    .SYNOPSIS
    Enables DNS blocking in Pi-hole.
 
    .DESCRIPTION
    This function authenticates to the Pi-hole API using Connect-PiHole, enables DNS blocking, and then disconnects the session using Disconnect-PiHole.
 
    .PARAMETER BaseUrl
    The base URL of the Pi-hole instance (e.g., http://pi.hole or https://pi.hole).
 
    .PARAMETER Credential
    A PSCredential object containing the Pi-hole password.
 
    .PARAMETER Timer
    Optional. Number of seconds until blocking mode is automatically changed back.
 
    .PARAMETER SkipCertificateCheck
    Skip SSL certificate validation. Useful for self-signed certificates.
 
    .EXAMPLE
    $cred = Get-Credential
    Enable-PiHoleBlocking -BaseUrl 'http://pi.hole' -Credential $cred
 
    .EXAMPLE
    $cred = Get-Credential
    Enable-PiHoleBlocking -BaseUrl 'https://pi.hole' -Credential $cred -SkipCertificateCheck
 
    .EXAMPLE
    Enable-PiHoleBlocking -BaseUrl 'http://pi.hole' -Credential $cred -Timer 300
    #>


    param (
        [Parameter(Mandatory = $true)]
        [string]$BaseUrl,

        [Parameter(Mandatory = $true)]
        [System.Management.Automation.PSCredential]$Credential,

        [Parameter(Mandatory = $false)]
        [int]$Timer,

        [switch]$SkipCertificateCheck
    )

    begin {
        # No validation needed - function now supports both HTTP and HTTPS
    }

    process {
        try {
            # Authenticate and get session data
            $sessionData = Connect-PiHole -BaseUrl $BaseUrl -Credential $Credential -SkipCertificateCheck:$SkipCertificateCheck

            # Prepare API request to enable blocking
            $url = "$BaseUrl/api/dns/blocking"
            $headers = @{
                'X-FTL-SID' = $sessionData.SID
                'Content-Type' = 'application/json'
            }

            $body = @{
                blocking = $true
                timer = if ($PSBoundParameters.ContainsKey('Timer')) { $Timer } else { $null }
            } | ConvertTo-Json

            $invokeParams = @{
                Uri         = $url
                Method      = 'Post'
                Headers     = $headers
                Body        = $body
                ErrorAction = 'Stop'
            }

            if ($SkipCertificateCheck) {
                $invokeParams.SkipCertificateCheck = $true
            }

            $response = Invoke-RestMethod @invokeParams

            return $response
        }
        catch {
            Write-Host "Error: $_" -ForegroundColor Red
            if ($sessionData) {
                Disconnect-PiHole -BaseUrl $BaseUrl -Id $sessionData.ID -SID $sessionData.SID -SkipCertificateCheck:$SkipCertificateCheck
            }
        }
        finally {
            if ($sessionData) {
                Disconnect-PiHole -BaseUrl $BaseUrl -Id $sessionData.ID -SID $sessionData.SID -SkipCertificateCheck:$SkipCertificateCheck
            }
        }
    }

    end {
        # Nothing to clean up in the end block
    }
}