Method invocation failed…doesn’t contain a method named ‘op_Addition’

Welcome Forums General PowerShell Q&A Method invocation failed…doesn’t contain a method named ‘op_Addition’

Viewing 12 reply threads
  • Author
    Posts
    • #261476
      Participant
      Topics: 1
      Replies: 5
      Points: 23
      Rank: Member

      Hi Folks,

      I hope all is well. I have a .ps1 script that runs successfully. It uploads two files to an sftp site. I need to add another file that goes to a different directory in the same sftp site. I reviewed the code and “followed suit” by adding the vars and commands I thought that would be needed to include the third file transfer.

      I get the following error when I run it:

      Error: Method invocation failed because [WinSCP.TransferOperationResult] does not contain a method named ‘op_Addition’.

      In my code below I made a comment above each new entry. You will see a “#”

      Any help is appreciated.

      • This topic was modified 2 weeks, 5 days ago by kvprasoon. Reason: code formatting: see https://powershell.org/forums/topic/guide-to-posting-code-2/
    • #261494
      Senior Moderator
      Topics: 9
      Replies: 1373
      Points: 5,061
      Helping Hand
      Rank: Community MVP

      Not all objects support addition, so you will have to explicitly type cast it as array of [WinSCP.TransferOperationResult]

    • #261506
      Participant
      Topics: 1
      Replies: 5
      Points: 23
      Rank: Member

      Not all objects support addition, so you will have to explicitly type cast it as array of [WinSCP.TransferOperationResult]

      Thank you @kvprasoon for formatting it to look like code. I couldn’t figure out how to do it. ๐Ÿ™‚

      Where do I declare the variable as an array? I tried to after line 39 and I got the same error.

      As you can see, i’m not familiar w/ this.

    • #261533
      Senior Moderator
      Topics: 9
      Replies: 1373
      Points: 5,061
      Helping Hand
      Rank: Community MVP

      You have to declare variables before using it. If in a loop, it should be above the loop. Here, it can be anywhere above $transferOptions = New-Object WinSCP.TransferOptions

      For code formatting, there is a detailed instruction https://powershell.org/forums/topic/guide-to-posting-code-2/

      • This reply was modified 2 weeks, 5 days ago by kvprasoon. Reason: post correction
    • #261542
      Participant
      Topics: 1
      Replies: 5
      Points: 23
      Rank: Member

      You have to declare variables before using it. If in a loop, it should be above the loop. Here, it can be anywhere above $transferOptions = New-Object WinSCP.TransferOptions

      For code formatting, there is a detailed instruction https://powershell.org/forums/topic/guide-to-posting-code-2/

      I did what you recommended (see below) and I still get the same error. What else needs to be done?

      • This reply was modified 2 weeks, 5 days ago by r dawson.
      • This reply was modified 2 weeks, 5 days ago by r dawson.
    • #261554
      Senior Moderator
      Topics: 9
      Replies: 1373
      Points: 5,061
      Helping Hand
      Rank: Community MVP

      Ahh, my apologies, I mentioned the wrong variable. Here it should be $transferResult.

      You should always add element to array once defined, so it will be like below.

    • #261566
      Participant
      Topics: 1
      Replies: 5
      Points: 23
      Rank: Member

      2
      $transferResult = @()
      $transferOptions = New-Object WinSCP.TransferOptions
      $transferOptions.TransferMode = [WinSCP.TransferMode]::Binary
      $transferResult += $session.PutFiles($file1, “/Budget/”, $False, $transferOptions)
      $transferResult += $session.PutFiles($file2, “/Actuals/”, $False, $transferOptions)
      $transferResult += $session.PutFiles($fileOMS, “/OMS_KPI/”, $False, $transferOptions)

      Wonderful! Thank you @kvprasoon. Can you provide any resource as to how this worked? (So I can learn from it)
      thank you again for your patience and assistance. ๐Ÿ™‚

    • #261578
      Senior Moderator
      Topics: 9
      Replies: 1373
      Points: 5,061
      Helping Hand
      Rank: Community MVP

      👍.

      Arrays are basically collections and + operator is used to add elements to a collection. An undefined variable in PowerShell gets its type when its getting set for the first time. And the + operator is type depended, if the type of the variable is a string, then + does the append operation. If the variable is an integer, then + will do addition. Here the type WinSCP.TransferOperationResult is not a string and was trying to add it expecting it to be a collection, but not.

      But in PowerShell arrays are heterogenous, means we can add any type of data in to a single array. Here its of the same WinSCP.TransferOperationResult type, since variable is defined as an array, here it adds value with WinSCP.TransferOperationResult type in all the three lines.

    • #261590
      js
      Participant
      Topics: 32
      Replies: 848
      Points: 2,731
      Helping Hand
      Rank: Community Hero

      += kills puppies.

    • #261746
      Participant
      Topics: 1
      Replies: 5
      Points: 23
      Rank: Member

      += kills puppies.

      ๐Ÿ˜€
      Do you think there would be a more elegant way to code something like this? i’m not too familiar w/ powershell fundamentals yet. i’ll get there some day.

      @kvprasoon, thanks for your information. i’ll copy pasta into my powershell notes.

    • #261770
      Senior Moderator
      Topics: 9
      Replies: 1373
      Points: 5,061
      Helping Hand
      Rank: Community MVP

      Arrays in PowerShell are of fixed in size. So doing += will actually create a new array with newly added element. hence its expensive with performance.

      If working with large collections, its better to go with arraylist and use Add() method.

    • #262688
      Participant
      Topics: 16
      Replies: 1794
      Points: 3,300
      Helping Hand
      Rank: Community Hero

      I like the ‘bubble up’ approach, so you could define your file information in an object like a hashtable or psobject and use a loop. Results returned from PutFiles are rolled into or bubbled up in to $transferResult.

      Note: This is psuedo-code and you can define these in appropriate places in your script:

      Using a PSObject gives you more flexibility as you could define all of the arguments that PutFiles requires. Note how for the OMS_KPI we specify that we DO want to remote the file, but do not for the previous two:

    • #262694
      Participant
      Topics: 1
      Replies: 5
      Points: 23
      Rank: Member

      I like the โ€˜bubble upโ€™ approach, so you could define your file information in an object like a hashtable or psobject and use a loop. Results returned from PutFiles are rolled into or bubbled up in to $transferResult.

      Note: This is psuedo-code and you can define these in appropriate places in your script:

      <link rel=”stylesheet” type=”text/css” href=”https://powershell.org/wp-content/plugins/urvanov-syntax-highlighter/themes/powershell-ise/powershell-ise.css”&gt;
      <link rel=”stylesheet” type=”text/css” href=”https://powershell.org/wp-content/plugins/urvanov-syntax-highlighter/fonts/liberation-mono.css”&gt;

      Using a PSObject gives you more flexibility as you could define all of the arguments that PutFiles requires. Note how for the OMS_KPI we specify that we DO want to remote the file, but do not for the previous two:

      <link rel=”stylesheet” type=”text/css” href=”https://powershell.org/wp-content/plugins/urvanov-syntax-highlighter/themes/powershell-ise/powershell-ise.css”&gt;
      <link rel=”stylesheet” type=”text/css” href=”https://powershell.org/wp-content/plugins/urvanov-syntax-highlighter/fonts/liberation-mono.css”&gt;

      wooohhh, this is cool. i’m gonna experiment with it this way. thanks @Rob Simmons!

Viewing 12 reply threads
  • You must be logged in to reply to this topic.