powershell - 如何创建适用于格式表的 PowerShell 数据结构
问题描述
当我执行“$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 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 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
$States2.Values |Format-Table
推荐阅读
- sapui5 - 根据条件操作表格行中的图标
- c++ - c++ 上的向量作为类的成员
- macos - 将数据从一个单元格中分离出来,没有分隔符到列
- flutter - TextFormField 在选择图像后忽略任何文本更改
- node.js - 环回nodejs服务器未启动
- javascript - React,gatsbyjs:循环对象 - 组件不会被渲染
- .htaccess - mod_rewrite 错误 500 对于某些没有重写的文件不会给出错误
- javascript - 从请求中解析响应正文并返回给客户端
- security - 如果用户没有电子邮件帐户,则实施密码恢复
- validation - Google 表单中的字段验证有多安全?