-
Notifications
You must be signed in to change notification settings - Fork 0
/
PrinterSNMPManagement.ps1
84 lines (73 loc) · 3.32 KB
/
PrinterSNMPManagement.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
Import-Module SNMP
# Define your devices with name and IP address
$devices = @(
@{ Name = "Printer Name"; IPAddress = "IP Address"; ColorPrinter = $true; OIDBlack = @("1.3.6.1.4.1.18334.1.1.1.5.7.2.2.1.5.1.1", "1.3.6.1.4.1.18334.1.1.1.5.7.2.2.1.5.1.2", "1.3.6.1.4.1.18334.1.1.1.5.7.2.3.1.12.1", "1.3.6.1.4.1.18334.1.1.1.5.7.2.3.1.7.1"); OIDColor = @("1.3.6.1.4.1.18334.1.1.1.5.7.2.2.1.5.2.2", "1.3.6.1.4.1.18334.1.1.1.5.7.2.2.1.5.2.1", "1.3.6.1.4.1.18334.1.1.1.5.7.2.2.1.5.4.1", "1.3.6.1.4.1.18334.1.1.1.5.7.2.3.1.7.1")},
@{ Name = "Printer Name"; IPAddress = "IP Address"; ColorPrinter = $false; OIDBlack = "1.3.6.1.4.1.18334.1.1.1.5.7.2.1.8.0" },
@{ Name = "Printer Name"; IPAddress = "IP Address"; ColorPrinter = $true; OIDBlack = @("1.3.6.1.4.1.18334.1.1.1.5.7.2.2.1.5.1.1", "1.3.6.1.4.1.18334.1.1.1.5.7.2.2.1.5.1.2"); OIDColor = @("1.3.6.1.4.1.18334.1.1.1.5.7.2.2.1.5.2.2", "1.3.6.1.4.1.18334.1.1.1.5.7.2.2.1.5.2.1", "1.3.6.1.4.1.18334.1.1.1.5.7.2.2.1.7.2.2")}
)
#; OIDBlack = ""
# Function to get SNMP data and output counts, including checking online status
function Get-SNMPDataForDevice {
param(
[string]$Name,
[string]$IPAddress,
[bool]$ColorPrinter,
[string[]]$OIDBlack,
[string[]]$OIDColor
)
$pingResult = Test-Connection -ComputerName $IPAddress -Count 1 -Quiet
$totalBlackWhite = 0
$totalColor = 0
if ($pingResult) {
# For black and white counts
foreach ($oid in $OIDBlack) {
$totalBlackWhite += (Get-SNMPData -IP $IPAddress -Community public -OID $oid).Data
}
if ($ColorPrinter) {
# For color counts
foreach ($oid in $OIDColor) {
$totalColor += (Get-SNMPData -IP $IPAddress -Community public -OID $oid).Data
}
# Output results for color printer
[PSCustomObject]@{
Name = $Name
IP = $IPAddress
PrintsBlackWhite = $totalBlackWhite
PrintsColor = $totalColor
Status = "Online"
}
} else {
# Output results for black and white printer
[PSCustomObject]@{
Name = $Name
IP = $IPAddress
PrintsBlackWhite = $totalBlackWhite
PrintsColor = "N/A"
Status = "Online"
}
}
} else {
# Output results if offline
[PSCustomObject]@{
Name = $Name
IP = $IPAddress
PrintsBlackWhite = "-"
PrintsColor = "-"
Status = "Offline"
}
}
}
# Array to collect output
$output = @()
# Loop through each device and retrieve SNMP data
foreach ($device in $devices) {
$newDeviceName = "`"$($device.Name)`""
# Append the data to the output using the new device name
$output += Get-SNMPDataForDevice -Name $newDeviceName -IPAddress $device.IPAddress -ColorPrinter $device.ColorPrinter -OIDBlack $device.OIDBlack -OIDColor $device.OIDColor
}
# Display the collected output
$output | Format-Table -AutoSize
$Date = Get-Date -Format "yyyy-MM"
$Path = 'C:\temp\Printercounts_' + $Date + '.csv'
# Export the output to a CSV file with specified headers
$output | Export-Csv -Path $Path -NoTypeInformation