在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发布于 2018-08-09 03:28:29
您不能使用+运算符将PSObject添加到ArrayList。使用ArrayList的Add方法将项追加到列表中。
$changeSet.Add($fileObject)发布于 2018-08-09 13:11:07
@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
}
}https://stackoverflow.com/questions/51754156
复制相似问题