-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDeploy-Printer.ps1
More file actions
137 lines (108 loc) · 4.15 KB
/
Deploy-Printer.ps1
File metadata and controls
137 lines (108 loc) · 4.15 KB
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
# Set the execution policy for the current process to Unrestricted without prompting
Set-ExecutionPolicy -ExecutionPolicy Unrestricted -Scope Process -Force
# Define the printer name and IP address
$printerName = "2nd floor"
$printerIP = "10.18.3.51"
# Define the URL for the zip, download location and extracted path
$driverZipUrl = "https://bitimages.nyc3.digitaloceanspaces.com/Uploads/Drivers/KM_v4UPD_UniversalDriver_PCL_2.4.0.1.zip"
$localZipPath = "$env:TEMP\KM_v4UPD_UniversalDriver_PCL.zip"
$extractFolder = "$env:TEMP\KM_v4UPD_UniversalDriver_PCL"
# Download the driver ZIP file from the internet
try {
Write-Output "Downloading driver ZIP file from $driverZipUrl..."
Invoke-WebRequest -Uri $driverZipUrl -OutFile $localZipPath -ErrorAction Stop
Write-Output "Download complete. Saved to $localZipPath."
}
catch {
Write-Error "Failed to download the driver ZIP file: $_"
exit 1
}
# Unzip the file
try {
# Remove the extraction folder if it already exists for a clean extraction.
if (Test-Path $extractFolder) {
Remove-Item -Path $extractFolder -Recurse -Force
}
Write-Output "Extracting driver ZIP file to $extractFolder..."
Expand-Archive -Path $localZipPath -DestinationPath $extractFolder -Force
Write-Output "Extraction complete."
}
catch {
Write-Error "Failed to extract the driver ZIP file: $_"
exit 1
}
# Locate the INF file in the extracted folder (search recursively) Tell Windows to use x64 because its dumb and will use ARM
try {
$infFiles = Get-ChildItem -Path $extractFolder -Filter *.inf -Recurse
if ($infFiles.Count -eq 0) {
Write-Error "No INF file found in the extracted folder. Exiting."
exit 1
}
# Filter for INF files that are for x64 Windows 10
$x64Win10InfFiles = $infFiles | Where-Object { $_.FullName -match "x64" -and $_.FullName -match "Win10" }
if ($x64Win10InfFiles.Count -gt 0) {
$driverInfPath = $x64Win10InfFiles[0].FullName
Write-Output "Found x64 Win10 INF file: $driverInfPath"
}
else {
if ($infFiles.Count -eq 1) {
$driverInfPath = $infFiles[0].FullName
Write-Output "Found single INF file: $driverInfPath"
}
else {
Write-Output "Multiple INF files found. Using the first found: $($infFiles[0].FullName)"
$driverInfPath = $infFiles[0].FullName
}
}
}
catch {
Write-Error "Error locating INF file: $_"
exit 1
}
# Install the driver using pnputil (with /subdirs in case additional files are in nested folders)
try {
Write-Output "Installing driver using pnputil..."
$pnputilOutput = pnputil.exe /add-driver "$driverInfPath" /install /subdirs
Write-Output $pnputilOutput
}
catch {
Write-Error "Failed to install the driver: $_"
exit 1
}
# Find driver name with pnputil
$publishedNameLine = $pnputilOutput | Where-Object { $_ -match "Published Name:" }
if ($publishedNameLine) {
$publishedInfName = ($publishedNameLine -replace "Published Name:\s*", "").Trim()
Write-Output "Driver published INF: $publishedInfName"
} else {
Write-Error "Published name not found in pnputil output. Please verify driver installation."
exit 1
}
# Set Driver name because "windows"
$driverName = "KONICA MINOLTA PS BW Laser Class Driver"
Write-Output "Using driver name: $driverName"
# Check if the printer port already exists; if not, create it
if (-not (Get-PrinterPort -Name $printerIP -ErrorAction SilentlyContinue)) {
try {
Write-Output "Creating printer port for IP $printerIP..."
Add-PrinterPort -Name $printerIP -PrinterHostAddress $printerIP
Write-Output "Printer port created."
}
catch {
Write-Error "Failed to create printer port: $_"
exit 1
}
}
else {
Write-Output "Printer port for IP $printerIP already exists. Skipping creation."
}
# Add the printer using the newly installed driver
try {
Write-Output "Adding printer '$printerName' with driver '$driverName'..."
Add-Printer -Name $printerName -PortName $printerIP -DriverName $driverName
Write-Output "Printer added successfully."
}
catch {
Write-Error "Failed to add the printer: $_"
exit 1
}