通过命名属性从导入的 CSV 中获取对象

时间:2021-02-24 12:27:28

标签: powershell

$Script = Import-Csv "C:\temp\script.csv"
$Script.GetType()
$Script
Write-Host $Script["0"].Body


IsPublic IsSerial Name                                     BaseType                                                                                                                   
-------- -------- ----                                     --------                                                                                                                   
True     True     Object[]                                 System.Array

Title        Body                                                                                                                                                                     
-----        ----                                                                                                                                                                     
IntroMessage Intro message body
Testmessage  Test message body

Intromessage body

我有上面的代码按预期工作,但我正在尝试(但失败)以更易读的方式读出对象。 IE。我希望能够使用 title 属性而不是索引来调用每个对象,但我在这里摸不着头脑。

3 个答案:

答案 0 :(得分:0)

听起来您需要一个循环语句或 ForEach-Object cmdlet:

$Script |ForEach-Object {
    Write-Host "This next object has the title $($_.Title)"
}

$_ 将引用正在处理的集合中的当前项目。

如果您需要跟踪索引,也可以使用 for 循环:

for($i = 0; $i -lt $Script.Length; $i++){
    Write-Host "Object at index $i has the title $($Script[$i].Title)"
}

答案 1 :(得分:0)

$Script = Import-Csv "C:\temp\script.csv"

您需要使用 $Script 数组中元素的索引并从中获取属性(即 $Script[1].Body --> "Test message body")

或者如果你想通过Title能够得到Body部分,你需要把这个对象数组转成Hashtable:

# create a Hashtable where the Keys are the items 'Title' property and
# the Values are the items 'Body' property
$Bodies = @{}
$Script | ForEach-Object { $Bodies[$_.Title] = $_.Body }

现在你可以使用

$Bodies['IntroMessage']  # --> "Intro message body"
$Bodies['Testmessage']   # --> "Test message body"

或 .dot 符号

$Bodies.IntroMessage  # --> "Intro message body"
$Bodies.Testmessage   # --> "Test message body"

另一种选择是保留 $Script(对象)数组原样并使用 Where-Object 子句过滤:

($Script | Where-Object { $_.Title -eq 'IntroMessage' }).Body  # --> "Intro message body"
($Script | Where-Object { $_.Title -eq 'Testmessage' }).Body   # --> "Test message body"

答案 2 :(得分:0)

我不知道我是否误读了这个问题,但是如果您想使用 title 属性引用对象,则必须使用哈希(字典)对象对它们进行索引。

$Script = @{} # Define an empty hash
Import-Csv "C:\temp\script.csv" |
ForEach-Object{
    If( !$Script.Contains($_.Title) )
    {
        $Script.Add( $_.Title, $_ )
    }
    Else
    {
        Write-Host "$($_.Title) is a duplicate key and cannot be added to the hash"
    }
}

# Now reference like:
$Script.'Testmessage' # will return the value/object at that key like:

<#
Title        Body                                                                                                                                                                     
-----        ----                                                                                                                                                                     
Testmessage  Test message body
#>

注意:您也可以使用$Script['Testmessage']这样的索引格式来回调值/对象。

这假设标题是唯一的。如果离散对象有重复的标题,您可以使用集合作为值。如果需要,我可以用一个例子来修改答案。当然,当前示例的编写方式将暴露该问题(如果存在)。

相关问题