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

Lui*_*cia 3 powershell sharepoint

我正在尝试将共享点列表中的一些数据导出到 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
Run Code Online (Sandbox Code Playgroud)

代码很简单

$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
} 
Run Code Online (Sandbox Code Playgroud)

mkl*_*nt0 8

tl;博士:

  • $ListItemCollection是类型[System.Management.Automation.PSObject],而不是数组。

  • 确保它是一个数组 (例如,$ListItemCollection = @()+=以按预期工作,即+=附加元素[1]

  • 请注意,通常输出多个项目的命令- 然后将这些项目收集在一个常规[object[]]数组中,如果分配给一个变量 -如果该命令碰巧只返回一个项目,则仅输出一个标量;换句话说:单项输出数组会自动展开

  • 因此,如果有一个机会,一个命令situationally只返回一个单一的对象,但你需要的结果始终是一个数组,使用@(...)时,阵列的子表达式运算符; 例如:

         # @(...) ensures that $file is an array, even if just 1 file matches
         $files = @(Get-ChildItem *.txt)
    
    Run Code Online (Sandbox Code Playgroud)

错误消息暗示它$ListItemCollection是类型[System.Management.Automation.PSObject]不是数组。

因为 type [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'
Run Code Online (Sandbox Code Playgroud)

如果要检查给定类型的运算符支持,请使用如下命令,以[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)
Run Code Online (Sandbox Code Playgroud)

笔记:

  • “原始” .NET 数据类型没有这样的方法,因为对它们的运算符支持是内置的。

  • 同样,PowerShell 本身+为数组和集合 ( [object[]], [System.Collections.Generic.List[object]], ...) 实现,但请注意:

    1. 总是会构造一个新实例,并且
    2. 结果始终是类型的[object[]](除非您使用类型约束变量将数组转换回不同的集合类型)。
  • -Force是需要的,因为默认情况下Get-Member隐藏op_*方法。


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