【问题标题】:Method invocation failed because [System.Management.Automation.PSObject] doesn't contain a method named 'op_Addition'方法调用失败,因为 [System.Management.Automation.PSObject] 不包含名为“op_Addition”的方法
【发布时间】:2018-11-27 09:52:54
【问题描述】:

我正在尝试将一些数据从共享点列表导出到 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
} 

【问题讨论】:

  • 为什么要foreach 并附加项目?为什么不只是$splist.Items | Select-Object PageID,"Html Component"?
  • 另外,你在哪里实例化$ListItemCollection?它是什么样的物体?
  • 刚刚从另一个博客复制了示例代码
  • 复制代码时最好参考代码的来源。

标签: powershell sharepoint


【解决方案1】:

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 数据类型没有有这样的方法,因为内置了对它们的操作员支持。

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

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


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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-03
    • 2020-04-05
    • 1970-01-01
    相关资源
    最近更新 更多