Last active
July 17, 2022 07:15
-
-
Save Pome-ro/c97c14c7913ecba8d0d31296ecc57698 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Com Object for making SNMP calls | |
$SNMP = new-object -ComObject olePrn.OleSNMP | |
# Printer objects from our print server | |
$All_Printers = get-printer -ComputerName $Config.PrintServer | |
# Array of printers to output, which will contain toner data, and some data from our print server. | |
$Printers = New-Object System.Collections.ArrayList | |
foreach ($Printer in $All_Printers) { | |
# Port name from the print server. You might have to clean up your port names | |
# if you have dupes or you've named them other then the IP address. | |
$Address = $Printer.PortName | |
#Name from the Print Server | |
$Name = $Printer.Name | |
# This could be better, but we check if the printer is online, | |
# if not we set its online state to false. | |
if (!(Test-Connection $address -Quiet -Count 1)) {$onlineState = $False} | |
# If the printer is online, we get the toner data. | |
if (Test-Connection $address -Quiet -Count 1) { | |
$onlineState = $True | |
# The Open method takes 4 params. Host, Community, Retry, and Timeout. | |
$SNMP.Open($Address, "public", 2, 3000) | |
# The Get method takes one param, a string representing the OID you want to query. | |
# This gets the printer type, in this case, HP M553DN. | |
$printertype = $snmp.Get(".1.3.6.1.2.1.25.3.2.1.3.1") | |
# This is where things get weird. There are two OIDs you need to get, | |
# one I've called Toner Volume, more accuretly, Toner maximum? And then the Current Volume. | |
# These are not small numbers if I remember correctly. Lastly, I calculate a percentage | |
# based on those two numbers so we can get some data worth using. | |
$black_tonervolume = $snmp.get("43.11.1.1.8.1.1") | |
$black_currentvolume = $snmp.get("43.11.1.1.9.1.1") | |
[int]$black_percentremaining = ($black_currentvolume / $black_tonervolume) * 100 | |
$cyan_tonervolume = $snmp.get("43.11.1.1.8.1.2") | |
$cyan_currentvolume = $snmp.get("43.11.1.1.9.1.2") | |
[int]$cyan_percentremaining = ($cyan_currentvolume / $cyan_tonervolume) * 100 | |
$magenta_tonervolume = $snmp.get("43.11.1.1.8.1.3") | |
$magenta_currentvolume = $snmp.get("43.11.1.1.9.1.3") | |
[int]$magenta_percentremaining = ($magenta_currentvolume / $magenta_tonervolume) * 100 | |
$yellow_tonervolume = $snmp.get("43.11.1.1.8.1.4") | |
$yellow_currentvolume = $snmp.get("43.11.1.1.9.1.4") | |
[int]$yellow_percentremaining = ($yellow_currentvolume / $yellow_tonervolume) * 100 | |
} | |
# I then store that data in a PSCustomObject, and add it to our ArrayList, | |
# and then close out the SNMP connection. There is a helper function here | |
# called ReturnZeroIfNegitive (I should fix that spelling) that does what it says. | |
# I'm not 100% sure why, and I'm sure someone can tell me, but sometimes you'll get a result of -2 or -3, | |
# and this just zeros that out. | |
$PrinterData = [PSCustomObject] @{ | |
"Name" = $Name | |
"Type" = $printertype | |
"Address" = $Address | |
"OnlineState" = $onlineState | |
"Toner" = @{ | |
"Name" = "Toner Levels" | |
"Max" = 100 # This is a hacky workaround for setting min and max values in UD bar charts. | |
"Min" = 0 | |
"Black" = ReturnZeroIfNegitive -Data $black_percentremaining | |
"Yellow" = ReturnZeroIfNegitive -Data $Yellow_percentremaining | |
"Cyan" = ReturnZeroIfNegitive -Data $Cyan_percentremaining | |
"Magenta" = ReturnZeroIfNegitive -Data $Magenta_percentremaining | |
} | |
} | |
$Printers.Add($PrinterData) | |
$SNMP.Close() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment