有没有办法将文本表转换为 PowerShell 对象 [英] Is there a way to convert tables of text into a PowerShell Object

查看:65
本文介绍了有没有办法将文本表转换为 PowerShell 对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有许多工具可以以表格格式输出数据.一个这样的例子是diskpart.去掉一些无关的输出,你会得到这样的东西.

磁盘 ### 状态大小 Free Dyn Gpt-------- ------------- ------- ------- --- ---磁盘 0 在线 136 GB 0 B磁盘 1 离线 136 GB 136 GB磁盘 2 保留 1027 MB 0 B *磁盘 3 保留 500 GB 0 B *磁盘 4 保留 500 GB 0 B *磁盘 5 保留 10 GB 0 B *磁盘 6 保留 13 GB 0 B *磁盘 7 保留 4102 MB 0 B *磁盘 8 保留 7169 MB 0 B *磁盘 9 保留 503 GB 0 B *磁盘 10 保留 506 GB 0 B *磁盘 11 保留 500 GB 0 B *磁盘 12 保留 3891 GB 0 B *磁盘 13 保留 500 GB 0 B *磁盘 14 保留 3891 GB 0 B *磁盘 15 保留 1843 GB 0 B磁盘 16 保留 3072 GB 0 B *磁盘 17 保留 2048 GB 0 B *磁盘 18 保留 808 GB 0 B *磁盘 19 保留 805 GB 0 B *磁盘 20 保留 3891 GB 0 B *磁盘 21 保留 3891 GB 0 B *磁盘 22 保留 3891 GB 0 B *磁盘 23 保留 6144 GB 0 B *

另一个例子是netstat,如下所示:

 原始本地地址外地址状态TCP 0.0.0.0:80 7ANDYS:0 聆听TCP 0.0.0.0:135 7ANDYS:0 聆听TCP 0.0.0.0:443 7ANDYS:0 聆听TCP 0.0.0.0:445 7ANDYS:0 聆听TCP 0.0.0.0:1025 7ANDYS:0 监听TCP 0.0.0.0:1026 7ANDYS:0 监听TCP 0.0.0.0:1027 7ANDYS:0 聆听TCP 0.0.0.0:1028 7ANDYS:0 监听TCP 0.0.0.0:1029 7ANDYS:0 聆听TCP 0.0.0.0:2048 7ANDYS:0 监听

我想弄清楚是否有一种相当可重复的方法将这种类型的数据转换为对象,这样对象的属性就是第一行的标题.我知道有很多方法可以使用正则表达式为单个工具的输出执行此操作,但我正在寻找更多有关如何解决此问题的策略,而不是仅针对 diskpart 或 netstat 的一次性解决方案.

我试图弄清楚如何在名为 Convert-TextToObject 的 Poshcode 上使用 Lee Holmes 的脚本,但是不太确定从哪里开始.

解决方案

你看到了吗:http://thepowershellguy.com/blogs/posh/archive/2007/03/24/hey-powershell-guy-how-can-i-parse-a-tab-delimited-file-and-then-save-that-as-a-comma-separated-values-file.aspx这可能正是您要找的.

There are many tools that output their data in in a table format. One such example is diskpart. Shaving off some extraneous output, you would get something like this.

Disk ###  Status         Size     Free     Dyn  Gpt
--------  -------------  -------  -------  ---  ---
Disk 0    Online          136 GB      0 B
Disk 1    Offline         136 GB   136 GB
Disk 2    Reserved       1027 MB      0 B        *
Disk 3    Reserved        500 GB      0 B        *
Disk 4    Reserved        500 GB      0 B        *
Disk 5    Reserved         10 GB      0 B        *
Disk 6    Reserved         13 GB      0 B        *
Disk 7    Reserved       4102 MB      0 B        *
Disk 8    Reserved       7169 MB      0 B        *
Disk 9    Reserved        503 GB      0 B        *
Disk 10   Reserved        506 GB      0 B        *
Disk 11   Reserved        500 GB      0 B        *
Disk 12   Reserved       3891 GB      0 B        *
Disk 13   Reserved        500 GB      0 B        *
Disk 14   Reserved       3891 GB      0 B        *
Disk 15   Reserved       1843 GB      0 B
Disk 16   Reserved       3072 GB      0 B        *
Disk 17   Reserved       2048 GB      0 B        *
Disk 18   Reserved        808 GB      0 B        *
Disk 19   Reserved        805 GB      0 B        *
Disk 20   Reserved       3891 GB      0 B        *
Disk 21   Reserved       3891 GB      0 B        *
Disk 22   Reserved       3891 GB      0 B        *
Disk 23   Reserved       6144 GB      0 B        *

Another example is netstat, which looks like the following:

 Proto  Local Address          Foreign Address        State
 TCP    0.0.0.0:80             7ANDYS:0               LISTENING
 TCP    0.0.0.0:135            7ANDYS:0               LISTENING
 TCP    0.0.0.0:443            7ANDYS:0               LISTENING
 TCP    0.0.0.0:445            7ANDYS:0               LISTENING
 TCP    0.0.0.0:1025           7ANDYS:0               LISTENING
 TCP    0.0.0.0:1026           7ANDYS:0               LISTENING
 TCP    0.0.0.0:1027           7ANDYS:0               LISTENING
 TCP    0.0.0.0:1028           7ANDYS:0               LISTENING
 TCP    0.0.0.0:1029           7ANDYS:0               LISTENING
 TCP    0.0.0.0:2048           7ANDYS:0               LISTENING

I am trying to figure out if there is a fairly repeatable way to convert this type of data into an object, such that the properties of the object are the headers in the first row. I know there are a bunch of ways to do this for the output of individual tools using regex, but I am looking for more of a strategy on how to go about solving this, rather than a one-off solution just for diskpart or netstat.

I was trying to figure out how to use Lee Holmes' script up on Poshcode called Convert-TextToObject, but wasn't quite sure where to start.

解决方案

Have you seen this: http://thepowershellguy.com/blogs/posh/archive/2007/03/24/hey-powershell-guy-how-can-i-parse-a-tab-delimited-file-and-then-save-that-as-a-comma-separated-values-file.aspx It might be what you are looking for.

这篇关于有没有办法将文本表转换为 PowerShell 对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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