Darkbat91 / Powershell

Powershell scripts that are useful
10 stars 6 forks source link

Get-PrinterCounts.ps1 return no data #4

Open bezik46 opened 10 months ago

bezik46 commented 10 months ago

I hope that eventually have the correct format for the cxml (testing with 1 printer)

<Objs Version="1.1.0.1" xmlns="http://schemas.microsoft.com/powershell/2004/04">
  <Obj RefId="0">
    <TN RefId="0">
      <T>System.Management.Automation.PSCustomObject</T>
      <T>System.Object</T>
    </TN>
    <MS>
      <S N="PrinterManufacturer">Sharp</S>
      <S N="PrinterIP">10.10.20.22</S>
    </MS>
  </Obj>
</Objs>

dll from: https://www.nuget.org/packages/Lextm.SharpSnmpLib is loaded from C:\PSScripts\SharpSnmpLib.dll

.ps1 runs with no error, .log gets created but no output csv with any data

Started INFO Logging for Get-PrinterCounts at 2024-01-18 12:48:12
Script Version: 1

Any ideas?

If I run it by hand I get:

PS C:\PSScripts> #Color Pages Sharp
PS C:\PSScripts> $OIDColorSharp = ".1.3.6.1.4.1.2385.1.1.19.2.1.3.5.4.63"
PS C:\PSScripts> #BW Pages Sharp
PS C:\PSScripts> $OIDBWSharp = ".1.3.6.1.4.1.2385.1.1.19.2.1.3.5.4.61"

### Functions ###

function Invoke-SnmpWalk1 ([string]$sIP, $sOIDstart, [string]$Community = "public", [int]$UDPport = 161, [int]$TimeOut=3000) {
    [System.Reflection.Assembly]::LoadFile((Resolve-path C:\PSScripts\SharpSnmpLib.dll))
    #> $null
    # $sOIDstart
    # $TimeOut is in msec, 0 or -1 for infinite

    # Create OID object
    $oid = New-Object Lextm.SharpSnmpLib.ObjectIdentifier ($sOIDstart)

    # Create list for results
    $results = New-Object 'System.Collections.Generic.List[Lextm.SharpSnmpLib.Variable]' # PowerShell v3

    # Create endpoint for SNMP server
    $ip = [System.Net.IPAddress]::Parse($sIP)
    $svr = New-Object System.Net.IpEndPoint ($ip, 161)

    # Use SNMP v2 and walk mode WithinSubTree (as opposed to Default)
    $ver = [Lextm.SharpSnmpLib.VersionCode]::V2
    $walkMode = [Lextm.SharpSnmpLib.Messaging.WalkMode]::WithinSubtree

    # Perform SNMP Get
    try {
        [Lextm.SharpSnmpLib.Messaging.Messenger]::Walk($ver, $svr, $Community, $oid, $results, $TimeOut, $walkMode)
    } catch {
        Write-Host "SNMP Walk error: $_"
        Return $null
    }

    $res = @()
    foreach ($var in $results) {
        $line = "" | Select OID, Data
        $line.OID = $var.Id.ToString()
        $line.Data = $var.Data.ToString()
        $res += $line
    }

    $res
}
$DebugPreference = "Continue"
Write-Debug -Message "Loaded Functions"

PS C:\PSScripts> Invoke-SnmpWalk1 -sIP 10.10.20.22 -sOIDstart $OIDColorSharp

GAC    Version        Location
---    -------        --------
False  v4.0.30319     C:\PSScripts\SharpSnmpLib.dll
0

I can get the correct values just fine with

snmpwalk -C:c -v:2 -c:public -r:10.10.20.22 -t:10 -os:.1.3.6.1.4.1.2385.1.1.19.2.1.3.5.4.60 -op:.1.3.6.1.4.1.2385.1.1.19.2.1.3.5.4.63
Darkbat91 commented 10 months ago

@ bezik46 Thank you for the detailed issue notes. I have not worked on this repo in 8 years nor have i used the code in roughly that amount of time. It is entirely possible that the hooks into the version of SharpSnmplib are not correct with the current version and that would need updated or that the library . If you have an HP or a Konica printer you could validate with that.

Other than the library hooks potentially being incorrect I see in your snmpWalk command you are starting at 4.60 and ending at 4.63 but the code you are using 61 and 63 are you sure that those are the correct OIDs for the page counts?

Recommended Solution

It looks like there is a Powershell gallery function that can be used to just get the SNMP OID that is using more current information than this legacy code.

bezik46 commented 10 months ago

Yes, values are correct (confirmed on my printers)

https://www.copytechnet.com/forums/sharp/176375-get-total-pages-counters-snmp.html#google_vignette

And yes, using Proxx.SNMP which does work fine

Will cobble own script, but do not like inventing wheel when one might already exist