如何使用PowerShell实例化对象并设置成员字段? [英] How to instantiate an object and set member fields with PowerShell?

查看:213
本文介绍了如何使用PowerShell实例化对象并设置成员字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试如何创建对象,为该对象设置字段,然后添加对象到一个集合.

I'm trying to work out how to create objects, set fields for that object, and then add the object to a collection.

具体来说,如何创建$newPerson,其中Name字段为"joe",并且array包含"phone1,phone2,phone3"?类似地,"sue"具有其属性的"cell4 etc"和"alice"数组,依此类推.最终,要将这三个对象放入对象数组中,$collectionOfPeople?

Specifically, how can I create a $newPerson where the Name field is "joe" and with an array consisting of "phone1, phone2, phone3"? Similarly, "sue" has an array of "cell4 etc" and "alice" with her attributes, and so on. Ultimately, to put these three objects into an array of objects, $collectionOfPeople?

输出:

thufir@dur:~/flwor/csv$ 
thufir@dur:~/flwor/csv$ pwsh import.ps1 
people name
joe name
phone1 attribute
phone2 attribute
phone3 attribute
sue name
cell4 attribute
home5 attribute
alice name
atrib6 attribute
x7 attribute
y9 attribute
z10 attribute
thufir@dur:~/flwor/csv$ 

代码:

$tempAttributes = @()
$collectionOfPeople = @()

function attribute([string]$line) {
  Write-Host $line  "attribute"
  $tempAttributes += $line
}
function name([string]$line) {
  Write-Host $line  "name"

  #is a $newPerson ever instantiated?
  $newPerson = [PSCustomObject]@{
    Name       = $line
    Attributes = $tempAttributes
  }
  $newPerson  #empty?  no output
  $collectionOfPeople += $newPerson
  $tempAttributes = @()
}

$output = switch -regex -file people.csv {

  '\d' { attribute($_) ; $_ }
  default { name($_); $_ }
}
#how to read the file from top to bottom?
#[array]::Reverse($output)

#$output

$collectionOfPeople  #empty???

来自CSV文件的输入:

people
joe
phone1
phone2
phone3
sue
cell4
home5
alice
atrib6
x7
y9
z10

在上面的CSV中,记录之间没有标记,因此我在假设"每个"属性都有数字,而名称从未有数字.

In the above CSV there's no marker between records, so I'm using an if statement on the assumption that every attribute has digits while the names never have digits.

推荐答案

您可以执行以下操作,该操作与您当前的逻辑紧密相关.这确实假定文件中的数据顺序与您所说的完全相同.

You could do something like the following, which keeps closely to your current logic. This does assume that the order of data in your file is exactly as you have stated. .

switch -regex -file people.csv {
   '\d' { $Attributes.Add($_) }
   default { 
      if ($Attributes -and $Name) { 
          [pscustomobject]@{Name = $Name; Attributes = $Attributes}
      }
      $Name = $_
      $Attributes = [Collections.Generic.List[string]]@()
   }
}
# Output Last Set of Attributes
[pscustomobject]@{Name = $Name; Attributes = $Attributes}
# Cleanup 
Remove-Variable Name
Remove-Variable Attributes

如果名称没有属性,则将忽略该名称.可以通过在default块中添加条件elseif ($Name) { [pscustomobject]@{Name = $Name; Attributes = $null} }来更改该功能.

If there are no attributes for a name, that name is ignored. That feature can be changed by adding a condition elseif ($Name) { [pscustomobject]@{Name = $Name; Attributes = $null} } within the default block.

这篇关于如何使用PowerShell实例化对象并设置成员字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆