-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGet-PrinterInformation.ps1
More file actions
389 lines (327 loc) · 10.8 KB
/
Copy pathGet-PrinterInformation.ps1
File metadata and controls
389 lines (327 loc) · 10.8 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
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
function Get-PrinterInformation
{
<#
.SYNOPSIS
Get user and globally installed printers from a remote computer
.DESCRIPTION
Get user and globally installed printers from a remote computer
.EXAMPLE
PS C:\> Get-PrinterInformation -ComputerName 999TSTPC999
Gets printer information
.INPUTS
[string] for ComputerName
.OUTPUTS
[PSCustomObject] as results
.NOTES
Requires remote registry permissions
#>
[CmdletBinding()]
param (
# Name of computer to retrieve information from
[Parameter(Mandatory = $true,
ValueFromPipeline = $true,
ValueFromPipelineByPropertyName = $true)]
[string]$ComputerName
)
begin
{
# Object to set return status
$StatusReturn = [pscustomobject]@{
ComputerName = $ComputerName
Status = ''
}
# Set object to collect printer info
$Global:PrinterInformation = [System.Collections.Generic.List[System.Object]]::New()
# Set object to hold loaded hives
$UserHivesLoaded = [System.Collections.Generic.List[System.Object]]::New()
# Regex string to match appropriate SIDs
[string]$sidpattern = 'S-1-5-21-\d+-\d+\-\d+\-\d+$'
}
process
{
if (!(Test-Connection -Count 1 -ComputerName $ComputerName -Quiet))
{
$StatusReturn.Status = 'Computer is offline'
return $StatusReturn
}
##
## Get SharePoint Printers
##
# Set global profile object
$objSharePointProfile = [pscustomobject]@{
SID = 'SHAREPOINT ADDED'
Username = 'SHAREPOINT ADDED'
Name = 'SHAREPOINT ADDED'
ADStatus = 'Enabled'
LastUsed = $Null
Printers = $Null
}
$objSQLPrinters = Get-DatabaseInformation -PresetName PrinterMappingByComputer -ComputerName $ComputerName
$objSharePointProfile.Printers = $objSQLPrinters.Printer
$PrinterInformation.Insert(0, $objSharePointProfile)
##
## Get global printers
##
# Set global profile object
$GlobalProfileObject = [pscustomobject]@{
SID = 'GLOBALLY ADDED'
Username = 'GLOBALLY ADDED'
Name = 'GLOBALLY ADDED'
ADStatus = 'Enabled'
LastUsed = $Null
Printers = $Null
}
# Open remote registry
$RemoteRegistry = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('LocalMachine', $ComputerName)
# Set Global installed printers path
$GlobalInstalledPrintersPath = 'Software\Microsoft\Windows NT\CurrentVersion\Print\Connections\'
# Open global printer installed path
$GlobalKey = $RemoteRegistry.OpenSubKey($GlobalInstalledPrintersPath)
# Get the connection names for each printer
$GlobalInstalledPrinters = $GlobalKey.GetSubKeyNames() | ForEach-Object { $GlobalKey.OpenSubKey($_).GetValue('Printer').ToUpper() }
# Set as profile object printers
$GlobalProfileObject.Printers = $GlobalInstalledPrinters
# Add global profile to printer info object, inserts it to the top
$PrinterInformation.Insert(0, $GlobalProfileObject)
try
{
# Get all profiles on a computer
$AllUserProfiles = Get-CimInstance -ClassName Win32_UserProfile -ComputerName $ComputerName | Where-Object {
($_.SID -match $SIDPatter) -and ($_.LocalPath -notlike "*Administrator*")
}
# Set bool to not use WMI
[bool]$useWMI = $false
}
catch
{
# Set bool to use WMI
[bool]$useWMI = $true
# Fall back to WMI
$AllUserProfiles = Get-WmiObject -Class Win32_UserProfile -ComputerName $ComputerName | Where-Object {
($_.SID -match $sidpattern) -and ($_.LocalPath -notlike "*Administrator*")
}
}
if ($null -ne $AllUserProfiles)
{
# Get Current user, Printer information is retrieved differently when user is logged on
if (!($useWMI))
{
# Use CIM
$CurrentUser = (Get-CimInstance -ClassName Win32_ComputerSystem -Property Username -ComputerName $ComputerName).UserName
}
else
{
# Use WIM
$CurrentUser = (Get-WmiObject -Class Win32_ComputerSystem -Property Username -ComputerName $ComputerName).Username
}
# Check if a user is actually logged on
if ($CurrentUser)
{
# Remove NGHS\ from the username
$CurrentUser = $CurrentUser -replace '^(.*\\)', ''
}
foreach ($UserObject in $AllUserProfiles) {
# Skip admin profiles
if ($UserObject.LocalPath -match 'defaultuser0|NetworkService|LocalService|systemprofile')
{
continue
}
# Object to hold user information
$UserProfileObject = [pscustomobject]@{
SID = $null
Username = $null
Name = ' '
ADStatus = $null
LastUsed = $Null
Printers = $Null
}
# Set SID
$UserProfileObject.SID = $UserObject.SID
# Search for user in AD by SID
$UserADSearch = [adsi]"LDAP://<SID=$($UserProfileObject.SID)>"
# Set profile path and remote path
# This is used to load remote hives
$UserProfilePath = $UserObject.LocalPath
$UserRemotePath = ($UserProfilePath).Replace('C:\', "\\$OldComputerName\C`$\")
# Detect if AD search was successfull
if ($UserADSearch.Properties.sAMAccountName)
{
# Assign username based on AD variable
$UserName = $UserADSearch.Properties.sAMAccountName
$UserProfileObject.UserName = ($UserName).ToString()
$UserProfileObject.Name = ($UserADSearch.Properties.Name).ToString()
# Get enabled status of user.
$UserAccountControl = $UserADSearch.Properties.userAccountControl
switch ($UserAccountControl)
{
512 {
# User enabled
$UserProfileObject.ADStatus = 'Enabled'
}
514 {
# User disabled
$UserProfileObject.ADStatus = 'Disabled'
}
66048 {
# User enabled, password never expires
$UserProfileObject.ADStatus = 'Enabled'
}
66050 {
# User disabled, password never expires
$UserProfileObject.ADStatus = 'Disabled'
}
}
}
else
{
# Set username, based on profile path
$UserProfileObject.Username = $UserObject.LocalPath -replace '^(.*\\)', ''
}
if (!($useWMI))
{
$UserProfileObject.LastUsed = $UserObject.LastUseTime
}
else
{
# Set last used, random errors, unsure of cause
$UserProfileObject.LastUsed = $UserObject.ConvertToDateTime($UserObject.LastUseTime)
}
if ($UserProfileObject.Username -eq $CurrentUser)
{
# Open remote registry
$RemoteRegistry = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('USERS', $Name)
# Get registry subkey of user
$RegValue = $RemoteRegistry.GetSubKeyNames() | Where-Object { $_ -eq $UserProfileObject.SID }
# If subkey is found, get printer connections
if ($RegValue)
{
# Path to printer connection subkey
$PrinterKeyPath = "$RegValue\Printers\Connections\"
# Get subkeys in under Connections
$PrinterKeyValues = $RemoteRegistry.OpenSubKey($PrinterKeyPath).GetSubKeyNames()
# If printer connections exist
if ($PrinterKeyValues)
{
# Get Connections and replace weird formatting
$CurrentUserPrinterConnections = $PrinterKeyValues.Replace(',', '\').ToUpper()
# Add to user object
$UserProfileObject.Printers = $CurrentUserPrinterConnections | Where-Object { $PrinterInformation[0].Printers -notcontains $_ }
}
else
{
# No printers attached to user
# Keep object null
}
}
else
{
# Unable to find user key, is this user even logged on?
}
$RemoteRegistry.Close()
}
else
{
# User is not currently logged on, need to load their hives
# Test path to users hive
# Some profiles are registered in Windows, but not all create a hive
if (Test-Path "$UserRemotePath\NTUSER.dat")
{
# Add usersid to loaded hives object
$UserHivesLoaded.Add("HKU\$UserSID")
# Load the hive
reg load "HKU\$UserSID" "$UserRemotePath\NTUSER.DAT"
# Load USERS registry
$UserRegistry = [Microsoft.Win32.RegistryKey]::OpenBaseKey('USERS', 0)
# Load subkey that matches user SID
$RegValue = $UserRegistry.GetSubKeyNames() | Where-Object { $_ -eq $UserSID }
if ($null -eq $RegValue)
{
# Subkey for user not found
continue
}
else
{
# Path to printer connection subkey
$PrinterKeyPath = "$RegValue\Printers\Connections\"
# Get subkeys in under Connections
$PrinterKeyValues = $RemoteRegistry.OpenSubKey($PrinterKeyPath).GetSubKeyNames()
# If printer connections exist
if ($PrinterKeyValues)
{
# Get Connections and replace weird formatting
$CurrentUserPrinterConnections = $PrinterKeyValues.Replace(',', '\').ToUpper()
# Add to user object
$UserProfileObject.Printers = $CurrentUserPrinterConnections
}
else
{
# No printers attached to user
# Keep object null
}
}
# Garbage Collect, disconnects the read stream to the hive
[System.GC]::Collect()
# Unload hive, unloading is rate-limited, potential to error out
# Will unload after everything is done
reg unload "HKU\$userSID"
}
}
# Add profile to printer information object
$PrinterInformation.Add($UserProfileObject)
}
}
else
{
# Unable to find profiles on computer
# Most likely a WMI permission issue
$StatusReturn.Status = 'Unable to get user profile information'
return $StatusReturn
}
# Get USB based printers
##
## Get USB Printers
##
# Set profile object for USB connections
$USBProfileObject = [pscustomobject]@{
SID = 'USB BASED'
Username = 'USB BASED'
Name = 'USB BASED'
ADStatus = 'Enabled'
LastUsed = $Null
Printers = $Null
}
# Query WMI for printer information
if (!($useWMI))
{
# Use CIM
$USBInstalledPrinters = (Get-CimInstance -Query "SELECT Name FROM Win32_Printer WHERE (PortName LIKE 'USB%')" -ComputerName $ComputerName).Name
}
else
{
# Use WMI
$USBInstalledPrinters = (Get-WmiObject -Query "SELECT Name FROM Win32_Printer WHERE (PortName LIKE 'USB%')" -ComputerName $ComputerName).Name
}
# Check if any USB installed printers
if ($USBInstalledPrinters.Count -gt 0)
{
# Prefix each printer with USB in name
$USBInstalledPrinters = $USBInstalledPrinters | ForEach-Object { "USB\$_" }
}
# Set as profile object printers
$USBProfileObject.Printers = $USBInstalledPrinters
# Add usb profile to printer info object, inserts it as second row
$PrinterInformation.Insert(1, $USBProfileObject)
}
end
{
foreach ($LoadedHive in $UserHivesLoaded) {
# Garbage Collect, disconnects the read stream to the hive
[System.GC]::Collect()
# Unload hive, unloading is rate-limited, potential to error out
# Will unload after everything is done
reg unload "$LoadedHive"
}
$StatusReturn.Status = 'Information Retrieved'
return $StatusReturn
}
}