Public/Get-PiHoleBlocking.ps1
|
function Get-PiHoleBlocking { <# .SYNOPSIS Retrieves the current blocking status from the Pi-hole API. .DESCRIPTION This function authenticates to the Pi-hole API using Connect-PiHole, retrieves the blocking status, 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 SkipCertificateCheck Skip SSL certificate validation. Useful for self-signed certificates. .OUTPUTS A PSCustomObject containing the blocking status, timer, and processing time. .EXAMPLE $cred = Get-Credential Get-PiHoleBlocking -BaseUrl 'http://pi.hole' -Credential $cred .EXAMPLE $cred = Get-Credential Get-PiHoleBlocking -BaseUrl 'https://pi.hole' -Credential $cred -SkipCertificateCheck #> param ( [Parameter(Mandatory = $true)] [string]$BaseUrl, [Parameter(Mandatory = $true)] [System.Management.Automation.PSCredential]$Credential, [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 get blocking status $url = "$BaseUrl/api/dns/blocking" $headers = @{ 'X-FTL-SID' = $sessionData.SID } $invokeParams = @{ Uri = $url Method = 'Get' Headers = $headers ErrorAction = 'Stop' } if ($SkipCertificateCheck) { $invokeParams.SkipCertificateCheck = $true } $response = Invoke-RestMethod @invokeParams # Process and return the response $blockingStatus = [PSCustomObject]@{ BlockingStatus = $response.blocking Timer = $response.timer Took = $response.took } return $blockingStatus } 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 } } |