-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLabPrinterMapping.ps1
413 lines (266 loc) · 12 KB
/
LabPrinterMapping.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
Function Get-ComputerOU {
<#
.Synopsis
Finds the Organizational Unit location of the Computer queried
.DESCRIPTION
Get-ComputerOU uses a System.DirectoryServices object to traverse
Active Directory for the location in the structure of the computer.
Information output is the computer name and the last 3 instances of
the OU structure the computer object resides.
.EXAMPLE
Get-ComputerOU -ComputerName SERVER1
.EXAMPLE
Get-ComputerOU -ComputerName SERVER1, SERVER2, SERVER3
#>
[CmdletBinding()]
Param (
[Parameter( Mandatory=$True,
ValueFromPipelineByPropertyName=$True,
HelpMessage='ComputerName of Computer you want to lookup')]
[String[]]$ComputerName
)
Begin {}
Process {
Try {
ForEach ($Computer in $ComputerName) {
$Filter = "(&(objectCategory=Computer)(Name=$Computer))"
$DirectorySearcher = New-Object -TypeName System.DirectoryServices.DirectorySearcher
$DirectorySearcher.Filter = $Filter
$SearcherPath = $DirectorySearcher.FindOne()
$DistinguishedName = $SearcherPath.GetDirectoryEntry().DistinguishedName
$OUName_Last = ($DistinguishedName.Split(','))[1]
$OUName_Second = ($DistinguishedName.Split(','))[2]
$OUName_First = ($DistinguishedName.Split(','))[3]
$OUName1 = $OUName_Last.SubString($OUName_Last.IndexOf('=')+1)
$ouname2 = $OUName_Second.SubString($OUName_Second.IndexOf('=')+1)
$ouname3 = $OUName_First.SubString($OUName_First.IndexOf('=')+1)
$Properties = @{
'ComputerName' = $Computer
'OrgUnit' = $OUName3 + '-' + $OUName2 + '-' + $ouname1
}
$Obj = New-Object -TypeName PSObject -Property $Properties
$Obj.PSObject.TypeNames.Insert(0,'PC.OrgUnit')
Write-Output -InputObject $Obj
#if ($obj -eq $Null) {
if ($Null -eq $obj) {
Write-Error -Message 'Unable to find Computer OU based off computername' -ErrorAction 'Stop'
}
}
} Catch {
# get error record
[Management.Automation.ErrorRecord]$e = $_
# retrieve information about runtime error
$info = [PSCustomObject]@{
Date = Get-Date
ComputerName = $ENV:computerName
Exception = $e.Exception.Message
Reason = $e.CategoryInfo.Reason
Target = $e.CategoryInfo.TargetName
Script = $e.InvocationInfo.ScriptName
Line = $e.InvocationInfo.ScriptLineNumber
Column = $e.InvocationInfo.OffsetInLine
}
# output information. Post-process collected info, and log info (optional)
Write-output -InputObject $info
}
}
End {}
} #Get-ComputerOU
Function Set-LabPrinters {
#Requires -Version 3
<#
.SYNOPSIS
Map network printers from server to computer
.DESCRIPTION
Set-LabPrinters uses Add-printer to connect to existing printers on a print server
.EXAMPLE
Set-LabPrinters -ServerAddress \\Server -DefaultPrinter 'Prhh112'
#>
[CmdletBinding()]
Param (
[Parameter( Mandatory=$True,
ValueFromPipelineByPropertyName=$True,
HelpMessage='Lab identifier - change value type based on needs')]
[String]$Org,
[Parameter( Mandatory = $True,
HelpMessage='UNC path for Print Server')]
[string]$ServerAddress,
[Parameter( Mandatory=$True,
HelpMessage='Printer name on the server of the primary printer based on physical location')]
[String]$DefaultPrinter,
[string]$PrinterSecondary,
[string]$PrinterThird,
[String]$ErrorLog = "$ENV:WINDIR\Temp\LabPrinterMapping.log"
)
Begin {}
Process {
Try {
#Add default printer
if ($DefaultPrinter -ne '') {
$PrinterConnections = @{
'ConnectionName' = "$ServerAddress\$DefaultPrinter"
'ErrorAction' = 'stop'
}
Write-Verbose -Message "Installing default printer $($DefaultPrinter)"
Add-Printer @PrinterConnections
} Else {
Write-Error -Message 'No Default Printer Found'
}
if ($PrinterSecondary -ne '') {
$PrinterConnections = @{
'ConnectionName' = "$ServerAddress\$PrinterSecondary"
'ErrorAction' = 'Stop'
}
Write-Verbose -Message "Installing printer $($PrinterSecondary)"
Add-Printer @PrinterConnections
}
if ($PrinterThird -ne '') {
$PrinterConnections = @{
'ConnectionName' = "$ServerAddress\$PrinterThird"
'ErrorAction' = 'Stop'
}
Write-Verbose -Message "Installing printer $($PrinterThird)"
Add-Printer @PrinterConnections
}
} Catch {
[Management.Automation.ErrorRecord]$e = $_
# retrieve information about runtime error
$info = [PSCustomObject]@{
Date = Get-Date
ComputerName = $ENV:computerName
UserName = $env:USERNAME
Exception = $e.Exception.Message
Reason = $e.CategoryInfo.Reason
Target = $e.CategoryInfo.TargetName
Script = $e.InvocationInfo.ScriptName
Line = $e.InvocationInfo.ScriptLineNumber
Column = $e.InvocationInfo.OffsetInLine
}
# output information. Post-process collected info, and log info (optional)
Write-output -InputObject $info | Out-file -FilePath $ErrorLog -Append
}
}
End {}
}
Function Set-Win7Printers {
<#
.SYNOPSIS
Map network printers from server to computer
.DESCRIPTION
Set-Win7Printers uses Wscript network object methods to connect to existing printers on a print server
.EXAMPLE
Set-Win7Printers -ServerAddress \\Server -DefaultPrinter 'Prhh112'
#>
[cmdletBinding()]
Param (
[Parameter( Mandatory=$True,
ValueFromPipelineByPropertyName=$True,
HelpMessage='Lab identifier - change value type based on needs')]
[String]$Org,
[Parameter( Mandatory = $True,
HelpMessage='UNC path for Print Server')]
[string]$ServerAddress,
[Parameter( Mandatory=$True,
HelpMessage='Printer name on the server of the primary printer based on physical location')]
[String]$DefaultPrinter,
[string]$PrinterSecondary,
[string]$Printerthird,
[String]$ErrorLog = "$ENV:WINDIR\Temp\LabPrinterMapping.log"
)
Begin {}
Process {
Try {
if ($DefaultPrinter -ne '') {
$NetObj = New-object -ComObject Wscript.Network
$Netobj.AddWindowsPrinterConnection("$ServerAddress\$DefaultPrinter")
} else {
Write-Error -Message 'No reference to Default Printer'
}
if ($Printersecond -ne '') {
$NetObj = New-object -ComObject Wscript.Network
$Netobj.AddWindowsPrinterConnection("$ServerAddress\$PrinterSecondary")
}
if ($Printerthird -ne '') {
$NetObj = New-object -ComObject Wscript.Network
$Netobj.AddWindowsPrinterConnection("$ServerAddress\$PrinterThird")
}
} Catch {
[Management.Automation.ErrorRecord]$e = $_
# retrieve information about runtime error
$info = [PSCustomObject]@{
Date = Get-Date
ComputerName = $ENV:computerName
UserName = $env:USERNAME
Exception = $e.Exception.Message
Reason = $e.CategoryInfo.Reason
Target = $e.CategoryInfo.TargetName
Script = $e.InvocationInfo.ScriptName
Line = $e.InvocationInfo.ScriptLineNumber
Column = $e.InvocationInfo.OffsetInLine
}
# output information. Post-process collected info, and log info (optional)
Write-output -InputObject $info | Out-file -FilePath $ErrorLog -Append
}
}
End {}
}
#controller Script
$ScriptPath = $MyInvocation.MyCommand.Path
$CurrentDir = Split-Path -Path $ScriptPath
[String]$CsvPath = "$currentdir\LabPrinters.csv"
[String]$Log = "$env:windir\Temp\LabPrinterMapping.log"
$PrinterConfig = Import-Csv -Path "$CSVPath"
$OU = Get-ComputerOU -ComputerName "$ENV:COMPUTERNAME"
$PrinterConfig | ForEach-Object {
if ($OU.orgunit -eq $_.Laborg ) {
Do {
Try {
$LabPrinterParams = @{
'ServerAddress' = $_.ServerAddress
'DefaultPrinter' = $_.DefaultPrinter
'PrinterSecondary' = $_.PrinterSecondary
'PrinterThird' = $_.PrinterThird
'ErrorLog' = "$ENV:WINDIR\Temp\LabPrinterMapping.log"
'ErrorAction' = 'Stop'
}
$OSWMI = @{
'class' = 'Win32_operatingSystem'
'ErrorAction' = 'stop'
}
$OS = Get-wmiobject @OSWMI
if ($OS.Version -like '10.*' ) {
Set-LabPrinters @LabPrinterParams
} else {
Set-Win7Printers @LabPrinterParams
}
Write-Output -InputObject "$(Get-Date) : $ENV:COMPUTERNAME : Username $ENV:USERNAME : Printers to be installed $($_.DefaultPrinter, $_.PrinterSecondary, $_.PrinterThird)" | Out-file -FilePath $Log -Append
Write-Output -InputObject "$(Get-Date) : $ENV:COMPUTERNAME : Username $ENV:USERNAME : Default Printer $($_.DefaultPrinter)" | Out-file -FilePath $Log -Append
$PrinterWMI = @{
'Class' = 'Win32_Printer'
'ErrorAction' = 'Stop'
}
$InstalledPrinters = Get-WmiObject @PrinterWMI | Where-object {$_.Name -like '\\*'}
Write-output -InputObject "$(Get-Date) : $ENV:COMPUTERNAME : Username $ENV:USERNAME : Installed Printers $($InstalledPrinters.Name)" | Out-file -FilePath $Log -Append
if ($InstalledPrinters -eq $Null) {
Write-Error -Message "$(Get-Date) : $ENV:COMPUTERNAME : Username $ENV:USERNAME : Error - Printers did not get installed from $($_.ServerAddress)"
}
} Catch {
[Management.Automation.ErrorRecord]$e = $_
# retrieve information about runtime error
$info = [PSCustomObject]@{
Date = Get-Date
ComputerName = $ENV:computerName
UserName = $env:USERNAME
Exception = $e.Exception.Message
Reason = $e.CategoryInfo.Reason
Target = $e.CategoryInfo.TargetName
Script = $e.InvocationInfo.ScriptName
Line = $e.InvocationInfo.ScriptLineNumber
Column = $e.InvocationInfo.OffsetInLine
}
# output information. Post-process collected info, and log info (optional)
Write-output -InputObject $info | Out-file -FilePath $Log -Append
}
} Until($InstalledPrinters -ne $Null)
}
}