[英]How to transform values from a Hash Table to a different columns?
I'm new in Power Shell so I'm having a hard time to understand which command to use to a certain operation. 我是Power Shell的新手,所以我很难理解要对某个操作使用哪个命令。
PROBLEM: I have a hash table where every parameter can have 4 different words in a string that composes the field VALUE. 问题:我有一个哈希表,其中每个参数在组成字段VALUE的字符串中可以有4个不同的单词。 Eg.:
例如。:
$table:
Name Value
----- --------
parameter 1 F15 STK15
parameter 2 STK15
parameter 3 GP F15 STK15
As you can see, the table has 3 parameters and each parameter value is a STRING that could be formed by 3 different words: STK F15 and GP. 如您所见,该表具有3个参数,每个参数值都是一个STRING,可以由3个不同的词形成:STK F15和GP。
What I really need is to EXPORT this hash table to EXCEL in 4 different columns: 我真正需要的是将此哈希表导出到EXCEL中的4个不同的列中:
NAME GP STK15 F15
So in EXCEL there will be 4 columns: 因此,在EXCEL中将有4列:
NAME GP STK15 F15
parameter1 NULL STK15 F15
and so on.
I don't know how to do it, piping with | 我不知道该怎么做。 out-GridView or what ever.
out-GridView或其他。
If I could divide the string into 3 columns it already would be perfect. 如果我可以将字符串分成3列,那将是完美的。 can someone help me ?
有人能帮我吗 ?
You can do the following: 您可以执行以下操作:
$table = @{parameter1="F15 STK15";parameter2="STK15";parameter3="GP F15 STK15"}
$outArray = @()
foreach($entry in $table.GetEnumerator()) {
$o = new-object PSCustomObject
$o | Add-Member -notepropertyName "NAME" -notepropertyvalue $entry.name
"F15","STK15","GP" | % {
if($entry.Value -match $_) {
$o | Add-Member -notepropertyname "$_" -notepropertyvalue "$_"
}
else {
$o | Add-Member -notepropertyname "$_" -notepropertyvalue "NULL"
}
}
$outArray += ,$o
}
Iterate over the table and check for a match of each of the data you're trying to identify. 遍历表并检查要尝试标识的每个数据是否匹配。 Create a custom object for each value in the hashtable and populate it with the name, the required data and whether or not the each of the data were matched.
为哈希表中的每个值创建一个自定义对象,并使用名称,所需数据以及每个数据是否匹配来填充该对象。 Then append the object to an array.
然后将对象附加到数组。
You then have an object as such: 然后,您将得到一个这样的对象:
[PS] > $outArray | ft -auto
NAME F15 STK15 GP
---- --- ----- --
parameter2 NULL STK15 NULL
parameter1 F15 STK15 NULL
parameter3 F15 STK15 GP
And you can convert it to a csv
format for exporting to Excel; 您可以将其转换为
csv
格式,以导出到Excel;
[PS] > $outArray | sort -Property NAME | ConvertTo-Csv -NoTypeInformation
"NAME","F15","STK15","GP"
"parameter1","F15","STK15","NULL"
"parameter2","NULL","STK15","NULL"
"parameter3","F15","STK15","GP"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.