From 1d9f74554b8d2230dd4c694abd424b81f0d8dc38 Mon Sep 17 00:00:00 2001 From: Mike Madeja Date: Sun, 15 Jun 2025 23:26:12 -0500 Subject: [PATCH 1/8] debugging --- PiHoleShell/PiHoleShell.psd1 | Bin 2136 -> 2364 bytes 1 file changed, 0 insertions(+), 0 deletions(-) diff --git a/PiHoleShell/PiHoleShell.psd1 b/PiHoleShell/PiHoleShell.psd1 index 7d2fbdd9777772fedf961de008fc7b66729939b2..88f8c07c69110e1892b13242d39da11f4dd2c9a1 100644 GIT binary patch delta 144 zcmca1ut#XaK9lLjsc7;cK%E5)E Date: Sun, 15 Jun 2025 23:27:06 -0500 Subject: [PATCH 2/8] debugging --- PiHoleShell/PiHoleShell.psd1 | Bin 2364 -> 2454 bytes 1 file changed, 0 insertions(+), 0 deletions(-) diff --git a/PiHoleShell/PiHoleShell.psd1 b/PiHoleShell/PiHoleShell.psd1 index 88f8c07c69110e1892b13242d39da11f4dd2c9a1..f33a7ea6b33aee3e18404320e980f73b663031c5 100644 GIT binary patch delta 38 lcmdlZG);KJCT7Nj$sgHeCvRddfU=ZW7C=~=53y9S000>04gdfE delta 36 ncmbOxyhmumCg#a)EMk+NF-HMuiOFRw9bo<{R<6x9tWhig{Syp< From 459ea5bd9494c1a8e76eb7e34df88c8174f5757a Mon Sep 17 00:00:00 2001 From: Mike Madeja Date: Sun, 15 Jun 2025 23:37:59 -0500 Subject: [PATCH 3/8] debugging --- PiHoleShell/PiHoleShell.psd1 | Bin 2454 -> 2474 bytes PiHoleShell/Private/Misc.ps1 | 45 +------------------------- PiHoleShell/Public/Authentication.ps1 | 15 +++++++-- PiHoleShell/Public/DnsControl.ps1 | 5 ++- PiHoleShell/Public/Metrics.ps1 | 2 +- 5 files changed, 18 insertions(+), 49 deletions(-) diff --git a/PiHoleShell/PiHoleShell.psd1 b/PiHoleShell/PiHoleShell.psd1 index f33a7ea6b33aee3e18404320e980f73b663031c5..50cbffefb41652bc5472b8efe79f9dfb0a838bde 100644 GIT binary patch delta 24 gcmbOxyh?ZjAM<1<7Lmypn8hZ0F$--rV@_ZL09};^)c^nh delta 12 TcmZ1_JWY55AM<7<<_JY diff --git a/PiHoleShell/Private/Misc.ps1 b/PiHoleShell/Private/Misc.ps1 index 8ac74df..b0c7772 100644 --- a/PiHoleShell/Private/Misc.ps1 +++ b/PiHoleShell/Private/Misc.ps1 @@ -45,47 +45,4 @@ function Convert-LocalTimeToPiHoleUnixTime { $ObjectFinal = $Object Write-Output $ObjectFinal -} - -# function Test-HttpPrefixForPiHole { -# param ( -# [Parameter(Mandatory)] -# [string]$Url -# ) - -# Write-Output ($Url -match '^https?://') -# } - -# function Test-PiHoleServerAccess { -# param ( -# [Parameter(Mandatory)] -# [string]$Url, -# [bool]$IgnoreSsl = $false -# ) - -# if (Test-HttpPrefixForPiHole -Url $Url) { -# $RawOutput = Invoke-WebRequest -Uri "$Url/admin/login" -Method Head -TimeoutSec 5 -ErrorAction Stop -SkipCertificateCheck -# } -# } - -# function Convert-EnabledBoolToString { -# param ( -# [bool]$Bool -# ) - -# switch ($Bool) { -# $false { -# $Enabled = "false" -# } -# $true { -# $Enabled = "true" -# } -# } - -# $Object = [PSCustomObject]@{ -# Bool = $Enabled -# } - -# Write-Output $Object - -# } \ No newline at end of file +} \ No newline at end of file diff --git a/PiHoleShell/Public/Authentication.ps1 b/PiHoleShell/Public/Authentication.ps1 index 6eb1263..7678d25 100644 --- a/PiHoleShell/Public/Authentication.ps1 +++ b/PiHoleShell/Public/Authentication.ps1 @@ -39,6 +39,12 @@ The URL to the PiHole Server, for example "http://pihole.domain.com:8080", or "h .PARAMETER Password The API Password you generated from your PiHole v6 server +.PARAMETER IgnoreSsl +Ignore SSL when interacting with the PiHole API + +.PARAMETER RawOutput +This will dump the response instead of the formatted object + .EXAMPLE Get-PiHoleCurrentAuthSession -PiHoleServer "http://pihole.domain.com:8080" -Password "fjdsjfldsjfkldjslafjskdl" #> @@ -46,7 +52,7 @@ Get-PiHoleCurrentAuthSession -PiHoleServer "http://pihole.domain.com:8080" -Pass [System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSAvoidUsingPlainTextForPassword", "Password")] param ( [string]$PiHoleServer, - $Password, + [string]$Password, [bool]$IgnoreSsl = $false, [bool]$RawOutput = $false ) @@ -143,6 +149,9 @@ The URL to the PiHole Server, for example "http://pihole.domain.com:8080", or "h .PARAMETER Password The API Password you generated from your PiHole server +.PARAMETER IgnoreSsl +Ignore SSL when interacting with the PiHole API + .EXAMPLE Get-PiHoleCurrentAuthSession -PiHoleServer "http://pihole.domain.com:8080" -Password "fjdsjfldsjfkldjslafjskdl" #> @@ -150,8 +159,8 @@ Get-PiHoleCurrentAuthSession -PiHoleServer "http://pihole.domain.com:8080" -Pass [System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSAvoidUsingPlainTextForPassword", "Password")] param ( $PiHoleServer, - $Password, - $IgnoreSsl, + [string]$Password, + [bool]$IgnoreSsl = $false, [int]$Id ) diff --git a/PiHoleShell/Public/DnsControl.ps1 b/PiHoleShell/Public/DnsControl.ps1 index cca20bf..eea3e6c 100644 --- a/PiHoleShell/Public/DnsControl.ps1 +++ b/PiHoleShell/Public/DnsControl.ps1 @@ -1,7 +1,7 @@ function Get-PiHoleDnsBlockingStatus { <# .SYNOPSIS -https://ftl.pi-hole.net/development-v6/docs/#get-/dns/blocking +Get current blocking status .PARAMETER PiHoleServer The URL to the PiHole Server, for example "http://pihole.domain.com:8080", or "http://192.168.1.100" @@ -9,6 +9,9 @@ The URL to the PiHole Server, for example "http://pihole.domain.com:8080", or "h .PARAMETER Password The API Password you generated from your PiHole server +.PARAMETER IgnoreSsl +Ignore SSL when interacting with the PiHole API + .PARAMETER RawOutput This will dump the response instead of the formatted object diff --git a/PiHoleShell/Public/Metrics.ps1 b/PiHoleShell/Public/Metrics.ps1 index b012841..30c26cb 100644 --- a/PiHoleShell/Public/Metrics.ps1 +++ b/PiHoleShell/Public/Metrics.ps1 @@ -191,7 +191,7 @@ The URL to the PiHole Server, for example "http://pihole.domain.com:8080", or "h .PARAMETER Password The API Password you generated from your PiHole server -.PARAMETER IgnoreSsl + This will dump the response instead of the formatted object .PARAMETER RawOutput From 495c6042d7d1432a5d3cc2d7a4463fac8e2e212f Mon Sep 17 00:00:00 2001 From: Mike Madeja Date: Mon, 16 Jun 2025 00:03:53 -0500 Subject: [PATCH 4/8] updated --- PiHoleShell/PiHoleShell.psd1 | Bin 2474 -> 2658 bytes 1 file changed, 0 insertions(+), 0 deletions(-) diff --git a/PiHoleShell/PiHoleShell.psd1 b/PiHoleShell/PiHoleShell.psd1 index 50cbffefb41652bc5472b8efe79f9dfb0a838bde..a0ec0ecd5c6e26cc7f6f3f420806a7038a361ee7 100644 GIT binary patch delta 188 zcmZ1_{77U&1k>b8Y(kT#G4-e`Gq^JpG2}CpG88cQG9)tO0dYD*Dnl+1=P{Hp=mEux z84M@KbBInhU=^Dj$1F72i+Pr+GJ^|HzL>!os28LTWJV55HG={K0(HrPb^T(l5LJe% lO=HLe+JG<@qN0Okg}5@%1*u@OG8vMAW+SQDY`|K@0suo`Dqa8p delta 88 zcmaDPvPyVE1e3M`g93vx5C<^iGn6x=G88ccGh_hi93WI+FlW#PlKBjIKwjnKaHjc_ fU$BZz&SMsu9K}2f%=Tfa0Mk6ID>lDitzrQHi`N&2 From a618011ae9bfc07c4b81fc06f9cc2a8a431333cf Mon Sep 17 00:00:00 2001 From: Mike Madeja Date: Mon, 16 Jun 2025 00:20:59 -0500 Subject: [PATCH 5/8] debugging --- PiHoleShell/PiHoleShell.psd1 | Bin 2658 -> 2694 bytes PiHoleShell/Public/Authentication.ps1 | 8 ++++++-- PiHoleShell/Public/Config.ps1 | 6 ++++-- PiHoleShell/Public/DnsControl.ps1 | 4 ++-- PiHoleShell/Public/GroupManagement.ps1 | 6 +++--- PiHoleShell/Public/Metrics.ps1 | 8 ++++---- 6 files changed, 19 insertions(+), 13 deletions(-) diff --git a/PiHoleShell/PiHoleShell.psd1 b/PiHoleShell/PiHoleShell.psd1 index a0ec0ecd5c6e26cc7f6f3f420806a7038a361ee7..84fb1ce81658d43e46c93cd4a10f086033db82d3 100644 GIT binary patch delta 46 xcmaDP(k8m$8>3 Date: Mon, 16 Jun 2025 16:58:47 -0500 Subject: [PATCH 6/8] debugging --- PiHoleShell/PiHoleShell.psd1 | Bin 2694 -> 3022 bytes PiHoleShell/Public/Authentication.ps1 | 9 +++++---- PiHoleShell/Public/Config.ps1 | 2 +- PiHoleShell/Public/DnsControl.ps1 | 8 ++++---- PiHoleShell/Public/GroupManagement.ps1 | 12 ++++++------ PiHoleShell/Public/Metrics.ps1 | 16 ++++++++-------- 6 files changed, 24 insertions(+), 23 deletions(-) diff --git a/PiHoleShell/PiHoleShell.psd1 b/PiHoleShell/PiHoleShell.psd1 index 84fb1ce81658d43e46c93cd4a10f086033db82d3..72e64dfd82697802559cd4cee7b259b7a5a10859 100644 GIT binary patch delta 109 zcmZn@Jtw|FhFRK?p_HM7A%h{6ArDArG9)u3g6VvQyvdf#@{?Owr6xzPsxc}|u4j`5 zvXc1K8G?YS3xWDl8HyQ7Cik<7GpjQsOm<`zoh-n@HCcw)YT^Zs$vkXgllL(TZLVYH GUn-X^+1hIw)fi`C|NEC%cVFf9cM diff --git a/PiHoleShell/Public/Authentication.ps1 b/PiHoleShell/Public/Authentication.ps1 index 882dfc3..4986107 100644 --- a/PiHoleShell/Public/Authentication.ps1 +++ b/PiHoleShell/Public/Authentication.ps1 @@ -18,6 +18,7 @@ function Request-PiHoleAuth { } $Response = Invoke-RestMethod @Params -Verbose: $false + Write-Verbose -Message "Request-PiHoleAuth Successful!" Write-Output $Response.session.sid } @@ -63,7 +64,7 @@ Get-PiHoleCurrentAuthSession -PiHoleServer "http://pihole.domain.com:8080" -Pass $Params = @{ Headers = @{sid = $($Sid) } - Uri = "$PiHoleServer/api/auth/sessions" + Uri = "$($PiHoleServer.OriginalString)/api/auth/sessions" Method = "Get" SkipCertificateCheck = $IgnoreSsl ContentType = "application/json" @@ -115,7 +116,7 @@ Get-PiHoleCurrentAuthSession -PiHoleServer "http://pihole.domain.com:8080" -Pass } function Remove-PiHoleCurrentAuthSession { - [Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSUseShouldProcessForStateChangingFunctions", "", Justification = "It removes sessions from PiHole only")] + #[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSUseShouldProcessForStateChangingFunctions", "", Justification = "It removes sessions from PiHole only")] #INTERNAL FUNCTION [CmdletBinding()] param ( @@ -125,7 +126,7 @@ function Remove-PiHoleCurrentAuthSession { ) $Params = @{ Headers = @{sid = $($Sid) } - Uri = "$PiHoleServer/api/auth" + Uri = "$($PiHoleServer.OriginalString)/api/auth" Method = "Delete" SkipCertificateCheck = $IgnoreSsl ContentType = "application/json" @@ -172,7 +173,7 @@ Get-PiHoleCurrentAuthSession -PiHoleServer "http://pihole.domain.com:8080" -Pass $Sid = Request-PiHoleAuth -PiHoleServer $PiHoleServer -Password $Password -IgnoreSsl $IgnoreSsl $Params = @{ Headers = @{sid = $($Sid) } - Uri = "$PiHoleServer/api/auth/session/$Id" + Uri = "$($PiHoleServer.OriginalString)/api/auth/session/$Id" Method = "Delete" SkipCertificateCheck = $IgnoreSsl ContentType = "application/json" diff --git a/PiHoleShell/Public/Config.ps1 b/PiHoleShell/Public/Config.ps1 index 585484c..1ca4ad0 100644 --- a/PiHoleShell/Public/Config.ps1 +++ b/PiHoleShell/Public/Config.ps1 @@ -19,7 +19,7 @@ https://TODO $Sid = Request-PiHoleAuth -PiHoleServer $PiHoleServer -Password $Password -IgnoreSsl $IgnoreSsl $Params = @{ Headers = @{sid = $($Sid) } - Uri = "$PiHoleServer/api/config" + Uri = "$($PiHoleServer.OriginalString)/api/config" Method = "Get" SkipCertificateCheck = $IgnoreSsl ContentType = "application/json" diff --git a/PiHoleShell/Public/DnsControl.ps1 b/PiHoleShell/Public/DnsControl.ps1 index 634b718..0f214c6 100644 --- a/PiHoleShell/Public/DnsControl.ps1 +++ b/PiHoleShell/Public/DnsControl.ps1 @@ -21,7 +21,7 @@ Get-PiHoleDnsBlockingStatus -PiHoleServer "http://pihole.domain.com:8080" -Passw [System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSAvoidUsingPlainTextForPassword", "Password")] param ( [Parameter(Mandatory = $true)] - [System.URI]$PiHoleServer + [System.URI]$PiHoleServer, [Parameter(Mandatory = $true)] [string]$Password, [bool]$IgnoreSsl = $false, @@ -32,7 +32,7 @@ Get-PiHoleDnsBlockingStatus -PiHoleServer "http://pihole.domain.com:8080" -Passw $Params = @{ Headers = @{sid = $($Sid) } - Uri = "$PiHoleServer/api/dns/blocking" + Uri = "$($PiHoleServer.OriginalString)/api/dns/blocking" Method = "Get" ContentType = "application/json" SkipCertificateCheck = $IgnoreSsl @@ -97,7 +97,7 @@ Set-PiHoleDnsBlocking -PiHoleServer "http://pihole.domain.com:8080" -Password "f [System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSAvoidUsingPlainTextForPassword", "Password")] param ( [Parameter(Mandatory = $true)] - [System.URI]$PiHoleServer + [System.URI]$PiHoleServer, [Parameter(Mandatory = $true)] [string]$Password, [ValidateSet("True", "False")] @@ -117,7 +117,7 @@ Set-PiHoleDnsBlocking -PiHoleServer "http://pihole.domain.com:8080" -Password "f Headers = @{sid = $($Sid) Accept = "application/json" } - Uri = "$PiHoleServer/api/dns/blocking" + Uri = "$($PiHoleServer.OriginalString)/api/dns/blocking" Method = "Post" ContentType = "application/json" Body = $Body diff --git a/PiHoleShell/Public/GroupManagement.ps1 b/PiHoleShell/Public/GroupManagement.ps1 index 4a1bcab..a7d3c7a 100644 --- a/PiHoleShell/Public/GroupManagement.ps1 +++ b/PiHoleShell/Public/GroupManagement.ps1 @@ -96,7 +96,7 @@ https://TODO [System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSAvoidUsingPlainTextForPassword", "Password")] param ( [Parameter(Mandatory = $true)] - [System.URI]$PiHoleServer + [System.URI]$PiHoleServer, [Parameter(Mandatory = $true)] [string]$Password, [Parameter(Mandatory = $true)] @@ -125,7 +125,7 @@ https://TODO $Params = @{ Headers = @{sid = $($Sid) } - Uri = "$PiHoleServer/api/groups" + Uri = "$($PiHoleServer.OriginalString)/api/groups" Method = "Post" SkipCertificateCheck = $IgnoreSsl ContentType = "application/json" @@ -176,7 +176,7 @@ https://TODO [Diagnostics.CodeAnalysis.SuppressMessage("PSUseShouldProcessForStateChangingFunctions", "", Justification = "Ignoring for now")] param ( [Parameter(Mandatory = $true)] - [System.URI]$PiHoleServer + [System.URI]$PiHoleServer, [Parameter(Mandatory = $true)] [string]$Password, [Parameter(Mandatory = $true)] @@ -229,7 +229,7 @@ https://TODO $Params = @{ Headers = @{sid = $($Sid) } - Uri = "$PiHoleServer/api/groups/$GroupName" + Uri = "$($PiHoleServer.OriginalString)/api/groups/$GroupName" Method = "Put" SkipCertificateCheck = $IgnoreSsl ContentType = "application/json" @@ -282,7 +282,7 @@ https://TODO [System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSAvoidUsingPlainTextForPassword", "Password")] param ( [Parameter(Mandatory = $true)] - [System.URI]$PiHoleServer + [System.URI]$PiHoleServer, [Parameter(Mandatory = $true)] [string]$Password, [Parameter(Mandatory = $true)] @@ -300,7 +300,7 @@ https://TODO $Params = @{ Headers = @{sid = $($Sid) } - Uri = "$PiHoleServer/api/groups/$GroupName" + Uri = "$($PiHoleServer.OriginalString)/api/groups/$GroupName" Method = "Delete" SkipCertificateCheck = $IgnoreSsl ContentType = "application/json" diff --git a/PiHoleShell/Public/Metrics.ps1 b/PiHoleShell/Public/Metrics.ps1 index a292de6..221ef7d 100644 --- a/PiHoleShell/Public/Metrics.ps1 +++ b/PiHoleShell/Public/Metrics.ps1 @@ -23,7 +23,7 @@ Get-PiHoleStatsRecentBlocked -PiHoleServer "http://pihole.domain.com:8080" -Pass [System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSAvoidUsingPlainTextForPassword", "Password")] param ( [Parameter(Mandatory = $true)] - [System.URI]$PiHoleServer + [System.URI]$PiHoleServer, [Parameter(Mandatory = $true)] [string]$Password, [int]$MaxResult = 1, @@ -35,7 +35,7 @@ Get-PiHoleStatsRecentBlocked -PiHoleServer "http://pihole.domain.com:8080" -Pass Write-Verbose -Message "MaxResults - $MaxResult" $Params = @{ Headers = @{sid = $($Sid) } - Uri = "$PiHoleServer/api/stats/recent_blocked?count=$MaxResult" + Uri = "$($PiHoleServer.OriginalString)/api/stats/recent_blocked?count=$MaxResult" Method = "Get" SkipCertificateCheck = $IgnoreSsl ContentType = "application/json" @@ -81,7 +81,7 @@ https://TODOFINDNEWAPILINK [System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSAvoidUsingPlainTextForPassword", "Password")] param ( [Parameter(Mandatory = $true)] - [System.URI]$PiHoleServer + [System.URI]$PiHoleServer, [Parameter(Mandatory = $true)] [string]$Password, [bool]$IgnoreSsl = $false, @@ -92,7 +92,7 @@ https://TODOFINDNEWAPILINK Write-Verbose -Message "MaxResults - $MaxResult" $Params = @{ Headers = @{sid = $($Sid) } - Uri = "$PiHoleServer/api/stats/query_types" + Uri = "$($PiHoleServer.OriginalString)/api/stats/query_types" Method = "Get" SkipCertificateCheck = $IgnoreSsl ContentType = "application/json" @@ -136,7 +136,7 @@ https://TODOFINDNEWAPILINK [System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSAvoidUsingPlainTextForPassword", "Password")] param ( [Parameter(Mandatory = $true)] - [System.URI]$PiHoleServer + [System.URI]$PiHoleServer, [Parameter(Mandatory = $true)] [string]$Password, [int]$MaxResult = 10, @@ -162,7 +162,7 @@ https://TODOFINDNEWAPILINK $Params = @{ Headers = @{sid = $($Sid) } - Uri = "$PiHoleServer/api/stats/top_domains?blocked=$Blocked&count=$MaxResult" + Uri = "$($PiHoleServer.OriginalString)/api/stats/top_domains?blocked=$Blocked&count=$MaxResult" Method = "Get" SkipCertificateCheck = $IgnoreSsl ContentType = "application/json" @@ -204,7 +204,7 @@ Get-PiHoleStatsSummary -PiHoleServer "http://pihole.domain.com:8080" -Password " [System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSAvoidUsingPlainTextForPassword", "Password")] param ( [Parameter(Mandatory = $true)] - [System.URI]$PiHoleServer + [System.URI]$PiHoleServer, [Parameter(Mandatory = $true)] [string]$Password, [bool]$IgnoreSsl = $false, @@ -215,7 +215,7 @@ Get-PiHoleStatsSummary -PiHoleServer "http://pihole.domain.com:8080" -Password " $Params = @{ Headers = @{sid = $($Sid) } - Uri = "$PiHoleServer/api/stats/summary" + Uri = "$($PiHoleServer.OriginalString)/api/stats/summary" Method = "Get" SkipCertificateCheck = $IgnoreSsl ContentType = "application/json" From 797b86a9726ed56695fa38cea6f545ba07b6a225 Mon Sep 17 00:00:00 2001 From: Mike Madeja Date: Mon, 16 Jun 2025 17:18:03 -0500 Subject: [PATCH 7/8] debugging --- PiHoleShell/PiHoleShell.psd1 | Bin 3022 -> 2982 bytes PiHoleShell/Public/DnsControl.ps1 | 11 +++--- tests/Set-PiHoleDnsBlocking.Tests.ps1 | 54 ++++++++++++++++++++++++++ 3 files changed, 60 insertions(+), 5 deletions(-) create mode 100644 tests/Set-PiHoleDnsBlocking.Tests.ps1 diff --git a/PiHoleShell/PiHoleShell.psd1 b/PiHoleShell/PiHoleShell.psd1 index 72e64dfd82697802559cd4cee7b259b7a5a10859..b2e00a6ded0274799949d8530c9e0ff061db2537 100644 GIT binary patch delta 16 XcmX>nzD#_B2+L$AR)x(mEJf@9FVF<> delta 40 ucmZ1`eolOY2n)YDLl8qMLm@*ckSt~>nY^A&oLQYAVKP6v>}DgD5_SO34GEn9 diff --git a/PiHoleShell/Public/DnsControl.ps1 b/PiHoleShell/Public/DnsControl.ps1 index 0f214c6..146247d 100644 --- a/PiHoleShell/Public/DnsControl.ps1 +++ b/PiHoleShell/Public/DnsControl.ps1 @@ -114,13 +114,14 @@ Set-PiHoleDnsBlocking -PiHoleServer "http://pihole.domain.com:8080" -Password "f $Body = "{`"blocking`":$Blocking,`"timer`":$TimeInSeconds}" $Params = @{ - Headers = @{sid = $($Sid) + Headers = @{sid = $($Sid) Accept = "application/json" } - Uri = "$($PiHoleServer.OriginalString)/api/dns/blocking" - Method = "Post" - ContentType = "application/json" - Body = $Body + Uri = "$($PiHoleServer.OriginalString)/api/dns/blocking" + Method = "Post" + ContentType = "application/json" + Body = $Body + SkipCertificateCheck = $IgnoreSsl } $Response = Invoke-RestMethod @Params diff --git a/tests/Set-PiHoleDnsBlocking.Tests.ps1 b/tests/Set-PiHoleDnsBlocking.Tests.ps1 new file mode 100644 index 0000000..4f31a2f --- /dev/null +++ b/tests/Set-PiHoleDnsBlocking.Tests.ps1 @@ -0,0 +1,54 @@ +# Requires -Module Pester +Describe 'Set-PiHoleDnsBlocking' { + BeforeAll { + # Sample input values + $server = [uri]'http://pihole.local' + $password = 'mock-password' + $sid = 'mock-session-id' + + # Mock external functions + Mock -CommandName Request-PiHoleAuth -MockWith { return $sid } + Mock -CommandName Remove-PiHoleCurrentAuthSession + Mock -CommandName Format-PiHoleSecond -MockWith { + return @{ TimeInSeconds = 60 } + } + + # Mock response from API + Mock -CommandName Invoke-RestMethod -MockWith { + return @{ + blocking = 'false' + timer = 60 + } + } + } + + It 'should call Request-PiHoleAuth and send correct POST body' { + Set-PiHoleDnsBlocking -PiHoleServer $server -Password $password -Blocking 'False' -TimeInSeconds 60 | Out-Null + + # Assert internal functions were called correctly + Assert-MockCalled Request-PiHoleAuth -Times 1 -Exactly -Scope It + Assert-MockCalled Invoke-RestMethod -Times 1 -Scope It + Assert-MockCalled Remove-PiHoleCurrentAuthSession -Times 1 -Scope It + } + + It 'should return a formatted PSCustomObject if RawOutput is $false' { + $result = Set-PiHoleDnsBlocking -PiHoleServer $server -Password $password -Blocking 'False' -TimeInSeconds 60 + $result | Should -BeOfType 'System.Object[]' + $result[0].Blocking | Should -Be 'false' + $result[0].TimeInSeconds | Should -Be 60 + } + + It 'should return raw response if RawOutput is $true' { + $result = Set-PiHoleDnsBlocking -PiHoleServer $server -Password $password -Blocking 'False' -TimeInSeconds 60 -RawOutput + $result | Should -HaveProperty 'blocking' + $result | Should -HaveProperty 'timer' + } + + It 'should handle errors and output them' { + # Mock to simulate error + Mock -CommandName Invoke-RestMethod -MockWith { throw "Test error" } -ParameterFilter { $Body -like '*' } + + { Set-PiHoleDnsBlocking -PiHoleServer $server -Password $password -Blocking 'False' } | + Should -Throw -ErrorMessage 'Test error' + } +} From 4dd701f676fe5ca18df19bc982116d556b07a61b Mon Sep 17 00:00:00 2001 From: Mike Madeja Date: Mon, 16 Jun 2025 17:23:24 -0500 Subject: [PATCH 8/8] debugging --- PiHoleShell/PiHoleShell.psd1 | Bin 2982 -> 2974 bytes PiHoleShell/Public/Authentication.ps1 | 2 +- 2 files changed, 1 insertion(+), 1 deletion(-) diff --git a/PiHoleShell/PiHoleShell.psd1 b/PiHoleShell/PiHoleShell.psd1 index b2e00a6ded0274799949d8530c9e0ff061db2537..63feccc6e0d94756d7f5dd14126e5883d9967379 100644 GIT binary patch delta 16 XcmZ1`K2Ll@80+LH7NgA*SjE@?F#iPw delta 28 gcmbOyzD#^W7%RI10~k&=