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) }
Olaf
April 5, 2021, 3:59pm
4
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.
Discourse uses Markdown for text formatting. There is a reference sheet and quick tutorial here: Markdown Reference
You can produce in-line code formatting by wrapping your code in backticks (`). For example, `Get-ChildItem` produces Get-ChildItem.
For larger code blocks, place a line containing three backticks above and below your code. For example
```
# 256-Color Foreground & Background Charts
echo “`n$esc[1;4m256-Color Foreground & Background Charts$esc[0m”
foreach ($fgbg in 38,48) { #…
You can go back and edit your existing post. You don’t have to create a new one.
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!!
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!
Hi people, here we again :-p
Same problem, but now, with the strings…
The Array (:
hundred-gigabit-ethernet-1/1/1
hundred-gigabit-ethernet-1/1/10
hundred-gigabit-ethernet-1/1/11
hundred-gigabit-ethernet-1/1/12
hundred-gigabit-ethernet-1/1/13
hundred-gigabit-ethernet-1/1/14
The Userinput
hundred-gigabit-ethernet-1/1/1
The code:
$teste = for ($i=1;$i -le 1;$i++) {
$userInput = $port
$lookupTable | Where-Object { $_.PortName -match [REGEX]::escape($userInput) }
}
The regex with match brings the entire array that contains Hundred-gigabit-ethernet-1/1/1, that is, the entire informed array.
The -EQ operator does not work with strings.
Does anyone have any ideas for a workaround for this?
You’ve not provided a good example of your data so I’ve just used an array and simplified your code.
If you don’t use the Escape()
method, you can append $
to show that that’s the end of the string that should be matched:
$lookupTable = @(
'hundred-gigabit-ethernet-1/1/1',
'hundred-gigabit-ethernet-1/1/10',
'hundred-gigabit-ethernet-1/1/11',
'hundred-gigabit-ethernet-1/1/12',
'hundred-gigabit-ethernet-1/1/13',
'hundred-gigabit-ethernet-1/1/14'
)
$port = 'hundred-gigabit-ethernet-1/1/1'
$lookupTable | Where-Object { $_ -match "$($port)$" }
The sub-expression $($port)
isn’t strictly necessary - { $_ -match "$port$" }
would work - but I think using the sub-expression makes it easier to read.
1 Like
Hi Mr. matt-bloomfield ,
I have change the code as your suggestion and appear to working. I am doing some tests and after that, I reply if works OK.
Thanks a lot