Public/Get-PiHoleConfig.ps1
|
function Get-PiHoleConfig { <# .SYNOPSIS Get current configuration of your Pi-hole. .DESCRIPTION Gets the current configuration of your Pi-hole using the /config API endpoint. .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 Detailed (Optional) If specified, returns detailed information about the configuration. Default is $false. .PARAMETER SkipCertificateCheck Skip SSL certificate validation. Useful for self-signed certificates. .OUTPUTS The configuration data returned by the Pi-hole API. .EXAMPLE Get-PiHoleConfig -BaseUrl 'https://pi.hole' -Credential $cred -SkipCertificateCheck .EXAMPLE Get-PiHoleConfig -BaseUrl 'http://pi.hole' -Credential $cred -Detailed #> [CmdletBinding()] param ( [Parameter(Mandatory = $true)] [string]$BaseUrl, [Parameter(Mandatory = $true)] [System.Management.Automation.PSCredential]$Credential, [Parameter(Mandatory = $false)] [switch]$Detailed, [switch]$SkipCertificateCheck ) begin { # Nothing to validate in the begin block } process { try { $sessionData = Connect-PiHole -BaseUrl $BaseUrl -Credential $Credential -SkipCertificateCheck:$SkipCertificateCheck $detailedValue = if ($Detailed.IsPresent) { $true } else { $false } $url = "$($BaseUrl)/api/config?detailed=$($detailedValue)" Write-Verbose "Request URL: $url" $headers = @{ 'X-FTL-SID' = $sessionData.SID; 'Accept' = 'application/json' } $invokeParams = @{ Uri = $url Method = 'Get' Headers = $headers 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 } } |