我在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
不能使用 +
运算符向ArrayList
添加PSObject
。使用 ArrayList
的 Add
方法将项追加到列表中。
$changeSet.Add($fileObject)
@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
}
}