Get Exclusive value

Hello everybody,
I have a script that collects a series of information and now I came across unexpected behavior. The data is in a list in the following format:

1/1/0
1/1/1
1/1/2
1/1/3
.
.
.
1/1/10
1/1/11

My problem is that I always need to obtain the absolute value based on an entry defined by the user, for example: 1/1/1
the problem is that in this case, my loop selects the values ​​that contain this data, example: 1/1/10, 1/1/11, 1/1/12 …
Would anyone have an idea how to get around this?

How are you selecting the values? If you use regex you can use the end of line anchor

$value -match ‘1/1/1$’

Hello, thank for your reply!

Here is the how I am selecting:

$lookupTable | Where-Object { $_.PortName -match [REGEX]::escape($userInput) }

Could you please format your code as code here in the forum. There are charachters missing in your code.

When you post code, error messages, sample data or console output format it as code, please.

Here you can read how that works: Guide to Posting Code.

You can go back and edit your existing post. You don’t have to create a new one. :wink:

Thanks in advance.

$lookupTable = 0..25 | foreach {"1/1/{0}" -f $_} 
$userInput = Read-Host 'Select a port'
$lookupTable | Where{$_ -like "*/$userInput"}

Output:

Select a port: 22
1/1/22

Thanks!! Here is my code to get data:

 $table = @($getportname)

$lookupTable = ($table) |
ForEach-Object {
    [PSCustomObject]@{
        PortNumber  =  $_.Substring(24,10)
        PortName    =  $_ -replace '.*\"(.*?)\"\s*$','$1'
    }
}

$teste = for ($i=1;$i -le 1;$i++) {
    $userInput = $port
   
    $lookupTable | Where-Object { $_.PortName -match [REGEX]::escape($userInput) }




} 

In $userinput have the list with all values in that format: 
0/0/1
1/1/1
1/2/1
 And many other. Have a Variable that we input what data we can get, ex: 1/1/1. If at the list have the value like 1/1/10 or 1/1/11 both appear in the result.

If you they are inputting 1/1/1 and you want to find a record for 1/1/1, then why not simply use the equal operator (-eq). Regex is going to find matches versus a specific record

$lookupTable | Where-Object { $_.PortName -eq  $userInput }

Thanks Mr. Rob, but I don’t know why do not work with -eq or -ceq or -like… only works with -match or -cmatch.

Can you show a couple rows of $lookupTable?

Here:

.1.3.6.1.2.1.4194312192 = STRING:0/1/0
.1.3.6.1.2.1.4194312448 = STRING:0/1/1
.1.3.6.1.2.1.4194312704 = STRING:0/1/2
.1.3.6.1.2.1.4194312960 = STRING:0/1/3
.1.3.6.1.2.1.4194313216 = STRING:0/1/4
.1.3.6.1.2.1.4194313472 = STRING:0/1/5
.1.3.6.1.2.1.4194313728 = STRING:0/1/6
.1.3.6.1.2.1.4194313984 = STRING:0/1/7
.1.3.6.1.2.1.4194314240 = STRING:0/1/8
.1.3.6.1.2.1.4194314496 = STRING:0/1/9
.1.3.6.1.2.1.4194314752 = STRING:0/1/10
.1.3.6.1.2.1.4194315008 = STRING:0/1/11
.1.3.6.1.2.1.4194315264 = STRING:0/1/12
.1.3.6.1.2.1.4194315520 = STRING:0/1/13
.1.3.6.1.2.1.4194315776 = STRING:0/1/14
.1.3.6.1.2.1.4194316032 = STRING:0/1/15
.1.3.6.1.2.1.4194320384 = STRING:0/2/0
.1.3.6.1.2.1.4194320640 = STRING:0/2/1
.1.3.6.1.2.1.4194320896 = STRING:0/2/2
.1.3.6.1.2.1.4194321152 = STRING:0/2/3
.1.3.6.1.2.1.4194321408 = STRING:0/2/4
.1.3.6.1.2.1.4194321664 = STRING:0/2/5
.1.3.6.1.2.1.4194321920 = STRING:0/2/6
.1.3.6.1.2.1.4194322176 = STRING:0/2/7
.1.3.6.1.2.1.4194322432 = STRING:0/2/8
.1.3.6.1.2.1.4194322688 = STRING:0/2/9
.1.3.6.1.2.1.4194322944 = STRING:0/2/10
.1.3.6.1.2.1.4194323200 = STRING:0/2/11
.1.3.6.1.2.1.4194323456 = STRING:0/2/12
.1.3.6.1.2.1.4194323712 = STRING:0/2/13
.1.3.6.1.2.1.4194323968 = STRING:0/2/14
.1.3.6.1.2.1.4194324224 = STRING:0/2/15 

Using your parse of what I assume is your provided in your $table variable, this is what it looks like:

PortNumber PortName
---------- --------
= STRING:0 .1.3.6.1.2.1.4194312192 = STRING:0/1/0
= STRING:0 .1.3.6.1.2.1.4194312448 = STRING:0/1/1
= STRING:0 .1.3.6.1.2.1.4194312704 = STRING:0/1/2
= STRING:0 .1.3.6.1.2.1.4194312960 = STRING:0/1/3

My assumption is you are trying to do something like this:

$lookupTable = ($table) |
ForEach-Object {
    [PSCustomObject]@{
        PortNumber  =  ($_ -split ' = ')[1] -replace "STRING:"
        PortName    =  ($_ -split ' = ')[0]
    }
}
PS C:\Users\rasim> $lookupTable


PortNumber PortName
---------- --------
0/1/0      .1.3.6.1.2.1.4194312192
0/1/1      .1.3.6.1.2.1.4194312448
0/1/2      .1.3.6.1.2.1.4194312704
0/1/3      .1.3.6.1.2.1.4194312960
0/1/4      .1.3.6.1.2.1.4194313216
0/1/5      .1.3.6.1.2.1.4194313472
0/1/6      .1.3.6.1.2.1.4194313728
0/1/7      .1.3.6.1.2.1.4194313984
0/1/8      .1.3.6.1.2.1.4194314240
0/1/9      .1.3.6.1.2.1.4194314496
0/1/10     .1.3.6.1.2.1.4194314752
0/1/11     .1.3.6.1.2.1.4194315008
0/1/12     .1.3.6.1.2.1.4194315264
0/1/13     .1.3.6.1.2.1.4194315520
0/1/14     .1.3.6.1.2.1.4194315776
0/1/15     .1.3.6.1.2.1.4194316032
0/2/0      .1.3.6.1.2.1.4194320384
0/2/1      .1.3.6.1.2.1.4194320640
0/2/2      .1.3.6.1.2.1.4194320896
0/2/3      .1.3.6.1.2.1.4194321152
0/2/4      .1.3.6.1.2.1.4194321408
0/2/5      .1.3.6.1.2.1.4194321664
0/2/6      .1.3.6.1.2.1.4194321920
0/2/7      .1.3.6.1.2.1.4194322176
0/2/8      .1.3.6.1.2.1.4194322432
0/2/9      .1.3.6.1.2.1.4194322688
0/2/10     .1.3.6.1.2.1.4194322944
0/2/11     .1.3.6.1.2.1.4194323200
0/2/12     .1.3.6.1.2.1.4194323456
0/2/13     .1.3.6.1.2.1.4194323712
0/2/14     .1.3.6.1.2.1.4194323968
0/2/15     .1.3.6.1.2.1.4194324224

PS C:\Users\rasim> $lookupTable | Where {$_.PortNumber -eq '0/1/7'}

PortNumber PortName
---------- --------
0/1/7      .1.3.6.1.2.1.4194313984

PS C:\Users\rasim>

Thanks Mr. Rob!

It is almost!! :slight_smile:

The PortNumber is the data .1.3.6.1.2.1.4194324224 and the PortName is 0/1/7. Based PortName the script return the PortNumber. The problem is if I put in the search a value that begin with other, the script return all data. EX:

0/1/0      .1.3.6.1.2.1.4194312192
0/1/1      .1.3.6.1.2.1.4194312448
0/1/2      .1.3.6.1.2.1.4194312704
0/1/3      .1.3.6.1.2.1.4194312960
0/1/4      .1.3.6.1.2.1.4194313216
0/1/5      .1.3.6.1.2.1.4194313472
0/1/6      .1.3.6.1.2.1.4194313728
0/1/7      .1.3.6.1.2.1.4194313984
0/1/8      .1.3.6.1.2.1.4194314240
0/1/9      .1.3.6.1.2.1.4194314496
0/1/10     .1.3.6.1.2.1.4194314752
0/1/11     .1.3.6.1.2.1.4194315008
0/1/12     .1.3.6.1.2.1.4194315264
0/1/13     .1.3.6.1.2.1.4194315520
0/1/14     .1.3.6.1.2.1.4194315776
0/1/15     .1.3.6.1.2.1.4194316032
0/2/0      .1.3.6.1.2.1.4194320384
0/2/1      .1.3.6.1.2.1.4194320640
0/2/2      .1.3.6.1.2.1.4194320896
0/2/3      .1.3.6.1.2.1.4194321152
0/2/4      .1.3.6.1.2.1.4194321408
0/2/5      .1.3.6.1.2.1.4194321664
0/2/6      .1.3.6.1.2.1.4194321920
0/2/7      .1.3.6.1.2.1.4194322176
0/2/8      .1.3.6.1.2.1.4194322432
0/2/9      .1.3.6.1.2.1.4194322688
0/2/10     .1.3.6.1.2.1.4194322944
0/2/11     .1.3.6.1.2.1.4194323200
0/2/12     .1.3.6.1.2.1.4194323456
0/2/13     .1.3.6.1.2.1.4194323712
0/2/14     .1.3.6.1.2.1.4194323968
0/2/15     .1.3.6.1.2.1.4194324224

If getting 0/2/1 is returned:

0/2/1      .1.3.6.1.2.1.4194320640
0/2/10     .1.3.6.1.2.1.4194322944
0/2/11     .1.3.6.1.2.1.4194323200
0/2/12     .1.3.6.1.2.1.4194323456
0/2/13     .1.3.6.1.2.1.4194323712
0/2/14     .1.3.6.1.2.1.4194323968
0/2/15     .1.3.6.1.2.1.4194324224

Again, you should be able to use -eq…

$table = @"
.1.3.6.1.2.1.4194312192 = STRING:0/1/0
.1.3.6.1.2.1.4194312448 = STRING:0/1/1
.1.3.6.1.2.1.4194312704 = STRING:0/1/2
.1.3.6.1.2.1.4194312960 = STRING:0/1/3
.1.3.6.1.2.1.4194313216 = STRING:0/1/4
.1.3.6.1.2.1.4194313472 = STRING:0/1/5
.1.3.6.1.2.1.4194313728 = STRING:0/1/6
.1.3.6.1.2.1.4194313984 = STRING:0/1/7
.1.3.6.1.2.1.4194314240 = STRING:0/1/8
.1.3.6.1.2.1.4194314496 = STRING:0/1/9
.1.3.6.1.2.1.4194314752 = STRING:0/1/10
.1.3.6.1.2.1.4194315008 = STRING:0/1/11
.1.3.6.1.2.1.4194315264 = STRING:0/1/12
.1.3.6.1.2.1.4194315520 = STRING:0/1/13
.1.3.6.1.2.1.4194315776 = STRING:0/1/14
.1.3.6.1.2.1.4194316032 = STRING:0/1/15
.1.3.6.1.2.1.4194320384 = STRING:0/2/0
.1.3.6.1.2.1.4194320640 = STRING:0/2/1
.1.3.6.1.2.1.4194320896 = STRING:0/2/2
.1.3.6.1.2.1.4194321152 = STRING:0/2/3
.1.3.6.1.2.1.4194321408 = STRING:0/2/4
.1.3.6.1.2.1.4194321664 = STRING:0/2/5
.1.3.6.1.2.1.4194321920 = STRING:0/2/6
.1.3.6.1.2.1.4194322176 = STRING:0/2/7
.1.3.6.1.2.1.4194322432 = STRING:0/2/8
.1.3.6.1.2.1.4194322688 = STRING:0/2/9
.1.3.6.1.2.1.4194322944 = STRING:0/2/10
.1.3.6.1.2.1.4194323200 = STRING:0/2/11
.1.3.6.1.2.1.4194323456 = STRING:0/2/12
.1.3.6.1.2.1.4194323712 = STRING:0/2/13
.1.3.6.1.2.1.4194323968 = STRING:0/2/14
.1.3.6.1.2.1.4194324224 = STRING:0/2/15
"@ -split [environment]::NewLine

$lookupTable = ($table) |
ForEach-Object {
    [PSCustomObject]@{
        PortName  =  ($_ -split ' = ')[1] -replace "STRING:"
        PortNumber    =  ($_ -split ' = ')[0]
    }
}

$rh = Read-Host "Enter port name"

$lookupTable | Where {$_.PortName -eq $rh}

Output:

Enter port name: 0/2/1

PortName PortNumber
-------- ----------
0/2/1    .1.3.6.1.2.1.4194320640

Hello, I don’t know why, but with -eq it doesn’t work. If I use -match or -cmatch, the value will be returned.

Clarifying a little more about the code: I do an SNMPWalk on a device and all the data I need is placed in a table. When I insert the slot I need, the script searches the table and returns a number, which is named in a PortNumber variable.

Again, thank you very much for your help!

[RESOLVED]

The problem was a space character and so the -eq didn’t work. After the adjustment, the script worked fine. Thank you very much for all your help!