Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

I have the following PowerShell script that copies all of the config files in certain directories to my local system. This script works as expected on many of my servers, but on 4 servers it fails:

$source = @()
switch ([System.Net.Dns]::GetHostByName(($env:computerName)).HostName)
# populate an array with a list of paths that contain config files to be copied
{
    "SERVER1.DOMAIN"  {
        $source = @("C:\Program Files\MXC\", "C:\inetpub\wwwroot\")
        $target = "\\tsclient\C\Backups\Configs\SERVER1\"
    }
    "SERVER2.DOMAIN" {
        $source = @("C:\Program Files\MXC\")
        $target = "\\tsclient\C\Backups\Configs\SERVER2\"
    }
    "SERVER3.DOMAIN" {
        $source = @("C:\inetpub\wwwroot\ccb\001\", "C:\inetpub\wwwroot\eab\020\")
        $target = "\\tsclient\C\Backups\Configs\SERVER3\"
    }
    "SERVER4.DOMAIN" {
        $source = @("C:\inetpub\wwwroot\arv\drv\", "C:\inetpub\wwwroot\arv\imp\")
        $target = "\\tsclient\C\Backups\Configs\SERVER4\"
    }

function CopyConfigs ($configsList) {
    try {
        foreach ($file in $configsList) {
            # get the folder the config file lives in so it can be created in the target dir
            $folder = $file.DirectoryName | Split-Path -Leaf
            $dest = $target + $folder + "\"

            # if the $dest dir does not exist, create it.
            if (-not (Test-Path -Path $dest -PathType Container -ErrorAction SilentlyContinue)) {
                New-Item -ItemType Directory -Force -Path $dest
            }
            # copy the config file to the target directory
            Copy-Item $file -Destination  $dest -Force
        }
        Write-Host " End copying config files ====="
    } catch {
        Write-Host "** ERROR:  An error occurred while trying to copy the $file to $dest"
        return $Error[0].Exception
        exit 1
    }
}

try {
    # get the locations and names of the config files to copy
    foreach ($dir in $source) {
        $configsList += get-childitem -Recurse -Path $dir *.config -Exclude *.dll.config,*.vshost*,*app.config,*packages.config,*-*,*company.config*
    }
    CopyConfigs $configsList
} catch {
    Write-Host "** ERROR:  An error occurred while trying to get a list of config files to copy."
    Write-Host $Error[0].Exception 
    return 1
}

When it fails the error is:

Method invocation failed because [System.IO.FileInfo] does not contain a method named 'op_Addition'.
   at System.Management.Automation.ExceptionHandlingOps.CheckActionPreference(FunctionContext funcContext, Exception exception)
   at System.Management.Automation.Interpreter.ActionCallInstruction`2.Run(InterpretedFrame frame)
   at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)
   at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)

I verified PowerShell 4.0 is on all systems and that all paths are valid. This script both succeeds and fails on Windows Server 2008 R2 and Wdows Server 2012 servers.

I researched the 'op_Addition' error but I saw nothing about it working some times and not others. Normally this error has to do with trying to operate on something that is not an array, but I believe I've accounted for that in my script. And, it does work sometimes.

I'm really not sure what the problem is. Any help you can provide is greatly appreciated.

like image 877
K.J. Avatar asked Dec 20 '16 18:12

K.J.


2 Answers

Just getting into Powershell, and I came across the same error message. The way I fixed this was to call the ".toString()" method where I was doing the string concatenation. I hope this helps someone else

   foreach ($file in $configsList) {
        # get the folder the config file lives in so it can be created in the target dir
        $folder = $file.DirectoryName | Split-Path -Leaf

        # $dest = $target + $folder + "\" <-- these are objects, so make them strings as below:

        $dest = $target.ToString() + $folder.ToString() + "\"

        # if the $dest dir does not exist, create it.
        if (-not (Test-Path -Path $dest -PathType Container -ErrorAction SilentlyContinue)) {
            New-Item -ItemType Directory -Force -Path $dest
        }
        # copy the config file to the target directory
        Copy-Item $file -Destination  $dest -Force
    }
    Write-Host " End copying config files ====="
like image 184
Dai Bok Avatar answered Sep 22 '22 16:09

Dai Bok


You need to initialize the variable as an array first. Add this before you use it:

$configsList = @()
like image 45
bryce schmitendorf Avatar answered Sep 22 '22 16:09

bryce schmitendorf