Update samy.ps1
This commit is contained in:
86
samy.ps1
86
samy.ps1
@@ -2002,96 +2002,10 @@ function Invoke-InstallPrinters {
|
||||
|
||||
#region Printer core (local config + install)
|
||||
|
||||
function Get-SamyPrinterLocalConfigPath {
|
||||
[CmdletBinding()]
|
||||
param()
|
||||
|
||||
# Use a stable location on every machine
|
||||
$configDir = Join-Path $env:ProgramData 'SVS\Samy\Printers'
|
||||
|
||||
if (-not (Test-Path $configDir)) {
|
||||
try {
|
||||
New-Item -Path $configDir -ItemType Directory -Force | Out-Null
|
||||
Write-LogHybrid "Created printer config folder at '$configDir'." Info Printers -LogToEvent
|
||||
}
|
||||
catch {
|
||||
Write-LogHybrid "Failed to create printer config folder '$configDir': $($_.Exception.Message)" Warning Printers -LogToEvent
|
||||
}
|
||||
}
|
||||
|
||||
return (Join-Path $configDir 'printers.json')
|
||||
}
|
||||
|
||||
function Get-SamyPrinterConfigFromFile {
|
||||
[CmdletBinding()]
|
||||
param()
|
||||
|
||||
$path = Get-SamyPrinterLocalConfigPath
|
||||
|
||||
if (-not (Test-Path $path)) {
|
||||
throw "Local printer config file not found at '$path'. Create or update printers.json first."
|
||||
}
|
||||
|
||||
$json = Get-Content -Path $path -Raw -ErrorAction Stop
|
||||
$profiles = $json | ConvertFrom-Json
|
||||
|
||||
if (-not $profiles) {
|
||||
throw "Printer config file '$path' is empty or invalid JSON."
|
||||
}
|
||||
|
||||
return $profiles
|
||||
}
|
||||
|
||||
# Per-session cache
|
||||
$Script:Samy_PrinterProfiles = $null
|
||||
|
||||
function Get-SamyPrinterProfiles {
|
||||
<#
|
||||
.SYNOPSIS
|
||||
Returns all printer profiles, optionally filtered by ClientCode.
|
||||
#>
|
||||
[CmdletBinding()]
|
||||
param(
|
||||
[string]$ClientCode
|
||||
)
|
||||
|
||||
if (-not $Script:Samy_PrinterProfiles) {
|
||||
$Script:Samy_PrinterProfiles = Get-SamyPrinterConfigFromFile
|
||||
}
|
||||
|
||||
$result = $Script:Samy_PrinterProfiles
|
||||
|
||||
if ($PSBoundParameters.ContainsKey('ClientCode') -and $ClientCode) {
|
||||
$result = $result | Where-Object { $_.ClientCode -eq $ClientCode }
|
||||
}
|
||||
|
||||
return $result
|
||||
}
|
||||
|
||||
function Get-SamyPrinterProfile {
|
||||
<#
|
||||
.SYNOPSIS
|
||||
Returns a single printer profile for a given ClientCode and ProfileName.
|
||||
#>
|
||||
[CmdletBinding()]
|
||||
param(
|
||||
[Parameter(Mandatory)][string]$ClientCode,
|
||||
[Parameter(Mandatory)][string]$ProfileName
|
||||
)
|
||||
|
||||
$profiles = Get-SamyPrinterProfiles -ClientCode $ClientCode
|
||||
$match = $profiles | Where-Object { $_.ProfileName -eq $ProfileName }
|
||||
|
||||
if (-not $match) {
|
||||
throw "No printer profile found for ClientCode '$ClientCode' and ProfileName '$ProfileName'."
|
||||
}
|
||||
|
||||
if ($match.Count -gt 1) {
|
||||
throw "Multiple printer profiles found for ClientCode '$ClientCode' and ProfileName '$ProfileName'. De-duplicate in printers.json."
|
||||
}
|
||||
|
||||
return $match
|
||||
}
|
||||
|
||||
function Ensure-SamyPrinterDriver {
|
||||
[CmdletBinding()]
|
||||
|
||||
Reference in New Issue
Block a user