op_Subtraction

This topic contains 10 replies, has 4 voices, and was last updated by Profile photo of Steve Steve 4 months ago.

  • Author
    Posts
  • #62245
    Profile photo of Tony Antony
    Tony Antony
    Participant

    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

  • #62257
    Profile photo of Ron
    Ron
    Participant

    ($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.

  • #62274
    Profile photo of Tony Antony
    Tony Antony
    Participant

    ok, thank you

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

    Tony

  • #62385
    Profile photo of Peter Jurgens
    Peter Jurgens
    Participant

    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.

    • #62451
      Profile photo of Tony Antony
      Tony Antony
      Participant

      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

    • #62454
      Profile photo of Ron
      Ron
      Participant
      ($Read_INI | Select-String -AllMatches "^$Items$").LineNumber | %{DoSomething $_}
  • #62460
    Profile photo of Tony Antony
    Tony Antony
    Participant

    What does the % do after the second pipe?

  • #62463
    Profile photo of Tony Antony
    Tony Antony
    Participant

    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.

  • #62469
    Profile photo of Ron
    Ron
    Participant

    % 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]}
  • #62472
    Profile photo of Tony Antony
    Tony Antony
    Participant

    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.

  • #62509
    Profile photo of Steve
    Steve
    Participant

    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....

You must be logged in to reply to this topic.