Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ jobs:
name: Analyze & Test
runs-on: windows-latest
strategy:
fail-fast: false
matrix:
psversion: ['5.1', '7.4']
steps:
Expand Down
6 changes: 5 additions & 1 deletion Modules/Core/PrinterToolkit.Core.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,11 @@ function Get-Printers {
[CmdletBinding()]
[OutputType([array])]
param()
return @(Get-Printer -ErrorAction SilentlyContinue)
try {
return ,@(Get-Printer -ErrorAction SilentlyContinue)
} catch {
return ,@()
}
Comment on lines +147 to +151

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎯 Functional Correctness | 🟠 Major | ⚡ Quick win

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
set -euo pipefail

# Locate the referenced files and inspect the surrounding lines.
for f in Modules/Core/PrinterToolkit.Core.psm1 Modules/Reporting/PrinterToolkit.Reporting.psm1 Modules/Sharing/PrinterToolkit.Sharing.psm1 Modules/Utilities/PrinterToolkit.Utilities.psm1; do
  echo "===== $f ====="
  if [ -f "$f" ]; then
    wc -l "$f"
    sed -n '1,260p' "$f" | nl -ba | sed -n '130,170p;210,235p;1,170p'
  else
    echo "missing"
  fi
  echo
done

# Search for Get-Printer usage and ErrorAction handling.
echo "===== Get-Printer usages ====="
rg -n --hidden --glob '!**/.git/**' 'Get-Printer|ErrorAction Stop|ErrorAction SilentlyContinue|catch \{' Modules

Repository: 00AstroGit00/windows-printer-toolkit

Length of output: 306


🏁 Script executed:

#!/bin/bash
set -euo pipefail

# Show exact snippets around the cited lines.
sed -n '140,155p' Modules/Core/PrinterToolkit.Core.psm1 | nl -ba
echo "-----"
sed -n '214,228p' Modules/Reporting/PrinterToolkit.Reporting.psm1 | nl -ba
echo "-----"
sed -n '14,28p' Modules/Sharing/PrinterToolkit.Sharing.psm1 | nl -ba
echo "-----"
sed -n '120,150p' Modules/Utilities/PrinterToolkit.Utilities.psm1 | nl -ba

Repository: 00AstroGit00/windows-printer-toolkit

Length of output: 214


🏁 Script executed:

#!/bin/bash
set -euo pipefail

# Inspect any docs/tests mentioning the behavior of Get-Printer errors or array output types.
rg -n --hidden --glob '!**/.git/**' 'Get-Printer|OutputType\(\[array\]\)|SilentlyContinue|ErrorAction Stop|try/catch' README* Modules tests .

Repository: 00AstroGit00/windows-printer-toolkit

Length of output: 50392


Use -ErrorAction Stop for the printer fallbacks

These try/catch blocks won’t catch non-terminating Get-Printer errors, so the empty-array fallback never runs. Switch the three calls to -ErrorAction Stop:

  • Modules/Core/PrinterToolkit.Core.psm1#L147-L151
  • Modules/Reporting/PrinterToolkit.Reporting.psm1#L219-L223
  • Modules/Sharing/PrinterToolkit.Sharing.psm1#L19-L23
🧰 Tools
🪛 PSScriptAnalyzer (1.25.0)

[info] 148-148: The cmdlet 'Get-Printers' returns an object of type 'System.Object[]' but this type is not declared in the OutputType attribute.

(PSUseOutputTypeCorrectly)


[info] 150-150: The cmdlet 'Get-Printers' returns an object of type 'System.Object[]' but this type is not declared in the OutputType attribute.

(PSUseOutputTypeCorrectly)

📍 Affects 3 files
  • Modules/Core/PrinterToolkit.Core.psm1#L147-L151 (this comment)
  • Modules/Reporting/PrinterToolkit.Reporting.psm1#L219-L223
  • Modules/Sharing/PrinterToolkit.Sharing.psm1#L19-L23
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@Modules/Core/PrinterToolkit.Core.psm1` around lines 147 - 151, Update the
Get-Printer calls inside the try/catch fallbacks to use -ErrorAction Stop so
failures reach the empty-array catch path. Apply this change at
Modules/Core/PrinterToolkit.Core.psm1 lines 147-151,
Modules/Reporting/PrinterToolkit.Reporting.psm1 lines 219-223, and
Modules/Sharing/PrinterToolkit.Sharing.psm1 lines 19-23; preserve the existing
return behavior.

}

function Set-DefaultPrinter {
Expand Down
8 changes: 6 additions & 2 deletions Modules/Reporting/PrinterToolkit.Reporting.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,11 @@ function Get-PrintComplianceReport {
[CmdletBinding()]
[OutputType([array])]
param()
$printers = @(Get-Printer -ErrorAction SilentlyContinue)
try {
$printers = @(Get-Printer -ErrorAction SilentlyContinue)
} catch {
$printers = @()
}
$drivers = @(Get-PrinterDriver -ErrorAction SilentlyContinue)

$results = foreach ($p in $printers) {
Expand Down Expand Up @@ -247,7 +251,7 @@ function Get-PrintComplianceReport {
}
}

return ,$results
return ,@($results)
}

Export-ModuleMember -Function New-PrinterReport, Get-PrintComplianceReport
12 changes: 8 additions & 4 deletions Modules/Sharing/PrinterToolkit.Sharing.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@ function Get-PrinterShareStatus {
[CmdletBinding()]
[OutputType([array])]
param()
$printers = @(Get-Printer -ErrorAction SilentlyContinue)
try {
$printers = @(Get-Printer -ErrorAction SilentlyContinue)
} catch {
$printers = @()
}
$results = foreach ($p in $printers) {
$driver = Get-PrinterDriver -Name $p.DriverName -ErrorAction SilentlyContinue
$isIPP = if ($p.PortName -match '^(http|ipp|wsd|wsdprint)') { $true } else { $false }
Expand All @@ -36,7 +40,7 @@ function Get-PrinterShareStatus {
}
}

return ,$results
return ,@($results)
}

function Enable-PrinterSharing {
Expand Down Expand Up @@ -121,7 +125,7 @@ function Get-SmbSharePermissions {
}
}

return ,$results
return ,@($results)
}

function Set-PrinterSharePermission {
Expand Down Expand Up @@ -230,7 +234,7 @@ function Get-PrinterSharingCompatibility {
}
}

return ,$results
return ,@($results)
}

Export-ModuleMember -Function Get-PrinterShareStatus, Enable-PrinterSharing, Disable-PrinterSharing, Get-SmbSharePermissions, Set-PrinterSharePermission, Set-PrinterSharingTransport, Get-PrinterSharingCompatibility
4 changes: 2 additions & 2 deletions Modules/Utilities/PrinterToolkit.Utilities.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ function Write-MenuHeader {
[string]$Subtitle = ''
)

Clear-Host
try { Clear-Host } catch { }
$line = '=' * 74
Write-Host $line -ForegroundColor Cyan
Write-Host " $Title" -ForegroundColor White
Expand All @@ -140,7 +140,7 @@ function Wait-Menu {
param()
Write-Host ''
Write-Host 'Press any key to continue...' -ForegroundColor DarkGray
$null = $Host.UI.RawUI.ReadKey('NoEcho,IncludeKeyDown')
try { $null = $Host.UI.RawUI.ReadKey('NoEcho,IncludeKeyDown') } catch { }
}

Export-ModuleMember -Function Test-Administrator, Test-Elevated, Assert-Elevated, Confirm-DestructiveAction, Get-SystemInfo, Write-MenuHeader, Wait-Menu
7 changes: 4 additions & 3 deletions Tests/PrinterToolkit.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,8 @@ Describe 'Core Module' {

Describe 'Utilities Module' {
It 'Test-Administrator should return boolean' {
Test-Administrator -is [bool] | Should -Be $true
$result = Test-Administrator
$result -is [bool] | Should -Be $true
}

It 'Test-Elevated calls Test-Administrator' {
Expand All @@ -110,12 +111,12 @@ Describe 'Utilities Module' {
}

It 'Assert-Elevated should not throw when admin' {
Mock Test-Administrator { return $true }
Mock Test-Administrator { return $true } -ModuleName 'PrinterToolkit.Utilities'
{ Assert-Elevated } | Should -Not -Throw
}

It 'Assert-Elevated should throw when not admin' {
Mock Test-Administrator { return $false }
Mock Test-Administrator { return $false } -ModuleName 'PrinterToolkit.Utilities'
{ Assert-Elevated } | Should -Throw
}
}
Expand Down
Loading