简体   繁体   English

如何创建与格式表配合使用的 PowerShell 数据结构

[英]How to create PowerShell datastructure that works well with format-table

When I do "$states2 | ft" I get this:当我做 "$states2 | ft" 我得到这个:

Name                           Value
----                           -----
NY                             State
AZ                             State

When I would rather get this:当我宁愿得到这个时:

Name    Acres   Population   Founded  FullName
----    -----   ----------   -------  --------
NY        50           100      1645  New York
AK       100           512      1745  Alaska

What am I doing wrong?我究竟做错了什么?

Here is my data structure in the form of a class:这是我的类形式的数据结构:

class State {
    [Int64]$Acres
    [Int64]$Population
    [Int16]$Founded
    [string]$FullName
    }

$States2 = @{}
$States2.AK = [State] @{Population = 512; Founded = 1745; Acres = 100; FullName = "Alaska"}

$States2.NY = [State]::new()
$States2.NY.Acres       = 50
$States2.NY.Population = 100
$States2.NY.Founded    = 1652
$States2.NY.FullName   = "New York"

Thanks in advance提前致谢

You'll need to make the abbreviated name part of the class definition:您需要将缩写名称作为类定义的一部分:

class State {
    [string]Name
    [Int64]$Acres
    [Int64]$Population
    [Int16]$Founded
    [string]$FullName
}

$States2 = @{}
$States2.AK = [State] @{Name = "AK"; Population = 512; Founded = 1745; Acres = 100; FullName = "Alaska"}

$States2.NY = [State]::new()
$States2.NY.Name       = "NY" 
$States2.NY.Acres      = 50
$States2.NY.Population = 100
$States2.NY.Founded    = 1652
$States2.NY.FullName   = "New York"

If you only want the show the values in the $States2 hashtable, do:如果您只想显示$States2哈希表中的,请执行以下操作:

$States2.Values |Format-Table

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM