0

I am getting an error while concatenating the array lists in powershell.

  For ($i=0; $i -lt $temp.Length; $i++)
  {

    $filePath = $filePath.Replace("/", "\")
    $fileExt = $filePath.Split(".")[-1] 

     $Content = Get-Content -LiteralPath      $(Build.SourcesDirectory)\$name -Encoding $EncodingType
     $OutputFileContent += $Content
     $FileObject = New-Object PSobject -Property @{
            Path = $filePath
            Extension = $fileExt
            Contents = $OutputFileContent 
        }


   $changeSet += $FileObject
  }

The last line is causing this issue.

   Method invocation failed because [System.Management.Automation.PSObject]     does not contain a method named 'op_Addition'.

  $changeSet += $FileObject

  CategoryInfo          : InvalidOperation: (op_Addition:String) [], 
  ParentContainsErrorRecordException
FullyQualifiedErrorId : MethodNotFound
2
  • Is your script running successfully locally? Commented Aug 10, 2018 at 9:31
  • What's $changeSet? How do you declare it? Commented Aug 13, 2018 at 8:57

2 Answers 2

0

You can't add a PSObject to an ArrayList with the + operator. Use the Add method of ArrayList to append items to the list.

$changeSet.Add($fileObject)
Sign up to request clarification or add additional context in comments.

3 Comments

Still getting an error. this FileObject @{Contents=System.Object[]; Extension=pkb; Path=.\Package\TEST_PKG_I_LOOKUPS2.pkb} Method invocation failed because [System.Management.Automation.PSCustomObject] does not contain a method named 'add'. At /Users//Downloads/vsts-agent-osx-x64-2.136.1/Liquibase/_temp/63f184e0-db7e-4bbe-b130-8c2325d2b373.ps1:95 char:8 $Changeset.add($fileObject) CategoryInfo : InvalidOperation: (add:String) [], ParentContainsErrorRecordException FullyQualifiedErrorId : MethodNotFound
Then $changeSet is not an ArrayList. Are you sure the code you posted is the code you're running?
Its not ArrayList I was testing with ArrayList. I updated it now.
0

@gopal,

Use the Power of PowerShell. You can simply do below without defining $changes

$changes = For ($i=0; $i -lt $temp.Length; $i++)
           {   

               $filePath = $filePath.Replace("/", "\")
               $fileExt = $filePath.Split(".")[-1] 

               $Content = Get-Content -LiteralPath $(Build.SourcesDirectory)\$name `
               -Encoding $EncodingType
               $OutputFileContent += $Content
               $FileObject = New-Object PSobject -Property @{
                                 Path = $filePath
                                 Extension = $fileExt
                                 Contents = $OutputFileContent 
                             }
           }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.