Author Posts

November 13, 2017 at 10:23 pm

Hi people,

I would like to know if have some way to use the substrings for to get a dynamically text: I have the below result:

$strcli = 1.3.6.1.4.1.3709.3.6.2.1.1.5.16777216 = STRING: "NAME012345678"

I use the follow:

$strcli = $strcli.Substring(50,12)

Result:

$strcli

NAME012345678

That works fine, but if the strings hav a name with less characters, the substring does not work.

Someone have an idea?

November 13, 2017 at 10:35 pm

Substring are static looks. If you want dynamic, look to using RegEx string match.

See details here:

PowerShell: Working With Regular Expressions (regex)
'social.technet.microsoft.com/wiki/contents/articles/4310.powershell-working-with-regular-expressions-regex.aspx'

Powershell regular expressions
'powershelladmin.com/wiki/Powershell_regular_expressions'

PowerShell regex crash course – Part 2 of 5
'blogs.technet.microsoft.com/heyscriptingguy/2016/10/07/powershell-regex-crash-course-part-2-of-5'

PowerShell Problem Solver: PowerShell String Parsing with Regular Expressions
'petri.com/powershell-string-parsing-with-regular-expressions'

November 13, 2017 at 10:37 pm

Strings are hard, because they aren't ALWAYS in the same format, BUT if they are, you can do some things. If you're final string is always inside double quotes, the below works.

$strcli = 1.3.6.1.4.1.3709.3.6.2.1.1.5.16777216 = STRING: "NAME012345678"
$substring = $strcli.split("'")[1]

If it doesn't always have double quotes, but does have ":", then you could do something like,

$strcli.split(':')[-1].trim().trim('"')

November 14, 2017 at 10:11 am

You may get away with

PS> $strcli.split('G: "')[6]
NAME012345678

November 14, 2017 at 11:17 am

Hi,

Thank you for all answer. Let me explain a more about the script. My need is get some information for an equipment with SNMPWalk. When I run the command (walk), the result is an array. See below:
.1.3.6.1.4.1.3709.3.6.2.1.1.5.16777216 = STRING: "54321C013217A"
.1.3.6.1.4.1.3709.3.6.2.1.1.5.16777217 = STRING: "12345F100417A"
.1.3.6.1.4.1.3709.3.6.2.1.1.5.16777472 = STRING: "54321C012817A"
.1.3.6.1.4.1.3709.3.6.2.1.1.5.16777473 = STRING: "12345F100317A"
.1.3.6.1.4.1.3709.3.6.2.1.1.5.16777728 = STRING: "12345F100017A"
.1.3.6.1.4.1.3709.3.6.2.1.1.5.16777729 = STRING: "12345F100617A"
.1.3.6.1.4.1.3709.3.6.2.1.1.5.16777730 = STRING: "12345F1TESTE2"
.1.3.6.1.4.1.3709.3.6.2.1.1.5.16777985 = STRING: "12345F100717A"
.1.3.6.1.4.1.3709.3.6.2.1.1.5.16777986 = STRING: "12345F10TESTE"
.1.3.6.1.4.1.3709.3.6.2.1.1.5.16778241 = STRING: "12345F100817A"
.1.3.6.1.4.1.3709.3.6.2.1.1.5.16778497 = STRING: "12345F100917A"
.1.3.6.1.4.1.3709.3.6.2.1.1.5.16778753 = STRING: "12345F101017A"
.1.3.6.1.4.1.3709.3.6.2.1.1.5.16779009 = STRING: "12345F101117A"
.1.3.6.1.4.1.3709.3.6.2.1.1.5.16779265 = STRING: "12345F101217A"
.1.3.6.1.4.1.3709.3.6.2.1.1.5.16779521 = STRING: "12345F101317A"
.1.3.6.1.4.1.3709.3.6.2.1.1.5.16779777 = STRING: "12345F102117A"
.1.3.6.1.4.1.3709.3.6.2.1.1.5.16780033 = STRING: "12345F102417A"

I would like to get only value between "".

The split almost work, but it get only first line.

TKS,

rc.

November 14, 2017 at 12:32 pm

try this:

$string = '.1.3.6.1.4.1.3709.3.6.2.1.1.5.16780033 = STRING: "12345F102417A"'
$pattern = '^.*"(?(.*))"$'
if ($string -match $pattern) {
   Write-Host $matches.name
}

November 14, 2017 at 12:37 pm

I was unable to edit the above reply, but try this:

$string = '.1.3.6.1.4.1.3709.3.6.2.1.1.5.16780033 = STRING: "12345F102417A"'
$pattern = '^.*"(?(.*))"$'   #Your editor is removing the less than name greater than part of this which should be directly after the ?.
if ($string -match $pattern) {
   Write-Host $matches.name
}

Due to a quirk in your HTML editor I cannot write the above correctly. This is what goes directly after the ? LESS THAN name GREATER THAN (no spaces).

November 16, 2017 at 2:48 pm

Thank you guys for the all answers.

I don't have solved that issue yet. The most problem is that the result (string between "") has the various size. Some have 8 characters, and some other have plus or minus.

thanks again.

rc.

November 16, 2017 at 3:32 pm

That's pretty easy, assuming your data come as separate lines (that is why I am doing the -split on it), all you need to do is replace everything that is not in the last quotes:

$s = @'
.1.3.6.1.4.1.3709.3.6.2.1.1.5.16777216 = STRING: "54321C013217A"
.1.3.6.1.4.1.3709.3.6.2.1.1.5.16777217 = STRING: "12345F100417A"
.1.3.6.1.4.1.3709.3.6.2.1.1.5.16777472 = STRING: "54321C012817A"
.1.3.6.1.4.1.3709.3.6.2.1.1.5.16777473 = STRING: "12345F100317A"
.1.3.6.1.4.1.3709.3.6.2.1.1.5.16777728 = STRING: "12345F100017A"
.1.3.6.1.4.1.3709.3.6.2.1.1.5.16777729 = STRING: "12345F100617A"
.1.3.6.1.4.1.3709.3.6.2.1.1.5.16777730 = STRING: "12345F1TESTE2"
.1.3.6.1.4.1.3709.3.6.2.1.1.5.16777985 = STRING: "12345F100717A"
.1.3.6.1.4.1.3709.3.6.2.1.1.5.16777986 = STRING: "12345dF10TESTE"
.1.3.6.1.4.1.3709.3.6.2.1.1.5.16778241 = STRING: "12345F100817A"
.1.3.6.1.4.1.3709.3.6.2.1.1.5.16778497 = STRING: "12345Fdd100917A"
.1.3.6.1.4.1.3709.3.6.2.1.1.5.16778753 = STRING: "12345F101017A"
.1.3.6.1.4.1.3709.3.6.2.1.1.5.16779009 = STRING: "12345Fdd101117A"
.1.3.6.1.4.1.3709.3.6.2.1.1.5.16779265 = STRING: "12345F101217A"
.1.3.6.1.4.1.3709.3.6.2.1.1.5.16779521 = STRING: "12345F101317A"
.1.3.6.1.4.1.3709.3.6.2.1.1.5.16779777 = STRING: "12345F102117A"
.1.3.6.1.4.1.3709.3.6.2.1.1.5.16780033 = STRING: "12345F102417A"
'@ -split "`n" #< - making it behave like the data would come from Get-Content

$s -replace '.*\"(.*?)\"\s*$','$1'

November 16, 2017 at 7:51 pm

I put your example in a text file. This will output all strings between "".

$file = Get-ChildItem .\string.txt

switch -Regex -File $file {
'"(.*)"'{$Matches[1]}
}

February 19, 2018 at 5:05 pm

Hi, thanks for all answers. I can not use a external file because is it a snmp walk.

Using split('"') it separate correctly, but I can't put the result into array.

$target = @($ini)
$ini=0 |
foreach {
    
    $strcli = (snmpwalk.exe -Ln -On -v 2c -c public $hostip ".1.3.6.1.4.1.3709.3.6.2.1.1.5")
    

    $target.Count
    ##Remove String Chars from OID###
    $target = $strcli.split('"',3)[1] ---- When put this, appear only first row from table
    $strcli = $target
}

Result without [1]


.1.3.6.1.4.1.3709.3.6.2.1.1.5.16777472 = STRING: 
021462

.1.3.6.1.4.1.3709.3.6.2.1.1.5.16777728 = STRING: 
033102

.1.3.6.1.4.1.3709.3.6.2.1.1.5.16777984 = STRING: 
021572

.1.3.6.1.4.1.3709.3.6.2.1.1.5.16778240 = STRING: 
033907

Thank you for help.

rc.

May 25, 2018 at 5:50 pm

Hi,

I am here again with some issue about string into powershell. Now I need get only value after a specific string, like below:


.1.3.6.1.2.1.15.3.1.2.1.1.1.1 = INTEGER: 6

I put the result in a variable and I try to use the -replace


$result = .1.3.6.1.2.1.15.3.1.2.1.1.1.1 = INTEGER: 6
$result = $result -replace ("(:\s([\d])")

But I cannot remove the whitespace in front of 6.

Thanks for you help.

rc.

May 26, 2018 at 2:27 am

If ('.1.3.6.1.2.1.15.3.1.2.1.1.1.1 = INTEGER: 6' -match "INTEGER: (.*)") {
    $result = $matches[1]
}

May 26, 2018 at 2:41 am

Thank you for your help Curtis,

Maybe I was not cleared about the case. I need to get the number after INTEGER:

Thank you for your time and sorry for the my mistake.

rc.

May 26, 2018 at 1:08 pm

Hey Rodrigo,
Did you try it? That's exactly what it does. After running this, $result contains the number after INTEGER: with no space

May 26, 2018 at 2:08 pm

There is a simple snmp powershell module here. It doesn't resolve hostnames, but for simple strings it should be straighforward. https://www.powershellgallery.com/packages/SNMP/1.0.0.1

On a tangent, I spent a week wrestling with interpreting bits in octetstrings for printer status. See the bottom.
https://stackoverflow.com/questions/27054622/how-to-read-snmp-oid-output

May 28, 2018 at 5:46 pm

It looks like what you really want to do is use regex to parse the strings and make PowerShell objects out of them so you can then work with the data in a more powershell way to filter or grab the data you want.

IE.

$data = @'
.1.3.6.1.4.1.3709.3.6.2.1.1.5.16777216 = STRING: "54321C013217A"
.1.3.6.1.2.1.15.3.1.2.1.1.1.1 = INTEGER: 6
.1.3.6.1.4.1.3709.3.6.2.1.1.5.16777217 = STRING: "12345F100417A"
.1.3.6.1.4.1.3709.3.6.2.1.1.5.16777472 = STRING: "54321C012817A"
'@ -split "`n"

$data |
Select-String -Pattern '^(.*?) = (.*?): (.*?)$' |
Select-Object -ExpandProperty Matches |
ForEach-Object {
    [pscustomobject]@{
        oid = $_.Groups[1].value
        type = $_.Groups[2].value
        value = $_.Groups[3].value.Trim('"')
    }
}

Results

oid                                    type      value          
---                                    --------- -----          
.1.3.6.1.4.1.3709.3.6.2.1.1.5.16777216 STRING    54321C013217A
.1.3.6.1.2.1.15.3.1.2.1.1.1.1          INTEGER   6              
.1.3.6.1.4.1.3709.3.6.2.1.1.5.16777217 STRING    12345F100417A
.1.3.6.1.4.1.3709.3.6.2.1.1.5.16777472 STRING    54321C012817A