Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/12.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
使用“时的惰性管道评估”;选择";导出CSV文件的步骤_Csv_Powershell_Select_Pipe_Lazy Evaluation - Fatal编程技术网

使用“时的惰性管道评估”;选择";导出CSV文件的步骤

使用“时的惰性管道评估”;选择";导出CSV文件的步骤,csv,powershell,select,pipe,lazy-evaluation,Csv,Powershell,Select,Pipe,Lazy Evaluation,预期脚本输出:我想从Microsoft SharePoint列表中导出具有特定列的CSV文件 脚本大纲: 首先,我获取$items中的列表项: $query = New-Object Microsoft.SharePoint.Client.CamlQuery $items = $list.GetItems($query) 输出CSV文件的列在hashmap中定义($displayName=>$internalName) 现在,我为每列创建一个新的哈希表,并将其添加到$selects: $sel

预期脚本输出:我想从Microsoft SharePoint列表中导出具有特定列的CSV文件

脚本大纲: 首先,我获取
$items
中的列表项:

$query = New-Object Microsoft.SharePoint.Client.CamlQuery
$items = $list.GetItems($query)
输出CSV文件的列在hashmap中定义(
$displayName=>$internalName

现在,我为每列创建一个新的哈希表,并将其添加到
$selects

$selects = @()
foreach ($h in $mapping.GetEnumerator()) {
     $selects += @{ L = $h.Name; E = { $_[$h.Value] } }
}
然后我导出CSV文件:

$items | Select-Object $selects | Export-Csv -Path $outputPath
现在的问题是:

在E中,我定义了一个表达式,只有当我需要它时才会计算它(在执行Select时也是如此)。 发生这种情况时,对
$h.Value
属性的引用已被更改,因为
$h
正在循环。 因此,在计算表达式时,
$h
指向已循环的最后一个对象(因此是hashmap的最后一个元素)。管道中正在求值的每个select语句都是这种情况

我可以通过将
$h
(或其值)的引用保存在另一个变量中来修复此问题。 但是我需要一个硬编码的变量数

如何解决此问题?

多亏了测试和工作代码示例:

foreach ($h in $mapping.GetEnumerator()) {
     $selects += @{ L = $h.Name; E = { $_[$h.Value] }.GetNewClosure() }
}

如前所述,我们可以使用闭包在循环中冻结对
$h
的引用。

{$\u[$h.Value]}
->
{$\u[$h.Value]}.GetNewClosure()