Public/Get-PiHoleHost.ps1
|
function Get-PiHoleHost { <# .SYNOPSIS Retrieves host information from the Pi-hole API. .DESCRIPTION Authenticates to the Pi-hole API and retrieves host information using the /info/host 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 SkipCertificateCheck Skip SSL certificate validation. Useful for self-signed certificates. .OUTPUTS The host information returned by the Pi-hole API. .EXAMPLE Get-PiHoleHost -BaseUrl 'http://pi.hole' -Credential $cred .EXAMPLE Get-PiHoleHost -BaseUrl 'https://pi.hole' -Credential $cred -SkipCertificateCheck #> [CmdletBinding()] param ( [Parameter(Mandatory = $true)] [string]$BaseUrl, [Parameter(Mandatory = $true)] [System.Management.Automation.PSCredential]$Credential, [switch]$SkipCertificateCheck ) begin { # Nothing to validate in the begin block } process { try { $sessionData = Connect-PiHole -BaseUrl $BaseUrl -Credential $Credential -SkipCertificateCheck:$SkipCertificateCheck $url = "$($BaseUrl)/api/info/host" 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 } } |