diff --git a/PiHoleShell/PiHoleShell.psm1 b/PiHoleShell/PiHoleShell.psm1 index d371a2d..630fe8e 100644 --- a/PiHoleShell/PiHoleShell.psm1 +++ b/PiHoleShell/PiHoleShell.psm1 @@ -16,7 +16,7 @@ foreach ($File in $PrivateFunctions) { Export-ModuleMember -Function @( #Actions.ps1 - 'Update-PiHoleActionsGravity', 'Invoke-PiHoleFlushLog' ` + 'Update-PiHoleActionsGravity', 'Invoke-PiHoleFlushLog', 'Restart-PiHoleDnsService' ` #Authentication.ps1 'Remove-PiHoleCurrentAuthSession' , 'Get-PiHoleCurrentAuthSession', 'Remove-PiHoleAuthSession', ` #GroupManagement.ps1 diff --git a/PiHoleShell/Public/Actions.ps1 b/PiHoleShell/Public/Actions.ps1 index a45ddd8..d144f4d 100644 --- a/PiHoleShell/Public/Actions.ps1 +++ b/PiHoleShell/Public/Actions.ps1 @@ -123,3 +123,71 @@ Invoke-PiHoleFlushLogs -PiHoleServer "http://pihole.domain.com:8080" -Password " } } } + +function Restart-PiHoleDnsService { + <# +.SYNOPSIS +https://dns3.local:8489/api/docs/#post-/action/restartdns + +.DESCRIPTION +Restarts the Pi-hole DNS resolver (FTL). + +.PARAMETER PiHoleServer +The URL to the PiHole Server, for example "http://pihole.domain.com:8080", or "http://192.168.1.100" + +.PARAMETER Password +The API Password you generated from your PiHole server + +.PARAMETER IgnoreSsl +Set to $true to skip SSL certificate validation + +.PARAMETER RawOutput +This will dump the response instead of the formatted object + +.EXAMPLE +Invoke-PiHoleRestartDns -PiHoleServer "http://pihole.domain.com:8080" -Password "fjdsjfldsjfkldjslafjskdl" + #> + [CmdletBinding()] + [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseShouldProcessForStateChangingFunctions', '', Justification = 'Restarts PiHole DNS')] + [System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSAvoidUsingPlainTextForPassword", "Password")] + param ( + $PiHoleServer, + $Password, + [bool]$IgnoreSsl = $false, + [bool]$RawOutput = $false + ) + + try { + $Sid = Request-PiHoleAuth -PiHoleServer $PiHoleServer -Password $Password -IgnoreSsl $IgnoreSsl + + $Params = @{ + Headers = @{sid = $($Sid) } + Uri = "$PiHoleServer/api/action/restartdns" + Method = "Post" + ContentType = "application/json" + SkipCertificateCheck = $IgnoreSsl + } + + $Response = Invoke-RestMethod @Params + + if ($RawOutput) { + Write-Output $Response + } + else { + $Object = [PSCustomObject]@{ + Status = "Restarted" + } + Write-Output $Object + } + } + + catch { + Write-Error -Message $_.Exception.Message + } + + finally { + if ($Sid) { + Remove-PiHoleCurrentAuthSession -PiHoleServer $PiHoleServer -Sid $Sid -IgnoreSsl $IgnoreSsl + } + } +} \ No newline at end of file