op_Subtraction

Hello,

What does this mean? It works sometimes, but then sometimes it gives me this message.

This is line 175,

$Line_Num = ($Read_INI | Select-String -AllMatches “^$Items$”).LineNumber - 1

Method invocation failed because [System.Object] does not contain a method named
‘op_Subtraction’.
At \hqfs1\Users\tantony\PowerShell\CalenderGroup\Calender.ps1:175 char:9

  •     $Line_Num = ($Read_INI | Select-String -AllMatches "^$Items$" ...
    
  •     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    • CategoryInfo : InvalidOperation: (op_Subtraction:String) , RuntimeException
    • FullyQualifiedErrorId : MethodNotFound

($Read_INI | Select-String -AllMatches “^$Items$”).LineNumber is most likely not a number, probably a string/array, which does not support numeric subtraction. You’ll need to force it to a number first. You’ll have to determine what type of object is being returned first to do that.

ok, thank you

I’ll do a write-host to see if it’s a number.

Tony

I remember reading one of your other posts here relating to select-string and your issue was due to select-string returning multiple results. Here is the same problem. You’re getting more than one match from select-string, returning a collection of linenumber properties (in your code) rather than just one, and you can’t perform arithmetic on a collection as such, hence the error.

Yes, this is the problem. In this example, I have more than one 4=697. How would I fix this? How would I tell powershell to grab the line numbers even if the string matches more than one time?

[MembersOf-4]
1=19
2=21
3=20
4=697
[MembersOf-5]
1=22
2=23
3=697
[MembersOf-6]
1=26
2=25
3=24
4=697

($Read_INI | Select-String -AllMatches "^$Items$").LineNumber | %{DoSomething $_}

What does the % do after the second pipe?

Sorry, I’m still not sure how to do this

$Line_Num = ($Read_INI | Select-String -AllMatches "^$Items$").LineNumber - 1     
$Look_Up = $Read_INI[0..$Line_Num]  
$test = $Line_Num | % $Look_Up
, and still gives the same result.

% is an alias for Foreach-Object.

($Read_INI | Select-String -AllMatches "^$Items$").LineNumber - 1

This is not going to work. You are try to subtract 1 from an array object.

Maybe this will work.

($Read_INI | Select-String -AllMatches "^$Items$") | Foreach-Object {$Read_INI[$_.LineNumber - 1]}

I’m still getting the same result, i’ll see if I can find a work around. Thank you. Unfortunately, I’m the only one in my department that does powershell.

A ton of assumptions here, but if it’s a simple “remove all strings matching these items”…

$Items | %{ $Read_INI = $ReadINI -notmatch "^$_$" }

EDIT:
If you just want the line numbers (well, element number in this case):

$Items | %{ IF ($Read_INI -match "^$_$") {$Read_INI.IndexOf($_)} }
# Or another method... 
$Items | Where {$Read_INI -match "^$_$"} | %{ $Read_INI.IndexOf($_) }

Just remember, Arrays start at Zero, not One…