-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathInstall-Printer.ps1
201 lines (174 loc) · 7.09 KB
/
Install-Printer.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
<#
.Synopsis
Created on: 31/12/2021
Created by: Ben Whitmore
Filename: Install-Printer.ps1
Simple script to install a network printer from an INF file. The INF and required CAB files hould be in the same directory as the script if creating a Win32app
#### Win32 app Commands ####
Install:
powershell.exe -executionpolicy bypass -file .\Install-Printer.ps1 -PortName "IP_10.10.1.1" -PrinterIP "10.1.1.1" -PrinterName "Canon Printer Upstairs" -DriverName "Canon Generic Plus UFR II" -INFFile "CNLB0MA64.inf"
Uninstall:
powershell.exe -executionpolicy bypass -file .\Remove-Printer.ps1 -PrinterName "Canon Printer Upstairs"
Detection:
HKEY_LOCAL_MACHINE\Software\Microsoft\Windows NT\CurrentVersion\Print\Printers\Canon Printer Upstairs
Name = "Canon Printer Upstairs"
.Example
.\Install-Printer.ps1 -PortName "IP_10.10.1.1" -PrinterIP "10.1.1.1" -PrinterName "Canon Printer Upstairs" -DriverName "Canon Generic Plus UFR II" -INFFile "CNLB0MA64.inf"
#>
[CmdletBinding()]
Param (
[Parameter(Mandatory = $True)]
[String]$PortName,
[Parameter(Mandatory = $True)]
[String]$PrinterIP,
[Parameter(Mandatory = $True)]
[String]$PrinterName,
[Parameter(Mandatory = $True)]
[String]$DriverName,
[Parameter(Mandatory = $True)]
[String]$INFFile
)
#Reset Error catching variable
$Throwbad = $Null
#Run script in 64bit PowerShell to enumerate correct path for pnputil
If ($ENV:PROCESSOR_ARCHITEW6432 -eq "AMD64") {
Try {
&"$ENV:WINDIR\SysNative\WindowsPowershell\v1.0\PowerShell.exe" -File $PSCOMMANDPATH -PortName $PortName -PrinterIP $PrinterIP -DriverName $DriverName -PrinterName $PrinterName -INFFile $INFFile
}
Catch {
Write-Error "Failed to start $PSCOMMANDPATH"
Write-Warning "$($_.Exception.Message)"
$Throwbad = $True
}
}
function Write-LogEntry {
param (
[parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[string]$Value,
[parameter(Mandatory = $false)]
[ValidateNotNullOrEmpty()]
[string]$FileName = "$($PrinterName).log",
[switch]$Stamp
)
#Build Log File appending System Date/Time to output
$LogFile = Join-Path -Path $env:SystemRoot -ChildPath $("Temp\$FileName")
$Time = -join @((Get-Date -Format "HH:mm:ss.fff"), " ", (Get-WmiObject -Class Win32_TimeZone | Select-Object -ExpandProperty Bias))
$Date = (Get-Date -Format "MM-dd-yyyy")
If ($Stamp) {
$LogText = "<$($Value)> <time=""$($Time)"" date=""$($Date)"">"
}
else {
$LogText = "$($Value)"
}
Try {
Out-File -InputObject $LogText -Append -NoClobber -Encoding Default -FilePath $LogFile -ErrorAction Stop
}
Catch [System.Exception] {
Write-Warning -Message "Unable to add log entry to $LogFile.log file. Error message at line $($_.InvocationInfo.ScriptLineNumber): $($_.Exception.Message)"
}
}
Write-LogEntry -Value "##################################"
Write-LogEntry -Stamp -Value "Installation started"
Write-LogEntry -Value "##################################"
Write-LogEntry -Value "Install Printer using the following values..."
Write-LogEntry -Value "Port Name: $PortName"
Write-LogEntry -Value "Printer IP: $PrinterIP"
Write-LogEntry -Value "Printer Name: $PrinterName"
Write-LogEntry -Value "Driver Name: $DriverName"
Write-LogEntry -Value "INF File: $INFFile"
$INFARGS = @(
"/add-driver"
"$INFFile"
)
If (-not $ThrowBad) {
Try {
#Stage driver to driver store
Write-LogEntry -Stamp -Value "Staging Driver to Windows Driver Store using INF ""$($INFFile)"""
Write-LogEntry -Stamp -Value "Running command: Start-Process pnputil.exe -ArgumentList $($INFARGS) -wait -passthru"
Start-Process pnputil.exe -ArgumentList $INFARGS -wait -passthru
}
Catch {
Write-Warning "Error staging driver to Driver Store"
Write-Warning "$($_.Exception.Message)"
Write-LogEntry -Stamp -Value "Error staging driver to Driver Store"
Write-LogEntry -Stamp -Value "$($_.Exception)"
$ThrowBad = $True
}
}
If (-not $ThrowBad) {
Try {
#Install driver
$DriverExist = Get-PrinterDriver -Name $DriverName -ErrorAction SilentlyContinue
if (-not $DriverExist) {
Write-LogEntry -Stamp -Value "Adding Printer Driver ""$($DriverName)"""
Add-PrinterDriver -Name $DriverName -Confirm:$false
}
else {
Write-LogEntry -Stamp -Value "Print Driver ""$($DriverName)"" already exists. Skipping driver installation."
}
}
Catch {
Write-Warning "Error installing Printer Driver"
Write-Warning "$($_.Exception.Message)"
Write-LogEntry -Stamp -Value "Error installing Printer Driver"
Write-LogEntry -Stamp -Value "$($_.Exception)"
$ThrowBad = $True
}
}
If (-not $ThrowBad) {
Try {
#Create Printer Port
$PortExist = Get-Printerport -Name $PortName -ErrorAction SilentlyContinue
if (-not $PortExist) {
Write-LogEntry -Stamp -Value "Adding Port ""$($PortName)"""
Add-PrinterPort -name $PortName -PrinterHostAddress $PrinterIP -Confirm:$false
}
else {
Write-LogEntry -Stamp -Value "Port ""$($PortName)"" already exists. Skipping Printer Port installation."
}
}
Catch {
Write-Warning "Error creating Printer Port"
Write-Warning "$($_.Exception.Message)"
Write-LogEntry -Stamp -Value "Error creating Printer Port"
Write-LogEntry -Stamp -Value "$($_.Exception)"
$ThrowBad = $True
}
}
If (-not $ThrowBad) {
Try {
#Add Printer
$PrinterExist = Get-Printer -Name $PrinterName -ErrorAction SilentlyContinue
if (-not $PrinterExist) {
Write-LogEntry -Stamp -Value "Adding Printer ""$($PrinterName)"""
Add-Printer -Name $PrinterName -DriverName $DriverName -PortName $PortName -Confirm:$false
}
else {
Write-LogEntry -Stamp -Value "Printer ""$($PrinterName)"" already exists. Removing old printer..."
Remove-Printer -Name $PrinterName -Confirm:$false
Write-LogEntry -Stamp -Value "Adding Printer ""$($PrinterName)"""
Add-Printer -Name $PrinterName -DriverName $DriverName -PortName $PortName -Confirm:$false
}
$PrinterExist2 = Get-Printer -Name $PrinterName -ErrorAction SilentlyContinue
if ($PrinterExist2) {
Write-LogEntry -Stamp -Value "Printer ""$($PrinterName)"" added successfully"
}
else {
Write-Warning "Error creating Printer"
Write-LogEntry -Stamp -Value "Printer ""$($PrinterName)"" error creating printer"
$ThrowBad = $True
}
}
Catch {
Write-Warning "Error creating Printer"
Write-Warning "$($_.Exception.Message)"
Write-LogEntry -Stamp -Value "Error creating Printer"
Write-LogEntry -Stamp -Value "$($_.Exception)"
$ThrowBad = $True
}
}
If ($ThrowBad) {
Write-Error "An error was thrown during installation. Installation failed. Refer to the log file in %temp% for details"
Write-LogEntry -Stamp -Value "Installation Failed"
}