简体   繁体   English

在“ {}”中具有String / ArrayValue的PowerShell New Pipline变量

[英]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.

相关问题 Bash,将字符串与数组值进行比较 - Bash, compare string to arrayvalue PowerShell 将字符串转换为变量 - PowerShell convert string to variable 有没有办法从反转的字符串变量中创建一个新的字符串变量? - Is there a way to make a new string variable from a reversed string variable? 将可变数量的数组添加到新的String数组 - Adding variable amount of arrays to a new String array Ruby —使用字符串作为变量名称来定义新变量 - Ruby — use a string as a variable name to define a new variable Firebase PHP数据提交 - arrayValue,mapValue - Firebase PHP Data Submission - arrayValue, mapValue 在PowerShell DSC中使用字符串替换中的变量时“错误格式化字符串” - “Error formatting string” when using variable from string replacement in PowerShell DSC Powershell-如何使用变量定义具有动态确定的多个元素的新阵列行 - Powershell - how do you use a variable to define a new array line with a number of elements dynamically determined SQL 数组:Select 来自 my_table 的 ID,其中“arrayvalue”=“defined_arrayvalue” - SQL ARRAY: Select ID from my_table where "arrayvalue" = "defined_arrayvalue" 基于字符串变量将二维数组项添加到新数组 - Add 2D array item to new array based on string variable
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM