首页 > 解决方案 > “{}”中带有字符串/ArrayValue 的 PowerShell 新 Pipline 变量

问题描述

我需要一个解决方案来创建某种格式的数组。

背景 我导出一个 SharePoint 列表并从一个 XML 文件中获取我的字段。

要导出的命令(通过 PnP)如下:

$AllSPListItems = Get-PnPListItem $SharePointListName

$ItemsSelection = @{ L = "ID"; E = { $_["ID"] } },@{ L = "SP_SiteURL"; E = { $_["SP_SiteURL"] } }

$AllSPListItems | Select $ItemsSelection | Export-Csv -Path "C:\Temp\XYZ.csv"

那行得通,但这就是我想要的:

$ItemsSelection = @{ L = "$FieldDisplayName[0]"; E = { $_[$FieldURLName[0]] } },@{ L = "$FieldDisplayName[1]"; E = { $_[$FieldURLName[1]] } } }

我从我的 XML 中获取$FieldDisplayName和变量。$FieldURLName

不幸的是,我只得到以下输出:

@{ L = "ID"; E = { $_[$FieldURLName[0]] } },@{ L = "SP_SiteURL"; E = { $_[$FieldURLName[1]] } } }

Name                           Value                                                                                                                                                     
----                           -----                                                                                                                                                     
E                              $FieldURLName[0]                                                                                                                 
L                              ID
E                              $FieldURLName[1]                                                                                                                 
L                              SP_SiteURL

那么我怎样才能得到“E”的值,而不是文本呢?变量无法解析。

非常感谢!

标签: arrayspowershellvariables

解决方案


So how can I get the value of "E", not the text?

You get the text of the E scriptblock because it hasn't executed yet - that only happens once you use it with Select-Object.

To get the correct label names, remove the " quotes around the label expression:

$ItemsSelection = @{ L = $FieldDisplayName[0]; E = { $_[$FieldURLName[0]] } },@{ L = $FieldDisplayName[1]; E = { $_[$FieldURLName[1]] } } }
$AllSPListItems | Select $ItemsSelection | Export-Csv -Path "C:\Temp\XYZ.csv"

推荐阅读