方法调用失败,因为 [System.Management.Automation.PSObject] 不包含名为“op_Addition"的方法 [英] Method invocation failed because [System.Management.Automation.PSObject] doesn't contain a method named 'op_Addition'

查看:38
本文介绍了方法调用失败,因为 [System.Management.Automation.PSObject] 不包含名为“op_Addition"的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

方法调用失败,因为 [System.Management.Automation.PSObject] 不包含名为op_Addition"的方法.在行:20 字符:2+ $ListItemCollection += $ExportItem+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~+ CategoryInfo : InvalidOperation: (op_Addition:String) [], RuntimeException+ FullQualifiedErrorId : MethodNotFound

代码很简单

$URL = "https://mysite"$List = "页面内容"$Web = Get-SPWeb $URL$web$spList = $Web.Lists[$List]$Items = $spList.GetItems()$listItems = $spList.Itemsforeach($listItems 中的 $item) {$ExportItem = 新对象 PSObject$ExportItem |添加成员 -MemberType NoteProperty -name "PageID" -value $item["PageID"]$ExportItem |Add-Member -MemberType NoteProperty -Name "Html Component" -value $item["Html Component"]#将具有属性的对象添加到数组中$ListItemCollection += $ExportItem}

解决方案

tl;dr:

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

  • 确保它是一个 array(例如,$ListItemCollection = @()),用于 +=旨在,即 += 附加一个元素[1].

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

  • 因此,如果命令有可能仅返回一个单个对象,但您需要结果始终是一个array,使用 @(...)数组子表达式运算符;例如:

     # @(...) 确保 $file 是一个数组,即使只有 1 个文件匹配$files = @(Get-ChildItem *.txt)


错误消息暗示 $ListItemCollection 属于 [System.Management.Automation.PSObject] 类型,不是数组.>

由于类型 [pscustomobject] ([System.Management.Automation.PSObject]) 没有静态 op_Addition 方法,您不能使用 + 运算符及其实例作为 LHS.
(特定于类型的运算符实现为静态 op_* 方法).

您可以通过以下方式验证:

PS>(新对象 System.Management.Automation.PSObject) + 1 # !!休息时间方法调用失败,因为 [System.Management.Automation.PSObject] 不包含名为op_Addition"的方法

如果要检查给定类型的运算符支持,请使用如下命令,以 [datetime] 类型为例:

PS>[日期时间] |Get-Member -Force -Static -Type 方法 op_*类型名称:System.DateTime名称 MemberType 定义---- ---------- ----------op_Addition 方法 static datetime op_Addition(datetime d, timespan t)op_Equality 方法 static bool op_Equality(datetime d1, datetime d2)op_GreaterThan 方法 static bool op_GreaterThan(datetime t1, datetime t2)op_GreaterThanOrEqual 方法 static bool op_GreaterThanOrEqual(datetime t1, datetime t2)op_Inequality 方法 static bool op_Inequality(datetime d1, datetime d2)op_LessThan 方法 static bool op_LessThan(datetime t1, datetime t2)op_LessThanOrEqual 方法 static bool op_LessThanOrEqual(datetime t1, datetime t2)op_Subtraction 方法 static datetime op_Subtraction(datetime d, timespan t), static timespan op_Subtraction(datetime d1, datetime d2)

注意:

  • 原始".NET 数据类型没有有这样的方法,因为对它们的操作符支持是内置的.

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

    1. 总是会构造一个新实例,并且
    2. 结果是总是 [object[]] 类型(除非您使用类型约束变量将数组转换回不同的集合类型).

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


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

I am trying to export some data from sharepoint list to csv and I got this error:

$ListItemCollection | Export-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

the code is really easy

$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;dr:

  • $ListItemCollection is of type [System.Management.Automation.PSObject], not an array.

  • Make sure that it is an array (e.g., $ListItemCollection = @()) for += to work as intended, i.e., for += to append an element[1].

  • Note that commands that typically output multiple items - which are then collected in a regular [object[]] array, if assigned to a variable - output only a scalar if the command situationally happens to return only one item; in other words: a single-item output array is automatically unwrapped.

  • Therefore, if there's a chance that a command situationally returns only a single object, yet you need the result to always be an array, use @(...), the array-subexpression operator; e.g.:

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


The error message implies that $ListItemCollection is of type [System.Management.Automation.PSObject] and not an array.

Since type [pscustomobject] ([System.Management.Automation.PSObject]) does not have a static op_Addition method, you cannot use the + operator with an instance of it as the LHS.
(Type-specific operators are implemented as static op_* methods).

You can verify this as follows:

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'

If you want to inspect a given type for operator support, use a command such as the following, using the [datetime] type as an example:

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)

Note:

  • The "primitive" .NET data types do not have such methods, because operator support for them is built in.

  • Similarly, it is PowerShell itself that implements + for arrays and collections ([object[]], [System.Collections.Generic.List[object]], ...), though note that:

    1. a new instance is invariably constructed, and
    2. the result is always of type [object[]] (unless you use a type-constrained variable that converts the array back to a different collection type).

  • -Force is needed, because Get-Member hides the op_* methods by default.


[1] Technically, a new array is created behind the scenes, because arrays are immutable. In loops this can be a performance concern; if so, use a mutable collection type such as [System.Collections.Generic.List[object]] and append to it with its .Add() method.

这篇关于方法调用失败,因为 [System.Management.Automation.PSObject] 不包含名为“op_Addition"的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆