-
Notifications
You must be signed in to change notification settings - Fork 95
/
Get-PrinterClients.ps1
51 lines (45 loc) · 1.47 KB
/
Get-PrinterClients.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
# KNOWN ISSUES:
# only finds printers that are *NOT* locally hosted
# if looking for all possible printers, might have to use Get-PrinterHosts as well
#
# REQUIRES:
# admin rights
function Get-PrinterClients {
Param (
[string]$comp = $env:COMPUTERNAME
)
if (!$comp) { throw 'No comp.' }
$ping = New-Object System.Net.NetworkInformation.Ping
try {
$result = $ping.Send($comp)
} catch {
$result = $null
}
if ($result.Status -eq 'Success') {
$reg = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('Users', $comp)
[string[]]$users = $reg.GetSubKeyNames() | ? {$_ -match '-\d{5}$'}
foreach ($user in $users) {
try {
$printers = $reg.OpenSubKey("$user\printers\connections").GetSubKeyNames()
} catch {
$printers = '-'
}
if ($printers -ne '-') {
$printers = @($printers | % {$_.Substring(2).Replace(',', '\').ToUpper()} | select -Unique) -join '; '
}
[pscustomobject]@{
Computer = $comp
IP = $result.Address.ToString()
User = ([System.Security.Principal.SecurityIdentifier]($user)).Translate([System.Security.Principal.NTAccount]).Value
Printer = $printers
}
}
} else {
[pscustomobject]@{
Computer = $comp
IP = '-'
User = '-'
Printer = '-'
}
}
}