我正在尝试将共享点列表中的一些数据导出到 csv,但出现此错误:

$ListItemCollection |导出-CSV "D:\LX.csv"-NoTypeInformation

Method invocation failed because [System.Management.Automation.PSObject] doesn't contain a method named 'op_Addition'.
At line:20 char:2
+     $ListItemCollection += $ExportItem
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (op_Addition:String) [], RuntimeException
    + FullyQualifiedErrorId : MethodNotFound

代码很简单
$URL = "https://mysite"
$List = "Page Contents"

$Web = Get-SPWeb $URL

$web
$spList = $Web.Lists[$List]
$Items = $spList.GetItems()


$listItems = $spList.Items

foreach($item in $listItems) {

    $ExportItem = New-Object PSObject
    $ExportItem | Add-Member -MemberType NoteProperty -name "PageID" -value $item["PageID"]
    $ExportItem | Add-Member -MemberType NoteProperty -Name "Html Component" -value $item["Html Component"]

    #Add the object with property to an Array
    $ListItemCollection += $ExportItem
}

最佳答案

tl;博士:

  • $ListItemCollection[System.Management.Automation.PSObject] 类型,而不是数组。
  • 确保它是一个数组(例如 $ListItemCollection = @() ),以便 += 按预期工作,即 += 附加元素 [1]。
  • 请注意,通常输出多个项目的命令 - 然后将其收集在常规 [object[]] 数组中,如果分配给变量 - 如果该命令碰巧仅返回一个项目,则仅输出标量;换句话说:单项输出数组会自动展开。
  • 因此,如果命令有可能只返回一个对象,但 您需要结果始终是一个数组,请使用 @(...) ,即 array-subexpression operator ;例如。:
         # @(...) ensures that $file is an array, even if just 1 file matches
         $files = @(Get-ChildItem *.txt)
    

  • 错误消息暗示 $ListItemCollection[System.Management.Automation.PSObject] 类型而不是数组。
    由于类型 [pscustomobject] ( [System.Management.Automation.PSObject] ) 没有静态 op_Addition 方法,因此不能将 + 运算符与它的实例一起用作 LHS。
    (特定于类型的运算符实现为静态 op_* 方法)。
    您可以按如下方式验证这一点:
    PS> (New-Object System.Management.Automation.PSObject) + 1 # !! Breaks
    Method invocation failed because [System.Management.Automation.PSObject] does not contain a method named 'op_Addition'
    
    如果要检查给定类型的运算符支持,请使用如下命令,以 [datetime] 类型为例:
    PS> [datetime] | Get-Member -Force -Static -Type Method op_*
    
       TypeName: System.DateTime
    
    Name                  MemberType Definition
    ----                  ---------- ----------
    op_Addition           Method     static datetime op_Addition(datetime d, timespan t)
    op_Equality           Method     static bool op_Equality(datetime d1, datetime d2)
    op_GreaterThan        Method     static bool op_GreaterThan(datetime t1, datetime t2)
    op_GreaterThanOrEqual Method     static bool op_GreaterThanOrEqual(datetime t1, datetime t2)
    op_Inequality         Method     static bool op_Inequality(datetime d1, datetime d2)
    op_LessThan           Method     static bool op_LessThan(datetime t1, datetime t2)
    op_LessThanOrEqual    Method     static bool op_LessThanOrEqual(datetime t1, datetime t2)
    op_Subtraction        Method     static datetime op_Subtraction(datetime d, timespan t), static timespan op_Subtraction(datetime d1, datetime d2)
    
    笔记:
  • “原始” .NET 数据类型没有这样的方法,因为对它们的运算符支持是内置的。
  • 类似地,PowerShell 本身为数组和集合( +[object[]] ,...)实现了 [System.Collections.Generic.List[object]],但请注意:
  • 总是构造一个新实例,并且
  • 结果始终是 [object[]] 类型(除非您使用类型约束变量将数组转换回不同的集合类型)。

  • -Force 是必需的,因为 Get-Member 默认隐藏了 op_* 方法。

  • [1] 从技术上讲,新数组是在幕后创建的,因为数组是不可变的。在循环中,这可能是一个性能问题;如果是这样,请使用可变集合类型,例如 [System.Collections.Generic.List[object]] 并使用其 .Add() 方法附加到它。

    关于powershell - 方法调用失败,因为 [System.Management.Automation.PSObject] 不包含名为 'op_Addition' 的方法,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/50911366/

    10-11 23:27
    查看更多