Concat ArrayList:方法调用失败,因为[System.Management.Automation.PSObject]不包含名为“ op_Addition”的方法

时间:2018-08-08 19:19:20

标签: azure-devops powershell-v2.0 azure-pipelines

在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
  }

最后一行导致此问题。

   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 个答案:

答案 0 :(得分:0)

不能使用PSObject运算符将ArrayList添加到+。使用Add的{​​{1}}方法将项​​目追加到列表中。

ArrayList

答案 1 :(得分:0)

@gopal,

使用PowerShell的强大功能。您只需在下面进行操作,而无需定义$ 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 
                             }
           }