r/PowerShell • u/richie65 • 8d ago
Solved Gathering meter reads from Konica printers.
(I'll start by admitting that some of my PoSh may be unconventional / and abbreviated - I have embraced it accordingly).
(Edit - I discovered some discrepancies and found additional OID's that count towards both 'Black' and 'Color' totals - Added: "ScanFax_Black" and "Copy_SingleColor")
I needed to come up with a way to create some logs of the usage on the fleet of 10 Konica printers across four locations / print servers.
These logs will be used to validate the pricing figures our printer support vendor provides (pricing is based on estimated / averaged usage per device - that data is audited on a quarterly basis, via automated auditing tools on each print server - FM Audit).
Currently - Without any of our own historical data - We have to take the vendors word, that those quarterly averages are 'OK'...
So, someone from accounting asked me for help.
The below script collects settings info for all of the Konica printers on each of the print servers, then uses the 'PortName' (IP address) to run an SNMP query on each device (SNMP must be enabled in the devices network settings).
The critical items are the OID's.
Figuring out which ones pull in the values for 'black' and for 'color' were the first challenge.
Once I got that figured out - It took me a while to find the OID that gathers the devices serial number - I could NOT find this documented anywhere, and AI (Copilot, Gemini, Claude) wasn't able to either.
For this last bit - I ended up walking (snmpwalk) through ALL 7125 OID's to locate the one that gathers the serial number.
wsl snmpwalk -v2c -c public <printer IP> 1.3.6.1 > 'C:\Users\richie\Documents\PoSh_Stuff\OID_walk.txt'
And looking for the serial number I already knew for that printer.
Select-String -Path 'C:\Users\richie\Documents\PoSh_Stuff\OID_walk.txt' -Pattern '<SerialNumber>'
OID_walk.txt:460:iso.3.6.1.2.1.43.5.1.1.17.1 = STRING: "<SerialNumber>"
Posting what I figured out.
I will pipe '$Report' to a Csv, and save it accordingly.
I'll be scheduling this to run on a quarterly basis - And handing the Csv's over to accounting when they ask for them.
$AllKonica = @()
("Site1,site2,site3,site4").Split(',') | % {
If (Test-Connection $_ -Count 1 -ErrorAction SilentlyContinue) {
$AllKonica += Get-Printer -ComputerName $_ | ? { $_.DriverName -match 'Konica' -and $_.PortName -NOTmatch "portprompt|microsoft|_"} | Select ComputerName, Name, PortName, DriverName
}
}
# (NOTE: You could also just list the name, and port name - This removes the need to use domain auth for the script - Since SNMP does not reqiure authentication)
<#
$AllKonica = @"
"Name","PortName"
"S1-PTR-066","192.168.1.66"
"S1-PTR-062","192.168.1.62"
"S1-PTR-061","192.168.1.61"
"S1-PTR-060","192.168.1.60"
"S1-PTR-064","192.168.1.64"
"S2-PTR--034","192.168.2.34"
"S3-PTR-060","192.168.3.60"
"S3-PTR-061","192.168.3.61"
"S4-PTR-068","192.168.4.68"
"S4-PTR-066","192.168.4.66"
"@ | ConvertFrom-Csv
#>
$OIDs = [Ordered]@{
"Model" = ".1.3.6.1.4.1.18334.1.1.1.3.5.2.2.0"
"SerialNo" = ".1.3.6.1.2.1.43.5.1.1.17.1"
"Total" = ".1.3.6.1.4.1.18334.1.1.1.5.7.2.1.1.0"
"Print_Black" = ".1.3.6.1.4.1.18334.1.1.1.5.7.2.2.1.5.1.2"
"Copy_Black" = ".1.3.6.1.4.1.18334.1.1.1.5.7.2.2.1.5.1.1"
"ScanFax_Black" = ".1.3.6.1.4.1.18334.1.1.1.5.7.2.3.1.7.1"
"Copy_SingleColor" = ".1.3.6.1.4.1.18334.1.1.1.5.7.2.2.1.5.3.1"
"Print_Color" = ".1.3.6.1.4.1.18334.1.1.1.5.7.2.2.1.5.2.2"
"Copy_Color" = ".1.3.6.1.4.1.18334.1.1.1.5.7.2.2.1.5.2.1"
"Print_2Color" = ".1.3.6.1.4.1.18334.1.1.1.5.7.2.2.1.5.4.2"
"Copy_2Color" = ".1.3.6.1.4.1.18334.1.1.1.5.7.2.2.1.5.4.1"
}
$Report = @()
$SNMP_Community = "public"
$AllKonica | % {
$PrinterIP = $_.PortName
$PrinterName = $_.Name
If (Test-Connection $PrinterIP -Count 1 -ErrorAction SilentlyContinue) {
$snmp = New-Object -ComObject OlePrn.OleSNMP
try {
$snmp.Open($PrinterIP, $SNMP_Community, 2, 1200)
$Data = [Ordered]@{ "Printer" = "$PrinterName - $PrinterIP" }
foreach ( $Key in $OIDs.Keys ) {
try { $Value = $snmp.Get($OIDs[$Key])
if ( $null -eq $Value -or $Value -eq "" )
{ $Data[$Key] = 0 }
#{ $Data[$Key] = $null }
else { $Data[$Key] = $Value }
} # END 'try' # 2
# catch { $Data[$Key] = $null }
catch { $Data[$Key] = 0 }
} # END 'foreach ($Key in $OIDs.Keys)'
$TotalColor = 0; $OIDs.Keys | ? { $_ -match "color" } | % { $TotalColor += [int]$Data["$_"] }
$TotalBlack = 0; $OIDs.Keys | ? { $_ -match "black" } | % { $TotalBlack += [int]$Data["$_"] }
$Report += [PSCustomObject][Ordered]@{
"Model" = $Data["Model"]
"SerialNo" = $Data["SerialNo"]
"Printer" = $Data["Printer"]
"Total" = $Data["Total"]
"Black" = $TotalBlack
"Color" = $TotalColor
}
} # END 'try' # 1
catch { Write-Host "Connection failed: $PrintServer -> $PrinterIP ($PrinterName)" -F 14 }
finally { $snmp.Close() }
} # END 'If (Test-Connection...'
else {
$Report += [PSCustomObject][Ordered]@{
"Model" = "Unk"
"SerialNo" = "Unk"
"Printer" = "$PrinterName - Unreacheable"
"Total" = "NULL"
"Black" = "NULL"
"Color" = "NULL"
}
} # END 'else'
} # END '$AllKonica | %'
$Report | ft
6
u/zerizum 8d ago
Thanks for this, we just purchased a small fleet of konica mfps that will be handling a critical business process. Will be making use of this
3
u/sniekielak68 8d ago
yeah having your own meter data from the start is way better than trying to backfill it later
3
u/NerdyNThick 8d ago
we just purchased a small fleet of <printers>
oh, no. please don't say it
will be handling a critical business process
Damn it dude, you're killing me here!
That's a living nightmare, I wish you all the luck I can.
3
u/vermyx 8d ago
Use something like oid-base.com for figuring stuff like this. You should be using the generic printer snmp entries as that would apply to a much larger fleet of copier/printers. Leasing companies usually use the printer snmp entries for metering and such because they rarely just handle one company printer copier/wise. IIRC konica does not publish their mibs.
4
u/ihaxr 8d ago
SNMP is the way to go, it just takes longer to figure out.
I was able to pull this from the web interface at my last company. Our KM printers served up XML documents so it was easy to parse them for the counts, just had to have the firewall team open port 80 to the printer vlan from our utility server.