Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Powershell 方法调用失败,因为[System.Management.Automation.PSObject]不包含名为“op_Addition”的方法_Powershell_Sharepoint - Fatal编程技术网

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

Powershell 方法调用失败,因为[System.Management.Automation.PSObject]不包含名为“op_Addition”的方法,powershell,sharepoint,Powershell,Sharepoint,我正在尝试将一些数据从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 + $ListItemColl

我正在尝试将一些数据从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
代码非常简单

$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=@for+=按预期工作,即for+=附加一个元素[1]

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

因此,如果一个命令有可能只返回一个对象,而您需要结果始终是一个数组,那么使用@;e、 g:

错误消息表示$ListItemCollection的类型为[System.Management.Automation.PSObject],而不是数组

由于类型[pscustomobject][System.Management.Automation.PSObject]没有静态op_加法方法,因此不能将+运算符与它的实例一起用作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成员在默认情况下隐藏op_*方法


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

为什么要使用foreach和append items?为什么不仅仅是$splist.Items |选择对象PageID、Html组件?还有,在哪里实例化$ListItemCollection?它是什么类型的对象?只是从另一个博客复制了示例代码
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)