-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaction1_data_source_network_printers.ps1
More file actions
108 lines (91 loc) · 3.77 KB
/
action1_data_source_network_printers.ps1
File metadata and controls
108 lines (91 loc) · 3.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
$ProgressPreference = 'SilentlyContinue'
$ErrorActionPreference = 'Stop'
function Get-MacFromIP {
param($IP)
if (-not $IP) { return $null }
try { Test-Connection -Count 1 -Quiet -ComputerName $IP -ErrorAction SilentlyContinue | Out-Null } catch {}
try {
$n = Get-NetNeighbor -AddressFamily IPv4 -IPAddress $IP -ErrorAction SilentlyContinue
if ($n -and $n.LinkLayerAddress) { return ($n.LinkLayerAddress -replace ':','-').ToUpper() }
} catch {}
try {
$arp = arp -a 2>$null
if ($arp -match "$IP\s+([0-9a-fA-F\.\:-]{11,17})") {
$mac = $Matches[1]
return ($mac -replace '\.', '-' -replace ':', '-' ).ToUpper()
}
} catch {}
return $null
}
function Get-PrinterIPFromPortName {
param($PortName)
if (-not $PortName) { return $null }
if ($PortName -match '((?:\d{1,3}\.){3}\d{1,3})') { return $Matches[1] }
if ($PortName -match 'socket://([^:\/]+)') { return $Matches[1] }
if ($PortName -match '^[a-zA-Z0-9\-.]+$') {
try {
$addrs = [System.Net.Dns]::GetHostAddresses($PortName) | Where-Object { $_.AddressFamily -eq 'InterNetwork' }
if ($addrs) { return $addrs[0].IPAddressToString }
} catch {}
}
return $null
}
function Get-DeterministicKey {
param($s)
if (-not $s) { $s = (Get-Random -Maximum 9999999).ToString() }
$md5 = [System.Security.Cryptography.MD5]::Create()
$bytes = [System.Text.Encoding]::UTF8.GetBytes($s)
$hash = $md5.ComputeHash($bytes)
$hex = ([System.BitConverter]::ToString($hash)).Replace('-','').ToLower()
return "A1_$hex"
}
# --- Collect printers (suppress any formatting) ---
try {
$printers = Get-Printer -ErrorAction Stop
} catch {
$printers = Get-CimInstance -ClassName Win32_Printer -ErrorAction SilentlyContinue
}
try { $printerPorts = Get-PrinterPort -ErrorAction SilentlyContinue } catch { $printerPorts = @() }
try { $tcpipPorts = Get-CimInstance -ClassName Win32_TCPIPPrinterPort -ErrorAction SilentlyContinue } catch { $tcpipPorts = @() }
$results = @()
foreach ($p in $printers) {
$name = ($p.Name) -as [string]
$driver = ($p.DriverName) -as [string]
$portName = ($p.PortName) -as [string]
$deviceId = ($p.DeviceId) -as [string]
$status = $null
if ($p.PrinterStatus -ne $null) { $status = $p.PrinterStatus }
$ip = $null; $mac = $null; $snmp = $null
if ($portName) {
try { $pp = $printerPorts | Where-Object { $_.Name -eq $portName } | Select-Object -First 1 } catch { $pp = $null }
if ($pp -and $pp.PrinterHostAddress) { $ip = $pp.PrinterHostAddress }
}
if (-not $ip -and $portName -and $tcpipPorts) {
try { $tcp = $tcpipPorts | Where-Object { $_.Name -eq $portName -or $_.PortName -eq $portName } | Select-Object -First 1 } catch { $tcp = $null }
if ($tcp) {
if ($tcp.HostAddress) { $ip = $tcp.HostAddress }
if ($tcp.PSObject.Properties.Match('SNMPEnabled').Count -gt 0 -and $tcp.SNMPEnabled -ne $null) { $snmp = [bool]$tcp.SNMPEnabled }
}
}
if (-not $ip -and $portName) { $ip = Get-PrinterIPFromPortName -PortName $portName }
if (-not $ip -and $portName) {
try {
$addrs = [System.Net.Dns]::GetHostAddresses($portName) | Where-Object { $_.AddressFamily -eq 'InterNetwork' }
if ($addrs) { $ip = $addrs[0].IPAddressToString }
} catch {}
}
if (-not $ip) { continue }
$mac = Get-MacFromIP -IP $ip
$obj = [PSCustomObject]@{
PrinterName = $name
DeviceID = $deviceId
DriverName = $driver
IPAddress = $ip
MacAddress = $mac -replace '-', ':'
SNMPEnabled = $snmp
PrinterStatus = $status
A1_Key = Get-DeterministicKey -s $ip
}
$results += $obj
}
$results