[英]PowerShell New Pipline variable with String/ArrayValue in “{}”
I need a solution to create an array in a certain format. 我需要一种以某种格式创建数组的解决方案。
Background I export a SharePoint list and get my fields from an XML file. 背景我导出SharePoint列表并从XML文件获取字段。
The command (via PnP) to export is as follows: 导出命令(通过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"
That works, but here's what I want: 可行,但这就是我想要的:
$ItemsSelection = @{ L = "$FieldDisplayName[0]"; E = { $_[$FieldURLName[0]] } },@{ L = "$FieldDisplayName[1]"; E = { $_[$FieldURLName[1]] } } }
I get the $FieldDisplayName
and $FieldURLName
variables from my XML. 我从XML获取$FieldDisplayName
和$FieldURLName
变量。
Unfortunately I only get the following output: 不幸的是,我只得到以下输出:
@{ L = "ID"; E = { $_[$FieldURLName[0]] } },@{ L = "SP_SiteURL"; E = { $_[$FieldURLName[1]] } } }
Name Value
---- -----
E $FieldURLName[0]
L ID
E $FieldURLName[1]
L SP_SiteURL
So how can I get the value of "E", not the text? 那么,如何获得“ E”而不是文本的值? The variables does not resolves. 变量无法解析。
Thanks a lot! 非常感谢!
So how can I get the value of "E", not the text? 那么,如何获得“ E”而不是文本的值?
You get the text of the E
scriptblock because it hasn't executed yet - that only happens once you use it with Select-Object
. 您会得到E
脚本块的文本,因为它尚未执行 -仅在与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"
Ok i have a solution, is unattractive but apparently there is no other possibility. 好吧,我有一个解决方案,没有吸引力,但显然没有其他可能性。 I use the switch Case: 我用的开关盒:
switch ($FieldDisplayName.Length){ 开关($ FieldDisplayName.Length){
1 {$items | Select @{ L = $FieldDisplayName[0]; E = { $_[$FieldURLName[0]] } } | Export-Csv -Path "C:\install\ExportV1.csv"}
2 {$items | Select @{ L = $FieldDisplayName[0]; E = { $_[$FieldURLName[0]] } },@{ L = $FieldDisplayName[1]; E = { $_[$FieldURLName[1]] } } | Export-Csv -Path "C:\install\ExportV1.csv"}
} }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.