-
Notifications
You must be signed in to change notification settings - Fork 222
Expand file tree
/
Copy pathImport-PrtMPrinterDrivers.ps1
More file actions
170 lines (154 loc) · 5.77 KB
/
Import-PrtMPrinterDrivers.ps1
File metadata and controls
170 lines (154 loc) · 5.77 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
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
#Requires -Version 5.0
#Requires -Modules PrintManagement
<#
.SYNOPSIS
Install printer drivers asyncron from csv file to a print server.
.DESCRIPTION
.NOTES
This PowerShell script was developed and optimized for ScriptRunner. The use of the scripts requires ScriptRunner.
The customer or user is authorized to copy the script from the repository and use them in ScriptRunner.
The terms of use for ScriptRunner do not apply to this script. In particular, ScriptRunner Software GmbH assumes no liability for the function,
the use and the consequences of the use of this freely available script.
PowerShell is a product of Microsoft Corporation. ScriptRunner is a product of ScriptRunner Software GmbH.
© ScriptRunner Software GmbH
CSV file pattern:
DriverName;ComputerName;InfFilePath
"Microsoft PS Class Driver";;
"Xerox FFPS Class Driver";Computer3000;
"HP Universal Printing PCL 5";Computer1;"C:\Windows\Inf\hpcu130t.inf"
.COMPONENT
Requires Module PrintManagement
.LINK
https://github.com/scriptrunner/ActionPacks/tree/master/WinPrintManagement/Drivers
.Parameter CsvFile
[sr-en] Path and filename of the CSV file to import
.Parameter Delimiter
[sr-en] Delimiter that separates the property values in the CSV file
.Parameter FileEncoding
[sr-en] Type of character encoding that was used in the CSV file
.Parameter MaxJobCount
[sr-en] Maximum number of concurrent executed jobs.
.Parameter AccessAccount
[sr-en] User account that has permission to perform this action. If Credential is not specified, the current user account is used.
.EXAMPLE
.\Import-PrinterDrivers.ps1 -CsvFile 'C:\Temp\drivers.csv'
#>
[CmdLetBinding()]
Param(
[Parameter(Mandatory=$true)]
[string]$CsvFile,
[string]$Delimiter= ';',
[ValidateSet('Unicode','UTF7','UTF8','ASCII','UTF32','BigEndianUnicode','Default','OEM')]
[string]$FileEncoding = 'UTF8',
[int]$MaxJobCount = 100,
[PSCredential]$AccessAccount
)
Import-Module PrintManagement
[bool]$Script:Err = $false
$Script:Cim = $null
$Script:Result = @()
$Script:Errors = @()
$Script:Output = @()
$Script:Failed = New-Object "System.Collections.Generic.List[String]"
$Script:Jobs = New-Object "System.Collections.Generic.Dictionary[Int,string]"
try{
if(Test-Path -Path $CsvFile -ErrorAction SilentlyContinue){
$Script:Drivers = Import-Csv -Path $CsvFile -Delimiter $Delimiter -Encoding $FileEncoding `
-Header @('DriverName','ComputerName', 'InfFilePath') -ErrorAction Stop
}
else{
Throw "$($CsvFile) does not exist"
}
# Install drivers
foreach($item in $Script:Drivers){
if($item.ComputerName -eq 'ComputerName'){
continue
}
if([System.string]::IsNullOrWhiteSpace($item.ComputerName)){
$item.ComputerName=[System.Net.DNS]::GetHostByName('').HostName
}
if($null -eq $AccessAccount){
$Script:Cim = New-CimSession -ComputerName $item.ComputerName -ErrorAction Stop
}
else {
$Script:Cim = New-CimSession -ComputerName $item.ComputerName -Credential $AccessAccount -ErrorAction Stop
}
if(Get-PrinterDriver -CimSession $Script:Cim -Name $item.DriverName.Trim() -ComputerName $item.ComputerName -ErrorAction SilentlyContinue ){
$Script:Output += "Printer driver $($item.DriverName) already exists"
continue
}
else{
$Error.RemoveAt(0)
if([System.String]::IsNullOrWhiteSpace($item.InfFilePath)){
$job = Add-PrinterDriver -AsJob -CimSession $Script:Cim -Name $item.DriverName.Trim() -ComputerName $item.ComputerName
}
else {
$job = Add-PrinterDriver -AsJob -CimSession $Script:Cim -Name $item.DriverName.Trim() -ComputerName $item.ComputerName -InfPath $item.InfFilePath
}
$Script:Jobs.Add($job.ID,$item.DriverName)
}
# Check max. jobs
do{
$tmp = Get-Job -State Running | Where-Object -FilterScript { $Script:Jobs.Keys -contains $_.Id }
if($tmp -and $tmp.Count -gt $MaxJobCount){
Start-Sleep -Seconds 5
}
else {
break
}
} while ($true)
}
# Wait for jobs finish
do{
$tmp = Get-Job -State Running | Where-Object -FilterScript { $Script:Jobs.Keys -contains $_.Id }
if($tmp){
Start-Sleep -Seconds 5 # wait
}
else {
break
}
} while ($true)
# Check job results
$tmp = Get-Job | Where-Object -FilterScript { $Script:Jobs.Keys -contains $_.Id }
foreach ($job in $tmp){
if($job.JobStateInfo.State -eq 'Failed'){
$Script:Errors += "Install printer driver: $($Script:Jobs[$job.Id]) failed."
$Script:Failed.Add($Script:Jobs[$job.Id])
$Script:Err = $true
}
if($job.JobStateInfo.State -eq 'Completed'){
$Script:Result += "Install printer driver: $($Script:Jobs[$job.Id]) succeeded"
continue
}
}
$Script:Jobs.CLear()
if($SRXEnv) {
if($Script:Err -eq $true){
$SRXEnv.ResultMessage = $Script:Errors
Write-Output $Script:Result
}
else{
$SRXEnv.ResultMessage = $Script:Result
}
}
else{
if($Script:Err -eq $true){
Write-Output $Script:Errors
}
Write-Output $Script:Result
}
Write-Output $Script:Output
if($Script:Err -eq $true){
Throw "An error has occurred"
}
}
catch{
Write-Output $Script:Result
Write-Output $Script:Output
Throw $_.Exception.Message
}
finally{
if($null -ne $Script:Cim){
Remove-CimSession $Script:Cim
}
}