First commit
This commit is contained in:
commit
129ff2e587
5 changed files with 182 additions and 0 deletions
21
Add-Printer.ps1
Executable file
21
Add-Printer.ps1
Executable file
|
@ -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()
|
||||
}
|
19
LICENCE.md
Normal file
19
LICENCE.md
Normal file
|
@ -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.
|
40
README.md
Normal file
40
README.md
Normal file
|
@ -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 <inf_file> -Ip <ip_adress> -DriverName <drivername> -Name <printer-name> -PortPrefix <port_name_prefix>
|
||||
```
|
||||
|
||||
* `-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.
|
16
lib/ManagePrinter.psd1
Normal file
16
lib/ManagePrinter.psd1
Normal file
|
@ -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'
|
||||
}
|
||||
}
|
||||
}
|
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