如何创建适用于 format-table 的 PowerShell 数据结构
How to create PowerShell datastructure that works well with format-table
当我执行“$states2 | ft”时,我得到这个:
Name Value
---- -----
NY State
AZ State
当我宁愿得到这个:
Name Acres Population Founded FullName
---- ----- ---------- ------- --------
NY 50 100 1645 New York
AK 100 512 1745 Alaska
我做错了什么?
这是我的数据结构 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"
提前致谢
您需要将缩写名称作为 class 定义的一部分:
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"
如果您只想显示 $States2
哈希表中的 values,请执行:
$States2.Values |Format-Table
当我执行“$states2 | ft”时,我得到这个:
Name Value
---- -----
NY State
AZ State
当我宁愿得到这个:
Name Acres Population Founded FullName
---- ----- ---------- ------- --------
NY 50 100 1645 New York
AK 100 512 1745 Alaska
我做错了什么?
这是我的数据结构 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"
提前致谢
您需要将缩写名称作为 class 定义的一部分:
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"
如果您只想显示 $States2
哈希表中的 values,请执行:
$States2.Values |Format-Table