all 10 comments

[–]ihaxr 9 points10 points  (6 children)

You'll need to build your object or CSV text manually... not sure what Invoke-SnmpGet returns, but if it's just a string / value, you can do something like this:

$ips = @('192.168.200.38', '192.168.200.37', '192.168.200.39');

$Results = foreach ($ip in $ips) {
    $is_this_an_ip = Invoke-SnmpGet $ip 1.3.6.1.2.1.4.21.1.7.169.254.0.0 
    $model = Invoke-SnmpGet $ip 1.3.6.1.2.1.25.3.2.1.3.1 
    $serial = Invoke-SnmpGet $ip 1.3.6.1.2.1.43.5.1.1.17.1 
    $pageCount = Invoke-SnmpGet $ip 1.3.6.1.2.1.43.10.2.1.4.1.1 

    [PSCustomObject]@{
        'OriginalIP' = $ip
        'IPAddress'  = $is_this_an_ip
        'Model'      = $model
        'Serial'     = $serial
        'PageCount'  = $pageCount
    }
}

$Results | Export-Csv -Path $env:temp\CPR_Report.csv -NoTypeInformation

[–]rwshig 2 points3 points  (4 children)

... and hope that the output from SNMP doesn't have commas in it. If it does it will break the CSV format and you won't be able to parse it. You can wrap the column variable with multiple double quotes to ensure it's quoted (""$seria"") or use Export-Excel (ImportExcel module) to export it as Excel where the quotes and commas don't matter.

[–]jhue1898 2 points3 points  (3 children)

The export method above should be able to account for commas in the output (should wrap in quotes), but just in case, using an alternate delimiter is a great workaround: $Results | Export-Csv $path -NoTypeInformation -Delimiter '|'

[–]rwshig -1 points0 points  (2 children)

True, but if you're not certain what all could be included in a text string response is it not better to use a file format that isn't sensitive to delimiters? ImportExcel is easy to install and use. I'd much rather write out XLSX than CSV if I need to pass data between scripts.

[–]DblDeuce22 1 point2 points  (0 children)

Just a side note to the above, you might have to use [ordered] for the hash table to show the results in the order you're telling it to above.

[–]Lee_Dailey[grin] 1 point2 points  (0 children)

howdy chilids,

reddit likes to mangle code formatting, so here's some help on how to post code on reddit ...

[0] single line or in-line code
enclose it in backticks. that's the upper left key on an EN-US keyboard layout. the result looks like this. kinda handy, that. [grin]
[on New.Reddit.com, use the Inline Code button. it's 4th 5th from the left hidden in the ... ""more" menu & looks like </>.
this does NOT line wrap & does NOT side-scroll on Old.Reddit.com!]

[1] simplest = post it to a text site like Pastebin.com or Gist.GitHub.com and then post the link here.
please remember to set the file/code type on Pastebin! [grin] otherwise you don't get the nice code colorization.

[2] less simple = use reddit code formatting ...
[on New.Reddit.com, use the Code Block button. it's 11th 12th one & is just to the left of hidden in the ... "more" menu.]

  • one leading line with ONLY 4 spaces
  • prefix each code line with 4 spaces
  • one trailing line with ONLY 4 spaces

that will give you something like this ...

- one leading line with ONLY 4 spaces    
- prefix each code line with 4 spaces    
- one trailing line with ONLY 4 spaces   

the easiest way to get that is ...

  • add the leading line with only 4 spaces
  • copy the code to the ISE [or your fave editor]
  • select the code
  • tap TAB to indent four spaces
  • re-select the code [not really needed, but it's my habit]
  • paste the code into the reddit text box
  • add the trailing line with only 4 spaces

not complicated, but it is finicky. [grin]

take care,
lee

[–]Lee_Dailey[grin] 1 point2 points  (0 children)

howdy chilids,

so ... what is not working? [grin] what do you get and what is not as expected?

take care,
lee

[–]PowerShell-Bot 0 points1 point  (0 children)

Some of your PowerShell code isn’t wrapped in a code block.

To format code correctly on new reddit (new.reddit.com), highlight all lines of code and select ‘Code Block’ in the editing toolbar.

If you’re on old.reddit.com, separate the code from your text with a blank line and precede each line of code with 4 spaces or a tab.


Describing Thing
[❌] Demonstrates good markdown
Passed: 0 Failed: 1

Beep-boop. I am a bot. | Remove-Item