Home > Net >  Powershell Foreach Write-Output
Powershell Foreach Write-Output

Time:06-01

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%

CodePudding user response:

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.

  • Related