1

I'm sort of new to powershell. But long story short, I'm building an application to monitor devices through SNMP. Since SNMP is pretty basic through powershell my plan is to schedule a script to run every once in a while which is going to poll snmp data from multiple IP-adresses. The plan is to output all strings from all OIDS to a .txt file and then import the content within the .txt file to a GUI built in Python.

My current problem is the output. I get the poll to work, but since the output is within the loop the output either gets doubled or overwritten. In the current setup, is there any way to output without overwriting data while not duplicating already written data in the file?

Currently my script is something like:

$ips = Get-Content "C:\temp\ip.txt"
foreach($ip in $ips) {

$SNMP = New-Object -ComObject olePrn.OleSNMP
$SNMP.open($ip,'public',3,1000)

$hostname = $SNMP.Get(".1.3.6.1.2.1.1.5.0")
$blacktonercap = $SNMP.Get(".1.3.6.1.2.1.43.11.1.1.8.1.1")
$blacktonerlvl = $SNMP.Get(".1.3.6.1.2.1.43.11.1.1.9.1.1")
$cyantonercap = $SNMP.Get(".1.3.6.1.2.1.43.11.1.1.8.1.2")
$cyantonerlvl = $SNMP.Get(".1.3.6.1.2.1.43.11.1.1.9.1.2")
$magentatonercap = $SNMP.Get(".1.3.6.1.2.1.43.11.1.1.8.1.3")
$magentatonerlvl = $SNMP.Get(".1.3.6.1.2.1.43.11.1.1.9.1.3")
$yellowtonercap = $SNMP.Get(".1.3.6.1.2.1.43.11.1.1.8.1.4")
$yellowtonerlvl = $SNMP.Get(".1.3.6.1.2.1.43.11.1.1.9.1.4")
$drumR1 = $SNMP.Get(".1.3.6.1.2.1.43.11.1.1.9.1.5")
$drumR2 = $SNMP.Get(".1.3.6.1.2.1.43.11.1.1.9.1.6")
$drumR3 = $SNMP.Get(".1.3.6.1.2.1.43.11.1.1.9.1.7")
$drumR4 = $SNMP.Get(".1.3.6.1.2.1.43.11.1.1.9.1.8")
$wastebox = $SNMP.Get(".1.3.6.1.2.1.43.11.1.1.9.1.10")
$transferbelt = $SNMP.Get(".1.3.6.1.2.1.43.11.1.1.9.1.11")                                                                 #överföringsband
$phaser = $SNMP.Get(".1.3.6.1.2.1.43.11.1.1.9.1.12")                                                                       #överföringsrulle

$Blacktonerstatus = [Math]::round($blacktonerlvl/$blacktonercap*100)
$Cyantonerstatus = [Math]::round($cyantonerlvl/$cyantonercap*100)
$magentatonerstatus = [Math]::round($magentatonerlvl/$magentatonercap*100)
$yellowtonerstatus = [Math]::round($yellowtonerlvl/$yellowtonercap*100)

Write-Output $hostname "Svart toner" $blacktonerstatus"%" "Cyan toner" $cyantonerstatus"%"  "Magenta toner" $magentatonerstatus"%" "Gul toner" $yellowtonerstatus"%" "Trumkassett R1" $drumR1"%" "Trumkassett R2" $drumR2"%" "Trumkassett R3" $drumR3"%" "Trumkassett R4" $drumR4"%" "Överskottsbehållare" $wastebox"%" "Överföringsband" $transferbelt"%" "Överföringsrulle" $phaser"%" >> "C:\temp\test.txt"

$SNMP.close

}

Note that this code is just a test to see if i can make it work, hence the mess.

Output looks something like this the second time you run the script:

Hostname1
Svart toner
11%
Cyan toner
1%
Magenta toner
1%
Gul toner
50%
Trumkassett R1
79%
Trumkassett R2
26%
Trumkassett R3
21%
Trumkassett R4
15%
Överskottsbehållare
100%
Överföringsband
24%
Överföringsrulle
59%
Hostname2
Svart toner
42%
Cyan toner
100%
Magenta toner
88%
Gul toner
95%
Trumkassett R1
0%
Trumkassett R2
72%
Trumkassett R3
69%
Trumkassett R4
98%
Överskottsbehållare
100%
Överföringsband
41%
Överföringsrulle
41%
Hostname1
Svart toner
11%
Cyan toner
1%
Magenta toner
1%
Gul toner
50%
Trumkassett R1
79%
Trumkassett R2
26%
Trumkassett R3
21%
Trumkassett R4
15%
Överskottsbehållare
100%
Överföringsband
24%
Överföringsrulle
59%
Hostname
Svart toner
42%
Cyan toner
100%
Magenta toner
88%
Gul toner
95%
Trumkassett R1
0%
Trumkassett R2
72%
Trumkassett R3
69%
Trumkassett R4
98%
Överskottsbehållare
100%
Överföringsband
41%
Överföringsrulle
41%
4
  • So you want a database? Commented Jun 1, 2022 at 8:06
  • Well i suppose something along those lines. Although this will be developed mostly for a few colleagues. I will have to input around 40-100 IP-adresses in the IP.txt, would the best option be to build a database? Commented Jun 1, 2022 at 8:12
  • A simple *.csv file is technically a database of sorts :-). I’m not really clear what you mean about data getting “doubled / overwritten / duplicated” though - could you clarify? Commented Jun 1, 2022 at 8:18
  • I was thinking about exporting to a csv and then importing the data into a GUI. Well, in the current structure of my script i have the "Write-Output" inside my loop. I also have 2 IP-adresses in the IP.txt file. The loop fetches the OID data and then writes the output into test.txt for each IP adress. If i use "Out-file" without "-Append" the latter IP-adress would overwrite the first IP Adress "Write-Output". And if I use "-Append" the .txt file would just write each hostname and its variables twice in the document if that makes any sense. Commented Jun 1, 2022 at 8:20

1 Answer 1

1

You can put the output to an Array, I guess that would be the cleanest solution.

$ips = Get-Content "C:\temp\printer.txt"
$output = @()

foreach($ip in $ips)
{
    $SNMP = New-Object -ComObject olePrn.OleSNMP
    $SNMP.open($ip,'public',3,1000)

    $props = @{
        hostname = $SNMP.Get(".1.3.6.1.2.1.1.5.0")
        blacktonercap = $SNMP.Get(".1.3.6.1.2.1.43.11.1.1.8.1.1")
        blacktonerlvl = $SNMP.Get(".1.3.6.1.2.1.43.11.1.1.9.1.1")
        cyantonercap = $SNMP.Get(".1.3.6.1.2.1.43.11.1.1.8.1.2")
        cyantonerlvl = $SNMP.Get(".1.3.6.1.2.1.43.11.1.1.9.1.2")
        magentatonercap = $SNMP.Get(".1.3.6.1.2.1.43.11.1.1.8.1.3")
        magentatonerlvl = $SNMP.Get(".1.3.6.1.2.1.43.11.1.1.9.1.3")
        yellowtonercap = $SNMP.Get(".1.3.6.1.2.1.43.11.1.1.8.1.4")
        yellowtonerlvl = $SNMP.Get(".1.3.6.1.2.1.43.11.1.1.9.1.4")
        drumR1 = $SNMP.Get(".1.3.6.1.2.1.43.11.1.1.9.1.5")
        drumR2 = $SNMP.Get(".1.3.6.1.2.1.43.11.1.1.9.1.6")
        drumR3 = $SNMP.Get(".1.3.6.1.2.1.43.11.1.1.9.1.7")
        drumR4 = $SNMP.Get(".1.3.6.1.2.1.43.11.1.1.9.1.8")
        wastebox = $SNMP.Get(".1.3.6.1.2.1.43.11.1.1.9.1.10")
        transferbelt = $SNMP.Get(".1.3.6.1.2.1.43.11.1.1.9.1.11")
        phaser = $SNMP.Get(".1.3.6.1.2.1.43.11.1.1.9.1.12")
    }

    $ServiceObject = New-Object -TypeName PSObject -Property $props
    $output += $ServiceObject

    $SNMP.close
}

$output | export-csv "C:\temp\printer_test.csv" -notypeinformation

There are 2 ways to do the calculation:

  1. You get the SNMP data before you fill the props for your array, then you are able to use the previous created variables for the calculation.
$blacktonercap = $SNMP.Get(".1.3.6.1.2.1.43.11.1.1.8.1.1")
$blacktonerlvl = $SNMP.Get(".1.3.6.1.2.1.43.11.1.1.9.1.1")
$cyantonercap = $SNMP.Get(".1.3.6.1.2.1.43.11.1.1.8.1.2")
$cyantonerlvl = $SNMP.Get(".1.3.6.1.2.1.43.11.1.1.9.1.2")
$magentatonercap = $SNMP.Get(".1.3.6.1.2.1.43.11.1.1.8.1.3")
$magentatonerlvl = $SNMP.Get(".1.3.6.1.2.1.43.11.1.1.9.1.3")
$yellowtonercap = $SNMP.Get(".1.3.6.1.2.1.43.11.1.1.8.1.4")
$yellowtonerlvl = $SNMP.Get(".1.3.6.1.2.1.43.11.1.1.9.1.4")

$props = @{
    blacktonercap = $blacktonercap
    blacktonerlvl = $blacktonerlvl
    cyantonercap = $cyantonercap
    cyantonerlvl = $cyantonerlvl
    magentatonercap = $magentatonercap
    magentatonerlvl = $magentatonerlvl
    yellowtonercap = $yellowtonercap
    yellowtonerlvl = $yellowtonerlvl
    drumR1 = $SNMP.Get(".1.3.6.1.2.1.43.11.1.1.9.1.5")
    drumR2 = $SNMP.Get(".1.3.6.1.2.1.43.11.1.1.9.1.6")
    drumR3 = $SNMP.Get(".1.3.6.1.2.1.43.11.1.1.9.1.7")
    drumR4 = $SNMP.Get(".1.3.6.1.2.1.43.11.1.1.9.1.8")
    wastebox = $SNMP.Get(".1.3.6.1.2.1.43.11.1.1.9.1.10")
    transferbelt = $SNMP.Get(".1.3.6.1.2.1.43.11.1.1.9.1.11")
    phaser = $SNMP.Get(".1.3.6.1.2.1.43.11.1.1.9.1.12")

    Blacktonerstatus = [Math]::round($blacktonerlvl/$blacktonercap*100)
    Cyantonerstatus = [Math]::round($cyantonerlvl/$cyantonercap*100)
    magentatonerstatus = [Math]::round($magentatonerlvl/$magentatonercap*100)
    yellowtonerstatus = [Math]::round($yellowtonerlvl/$yellowtonercap*100)
    }
  1. You get the data directly when you do the calculation but that means that you have to get the SNMP data twice.
$props = @{
    hostname = $SNMP.Get(".1.3.6.1.2.1.1.5.0")
    blacktonercap = $SNMP.Get(".1.3.6.1.2.1.43.11.1.1.8.1.1")
    blacktonerlvl = $SNMP.Get(".1.3.6.1.2.1.43.11.1.1.9.1.1")
    cyantonercap = $SNMP.Get(".1.3.6.1.2.1.43.11.1.1.8.1.2")
    cyantonerlvl = $SNMP.Get(".1.3.6.1.2.1.43.11.1.1.9.1.2")
    magentatonercap = $SNMP.Get(".1.3.6.1.2.1.43.11.1.1.8.1.3")
    magentatonerlvl = $SNMP.Get(".1.3.6.1.2.1.43.11.1.1.9.1.3")
    yellowtonercap = $SNMP.Get(".1.3.6.1.2.1.43.11.1.1.8.1.4")
    yellowtonerlvl = $SNMP.Get(".1.3.6.1.2.1.43.11.1.1.9.1.4")
    drumR1 = $SNMP.Get(".1.3.6.1.2.1.43.11.1.1.9.1.5")
    drumR2 = $SNMP.Get(".1.3.6.1.2.1.43.11.1.1.9.1.6")
    drumR3 = $SNMP.Get(".1.3.6.1.2.1.43.11.1.1.9.1.7")
    drumR4 = $SNMP.Get(".1.3.6.1.2.1.43.11.1.1.9.1.8")
    wastebox = $SNMP.Get(".1.3.6.1.2.1.43.11.1.1.9.1.10")
    transferbelt = $SNMP.Get(".1.3.6.1.2.1.43.11.1.1.9.1.11")
    phaser = $SNMP.Get(".1.3.6.1.2.1.43.11.1.1.9.1.12")

    Blacktonerstatus = [Math]::round(($SNMP.Get(".1.3.6.1.2.1.43.11.1.1.9.1.1"))/($SNMP.Get(".1.3.6.1.2.1.43.11.1.1.8.1.1"))*100)
    Cyantonerstatus = [Math]::round(($SNMP.Get(".1.3.6.1.2.1.43.11.1.1.9.1.2"))/($SNMP.Get(".1.3.6.1.2.1.43.11.1.1.8.1.2"))*100)
    magentatonerstatus = [Math]::round(($SNMP.Get(".1.3.6.1.2.1.43.11.1.1.9.1.3"))/($SNMP.Get(".1.3.6.1.2.1.43.11.1.1.8.1.3"))*100)
    yellowtonerstatus = [Math]::round(($SNMP.Get(".1.3.6.1.2.1.43.11.1.1.9.1.4"))/($SNMP.Get(".1.3.6.1.2.1.43.11.1.1.8.1.4"))*100)
}

Both are not perfect to be honest but should work.

Sign up to request clarification or add additional context in comments.

7 Comments

I edited the script, so you should be able to directly execute it. Can you show me your output?
It does look alot better for sure. However if i structure my script like that the output only contains the latter of the 2 IP-Adresses: drumR1 : 0 drumR2 : 72 yellowtonerlvl : 14250 wastebox : 100 drumR3 : 69 cyantonercap : 15000 yellowtonercap : 15000 magentatonerlvl : 13200 cyantonerlvl : 15000 drumR4 : 98 blacktonerlvl : 9240 transferbelt : 41 blacktonercap : 22000 hostname : Hostname1 magentatonercap : 15000 phaser : 41
Did you read my last comment?
Actually works really well! It didn't work the first time around, the only difference i did was to pipe it to an .txt-file instead of CSV. But this works like a charm. Is there anyway to include the maths in the array output? Or would that be better to do afterwards? If i could include tonerlvl/tonercap*100 everything in the array would be in percent and easier to import in the python GUI.
thank you very much for the answers! I'm still learning, so kudos to you for taking your time and explaining as well, it helps me on my journey! :)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.