From 129ff2e587a231dd4a99fba2ec8bbdb4937aa7c2 Mon Sep 17 00:00:00 2001 From: Yorick Barbanneau Date: Sun, 31 Jan 2021 22:36:14 +0100 Subject: [PATCH] First commit --- Add-Printer.ps1 | 21 +++++++++++ LICENCE.md | 19 ++++++++++ README.md | 40 ++++++++++++++++++++ lib/ManagePrinter.psd1 | 16 ++++++++ lib/ManagePrinter.psm1 | 86 ++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 182 insertions(+) create mode 100755 Add-Printer.ps1 create mode 100644 LICENCE.md create mode 100644 README.md create mode 100644 lib/ManagePrinter.psd1 create mode 100644 lib/ManagePrinter.psm1 diff --git a/Add-Printer.ps1 b/Add-Printer.ps1 new file mode 100755 index 0000000..552957f --- /dev/null +++ b/Add-Printer.ps1 @@ -0,0 +1,21 @@ +param ( + [Parameter(Mandatory=$true)][string]$Inf, + [Parameter(Mandatory=$true)][string]$Ip, + [Parameter(Mandatory=$true)][string]$DriverName, + [Parameter(Mandatory=$false)][string]$Name="", + [Parameter(Mandatory=$false)][string]$PortPrefix = "salt_" +) + +Import-Module -Name "$PSScriptRoot\lib\ManagePrinter.psd1" -Force + +if ( -Not $Name ) { + Write-Verbose "Printer name was not set, using Driver name instead" + $Name = $DriverName +} +try { + Add-NetworkPrinter -Inf $Inf -Ip $Ip -Driver $DriverName -PrinterName $Name -PortPrefix $PortPrefix -EA Stop + Set-PrintConfiguration -DuplexingMode OneSided -Color $false -PrinterName $Name +} +Catch { + Write-Host -ForegroundColor Red $Error[0].ToString() +} diff --git a/LICENCE.md b/LICENCE.md new file mode 100644 index 0000000..1318889 --- /dev/null +++ b/LICENCE.md @@ -0,0 +1,19 @@ +Copyright 2021 Yorick Barbanneau + +Permission is hereby granted, free of charge, to any person obtaining a copy of +this software and associated documentation files (the "Software"), to deal in +the Software without restriction, including without limitation the rights to +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies +of the Software, and to permit persons to whom the Software is furnished to do +so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..baf55f1 --- /dev/null +++ b/README.md @@ -0,0 +1,40 @@ +Win-ConfigScripts +----------------- + +A collection of Powershell scripts to help manage Windows. For now there is only +a script to help installing network printer. + +## `Add-NetworkPrinter.ps1` : Add a network printer + +### usage + +``` +Add-NetworkPrinter.ps1 -Inf -Ip -DriverName -Name -PortPrefix +``` + + * `-Inf`: path to inf file. + * `-Ip`: IP adress of printer. + * `-DriverName`: name of the driver. + * `-Name` : name given to the printer in configuration panel, if empty then + will be `DriverName`. + * `-PortPrefix`: prefix fot the printer port name, the prefix will be added to + the IP adress. Default is `salt_`. + +#### Example + +``` +.\Add-NetworkPrinter.ps1 -Inf \\server\printer\driver12.inf -Ip 192.168.9.212 -DriverName "Konica-Minolta C224e' -Name "Konica second floor" +``` + +Add a printer named "Konica second floor " with driver "Konica-Minolta c224e" +located in `\\server\printer\driver12.inf`. + +### Module + +This script use a powershel module located in `lib\ManagePrinter.psd1`. This +module contains several commandlets to help manage printers : +`Add-NetworkPrinterPort`,`Install-PrinterDriver`, `Add-NetworkPrinter` + +## Licence + +All this work is licenced under the MIT Expat Licence. diff --git a/lib/ManagePrinter.psd1 b/lib/ManagePrinter.psd1 new file mode 100644 index 0000000..fb04398 --- /dev/null +++ b/lib/ManagePrinter.psd1 @@ -0,0 +1,16 @@ +@{ + RootModule = 'ManagePrinters.psm1' + GUID = '21004010-43d2-4cc0-a4d1-eadd604f97d1' + ModuleVersion = '0.2' + Author = 'Yorick Barbanneau' + Copyright = '(c) 2021 Yorick Barbanneau' + Description = 'Manage Printer' + FunctionsToExport = @('Add-NetworkPrinterPort','Install-PrinterDriver', 'Add-NetworkPrinter' ) + CmdletsToExport = @() + AliasesToExport = @() + PrivateData = @{ + PSData = @{ + LicenseUri = 'https://git.epha.se/ephase/win_configscripts/LICENCE.md' + } + } +} diff --git a/lib/ManagePrinter.psm1 b/lib/ManagePrinter.psm1 new file mode 100644 index 0000000..bfa9e77 --- /dev/null +++ b/lib/ManagePrinter.psm1 @@ -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())" + } +} +