将数据表转换/处理为自定义PSObject的最快方法 [英] Fastest way to convert/process Datatable to Custom PSObject

查看:133
本文介绍了将数据表转换/处理为自定义PSObject的最快方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我能够通过MySql查询(例如)填充数据表,$ devices::

I'm able to fill a datatable, $devices from a MySql query (for example):

PS C:\Users\MKANET\Desktop\dev\lab> $devices.table[10]

name                                    ip                                          mac                                    vendor
----                                    --                                          ---                                    ------
ComputerTest                            10.51.18.6                                  fd1969ff4cb9                           HewlettP

我想将该数据表类型转换为自定义PSObject;其中,mac列将转换为PSObject NoteProperty名称 Mac-Address;并且,将各个值转换为00-00-00-00字符串格式:

I'd like to convert that datatable type into a custom PSObject; where, the mac column is converted to PSObject NoteProperty name "Mac-Address"; and, respective values converted to 00-00-00-00 string format:

PS C:\Users\MKANET\Desktop\dev\lab> $devices[1]

name                               ip                                      MAC-Address                             vendor
-------                            --                                      -----------                             ------------
ComputerTest                       10.51.18.6                              fd-19-69-ff-4c-b9                       HewlettP

考虑到这些数据表可能相对较大(可能有几百行),因此我需要在Powershell中使用最快的处理方法来完成此操作。


如果它将进行转换/大大加快了修改过程的速度,我很乐意将$ Devices保留为数据表;只能将 mac列的值修改/处理为:00-00-00-00-00-00文本格式,永久

Considering these datatables may be relatively large (may have a couple hundred rows), I need the fastest processing method to do this in Powershell.

If it'll make the converting/modifying process significantly faster, I'd be happy with keeping $Devices as a Datatable; only modifying/processing the values for the "mac" column to: 00-00-00-00-00-00 text format, permanently.

推荐答案

使用Select-Object,它应该将DataRow / DataTable自动转换为Powershell中的PSCustomObject。这是我要做的转换为PSCustomObject并一口气处理MAC地址格式的操作。可以将它应用于特定索引的一行(例如示例代码),也可以应用于整个DataTable,以转换为PSObjects数组。

Using Select-Object, it should auto-convert a DataRow / DataTable to a PSCustomObject in Powershell. Here's what I would run to do the conversion to PSCustomObject and handle the MAC address formatting in one line. This can be applied to a single row at specific index (like the example code) or against the entire DataTable to convert to an array of PSObjects.

MAC地址格式删除了原始字符中的所有非字母数字字符都将转换为小写字母,然后在适当的索引处插入连字符。

The MAC address formatting removes any non-alphanumeric characters in the original, casts to lowercase, then inserts the hyphens at the appropriate indexes.

$devices.table[10] | Select-Object name,ip,@{N="MAC-Address";E={((((($_.mac -replace '[^a-zA-Z0-9]','').ToLower().insert(2,"-")).insert(5,"-")).insert(8,"-")).insert(11,"-")).insert(14,"-")}},vendor

这篇关于将数据表转换/处理为自定义PSObject的最快方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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