Share via

Method invocation failed because [System.IO.FileInfo] does not contain a method named 'op_Addition'

31297832 0 Reputation points
2024-01-18T06:10:25.61+00:00

Hello, I am getting below error while running my power shell script. This error is intermittently happening. I am running same script on other servers and it works fine. So it means the code is working fine and all the suggestion to fix the issue does not help. I am calling the powershell script from command prompt and when error is encountered the cmd displays error code = 5 (Access is Denied)

Here is line of code that throw error.

$objFileToFTP = New-Object System.IO.FileInfo($objFileToFTP+$Filename+$RemoteFilename)

Any help would be appreciated.

Method invocation failed because [System.IO.FileInfo] does not contain a method named 'op_Addition'.
At F:\BIN\ESP\JOBS_CAAS_TEST\PS_FtpTransmission_GET.ps1:94 char:5
+     $objFileToFTP = New-Object System.IO.FileInfo($objFileToFTP+$File ...
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (op_Addition:String) [], RuntimeException
    + FullyQualifiedErrorId : MethodNotFound
Windows for business | Windows Server | User experience | PowerShell
0 comments No comments

2 answers

Sort by: Most helpful
  1. Rich Matheisen 48,116 Reputation points
    2024-01-20T16:15:22.76+00:00

    Not providing all the information in your problem description makes answering more like guessing!

    As a first step: add some error checking to your code!

    Then take that string concatenation operator out of the picture. A "+" in a file name may be causing the problem. You said "I am running same script on other servers and it works fine." You need to stop believing this and start adding code to your scripts that expects to encounter errors and handle them in a way that helps you figure out what's gone wrong.

    At a minimum, try adding this to replace the existing $objFileToFTP = New-Object System.IO.FileInfo($objFileToFTP+$Filename+$RemoteFilename) in your post:

    $f = "{0}{1}{2}" -f $objFileToFTP,$Filename,$RemoteFilename
    try{
        $objFileToFTP = New-Object System.IO.FileInfo($f)
    }
    catch{
        "Error creating object with string: $f"
        "`$objFileToFTP = $objFileToFTP"
        "`$Filename = $Filename"
        "`$RemoteFilename = $RemoteFilename"
        $_
    }
    

    Was this answer helpful?

    0 comments No comments

  2. Anonymous
    2024-01-19T02:35:29.2366667+00:00

    Hi 31297832,

    Please check the type of the variables $objFileToFTP, $Filename and $RemoteFilename with the GetType() method like $objFileToFTP.GetType(). It seems some of them are of type System.IO.FileInfo, not string.

    Best Regards,

    Ian Xue


    If the Answer is helpful, please click "Accept Answer" and upvote it.

    Was this answer helpful?


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.