First commit
This commit is contained in:
commit
129ff2e587
5 changed files with 182 additions and 0 deletions
86
lib/ManagePrinter.psm1
Normal file
86
lib/ManagePrinter.psm1
Normal file
|
@ -0,0 +1,86 @@
|
|||
# Manage printers
|
||||
|
||||
function Get-CurrentPrinterDriverVersion {
|
||||
param (
|
||||
[Parameter(Mandatory=$true)][string]$DriverName
|
||||
)
|
||||
$printer = Get-PrinterDriver -Name $DriverName
|
||||
if ( $printer -eq $false ) {
|
||||
return $false
|
||||
}
|
||||
$version = $printer.DriverVersion
|
||||
return ( 3..0 | foreach-object {( $version -shr ($_ * 16)) -band 0xffff }) -join'.'
|
||||
}
|
||||
|
||||
Function Add-NetworkPrinterPort {
|
||||
param (
|
||||
[Parameter(Mandatory=$true)][string]$Ip,
|
||||
[Parameter(Mandatory=$true)][string]$Name
|
||||
)
|
||||
if ( $(Get-PrinterPort | Where PrinterHostAddress -eq $Ip) ) {
|
||||
Write-Host -ForegroundColor Cyan "This driver version is already installed"
|
||||
}
|
||||
else {
|
||||
Try {
|
||||
Add-PrinterPort -Name $Name -PrinterHostAddress $Ip
|
||||
}
|
||||
Catch {
|
||||
Throw $($PSItem.ToString())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function Install-PrinterDriver {
|
||||
param (
|
||||
[Parameter(Mandatory=$true)][string]$Inf,
|
||||
[Parameter(Mandatory=$true)] [string]$Name
|
||||
)
|
||||
|
||||
if ( -Not (Test-Path $Inf -EA SilentlyContinue) ) {
|
||||
Throw [System.IO.FileNotFoundException]::new("Driver file $Inf not found")
|
||||
}
|
||||
|
||||
if ( Get-PrinterDriver -Name $Name -EA SilentlyContinue ) {
|
||||
Write-Warning "Printer $Nam e already exist"
|
||||
|
||||
# Compare version
|
||||
$current_version = Get-CurrentPrinterDriverVersion $Name
|
||||
$inf_version = $(((Select-String -Pattern "DriverVer" -path $Inf) -split ',')[1])
|
||||
if ($inf_version -eq $current_version) {
|
||||
Write-Host -ForegroundColor Cyan "This driver version is already installed"
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
Try {
|
||||
pnputil /add-driver $Inf /install | Out-Null
|
||||
Add-PrinterDriver -Name $Name -EA Stop
|
||||
}
|
||||
Catch [Microsoft.Management.Infrastructure.CimException] {
|
||||
Throw $($PSItem.ToString())
|
||||
}
|
||||
Catch {
|
||||
Throw "Error when Adding $Name from $Inf in Windows Driver Store"
|
||||
}
|
||||
}
|
||||
|
||||
Function Add-NetworkPrinter () {
|
||||
[CmdletBinding()]
|
||||
param (
|
||||
[Parameter(Mandatory=$true)][string]$Inf,
|
||||
[Parameter(Mandatory=$true)][string]$Ip,
|
||||
[Parameter(Mandatory=$true)][string]$Driver,
|
||||
[Parameter(Mandatory=$true)][string]$PrinterName,
|
||||
[Parameter(Mandatory=$false)][string]$PortPrefix=""
|
||||
)
|
||||
$PortName = "$PortPrefix$Ip"
|
||||
Try {
|
||||
Install-PrinterDriver $Inf $Driver
|
||||
Add-NetworkPrinterPort $Ip $PortName
|
||||
Add-Printer -DriverName $Driver -Name $PrinterName -PortName $PortName -EA Stop
|
||||
}
|
||||
Catch {
|
||||
Throw "Error in Add-NetworkPrinter: $($Error[0].ToString())"
|
||||
}
|
||||
}
|
||||
|
Reference in a new issue