-
Notifications
You must be signed in to change notification settings - Fork 94
/
Copy pathGet-PrinterHosts.ps1
58 lines (51 loc) · 2.33 KB
/
Get-PrinterHosts.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
# need admin rights
# if you include USB printers on workstations, it picks up peoples home usb printers as well...
# also including shared printers may include home printers
function Get-PrinterHosts {
Param (
[string]$comp = $env:COMPUTERNAME
)
if (!$comp) { throw 'No comps.' }
$ping = New-Object System.Net.NetworkInformation.Ping
try {
$result = $ping.Send($comp)
} catch {
$result = $null
}
if ($result.Status -eq 'Success') {
$printers = Get-WmiObject Win32_Printer -ComputerName $comp | select name, sharename, systemname, portname, shared, printerpapernames, capabilitydescriptions, location, comment, drivername
# get the ip address
$ip = $result.Address.ToString()
foreach ($printer in $printers) {
[pscustomobject]@{
Computer = $comp.ToUpper()
ComputerIP = $ip
Printer = $printer.Name.ToUpper()
ShareName = $(try { $printer.ShareName.ToUpper() } catch { $null })
SystemName = $(if ($printer.SystemName.StartsWith('\')) {$printer.SystemName.Substring(2).ToUpper()} else {$printer.SystemName.ToUpper()})
Location = $(try { $printer.Location } catch { $null })
Comment = $(try { $printer.Comment } catch { $null })
DriverName = $(try { $printer.DriverName } catch { $null })
Port = $printer.PortName
Shared = $printer.Shared
PrinterPaperNames = $printer.PrinterPaperNames
CapabilityDescriptions = $printer.CapabilityDescriptions
}
}
} else {
[pscustomobject]@{
Computer = $comp.ToUpper()
ComputerIP = '-'
Printer = '-'
ShareName = '-'
SystemName = '-'
Location = '-'
Comment = '-'
DriverName = '-'
Port = '-'
Shared = '-'
PrinterPaperNames = '-'
CapabilityDescriptions = '-'
}
}
}