如何在Powershell中的csv中包含所有数组对象的所有属性 [英] How to include all properties of all array objects in csv in powershell

查看:240
本文介绍了如何在Powershell中的csv中包含所有数组对象的所有属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Powershell脚本,该脚本读取一个带有共享及其属性的单行描述的文本文件.

I have a Powershell script that reads a text file with single line descriptions of shares and their properties.

然后,对于每个共享,它向具有属性的数组中添加一个对象.

It then, for each share, adds an object to an array, with properties.

我最终获得了一系列股份,其中许多具有共同的属性,但也有一些只出现在少数股份中.

I end up with an array of shares, many with common properties but also some that only appear on a few shares.

当我将完成的数组通过管道传输到Export-CSV时,生成的CSV仅具有与数组中第一个对象的属性匹配的列.

When I pipe the finished array to Export-CSV, the resulting CSV only has columns matching the properties of the first object in the array.

感谢收到任何想法/帮助.

Any ideas/help gratefully received.

这是代码:

$shares_only = gc \\server\share\SHARES.txt | where {$_ -match '^*TYPE = DISK'}

$share_index = @()

$shares_only |foreach {

    $sharename = ($_.Split(" ")[1])
    $_ -match '\([\w\s.,"=<>()\/\*\+\-]*\);'|out-null

    $shareparams = $matches[0]

    $paramlist = ($shareparams.Substring(1,($shareparams.Length -3))).Split(",")

    $obj = $null
    $obj = New-Object System.Object

    $obj | Add-Member -type NoteProperty -Name NAME -Value $sharename

    $paramlist | foreach {
        $obj `
        | Add-Member -type NoteProperty -Name ($_.Split("=")[0]) -Value ($_.Split("=")[1])
    }
    $share_index += $obj
}

$share_index | Select * | Export-CSV -notype \\server\share\Shares.csv

以下是数组中的前两个对象作为示例(使用$share_index[0..2]输出到控制台):

Here is the first two objects in the array as an example (using $share_index[0..2] to output to console):

NAME          : _ACCTEST_
TYPE          :  DISK 
PREFIX        :  (<USERCODE>) 
AREABYTES     :  184320 
ACCESS        :  ALL 
FAMILY        :  ACCTESTPACK 
DOWNSIZEAREA  :  TRUE 

NAME              : _HOME_
TYPE              :  DISK 
PREFIX            :  (<USERCODE>) 
COMMENT           :  Private user files 
AREABYTES         :  184320 
ACCESS            :  ALL -EXTRACT 
FAMILY            :  <FAMILY> 
DOWNSIZEAREA      :  TRUE 
ALLOWGUESTACCESS  :  TRUE 

这是CSV的前三行:

"NAME","TYPE ","PREFIX ","AREABYTES ","ACCESS ","FAMILY ","DOWNSIZEAREA "
"_ACCTEST_"," DISK "," (<USERCODE>) "," 184320 "," ALL "," ACCTESTPACK "," TRUE "
"_HOME_"," DISK "," (<USERCODE>) "," 184320 "," ALL -EXTRACT "," <FAMILY> "," TRUE "

即使在_HOME_共享的数组中,您也可以看到COMMENTALLOWGUESTACCESS属性丢失.

You can see that COMMENT and ALLOWGUESTACCESS properties are missing even although they are in the array for the _HOME_ share.

编辑:已接受@JPBlancs答案的略微修改版本,因此我的代码的最后两行现在为:

Accepted slightly modified version of @JPBlancs answer, so last 2 lines of my code is now:

$fixed_index = $share_index | Sort-Object -Property @{expression={(($_.psobject.Properties)|Measure-Object).count}} -Descending | ConvertTo-CSV -notype
$fixed_index | ConvertFrom-CSV | Sort -Property Name | Export-CSV -notype \\server\share\Shares.csv 

因此,按照建议进行操作,然后在新对象中将其转换为CSV. 然后,将该对象从CSV转换回去,保留新的属性,在名称"上再次排序以获取所需的字母顺序列表,然后再次导出为CSV.

So, done as suggested, then converted to CSV in a new object. Then, convert that object back from CSV, retaining the new properties, sort again on Name to get the alphabetical list I need, then Export to CSV again.

例如,给我

"NAME","TYPE","PREFIX","PUBLIC","COMMENT","AREABYTES","ACCESS","FAMILY","DOWNSIZEAREA","ALLOWGUESTACCESS"
"_ACCTEST_"," DISK "," (<USERCODE>) ","",""," 184320 "," ALL "," ACCTESTPACK "," TRUE ",
"_HOME_"," DISK "," (<USERCODE>) ",""," Private user files "," 184320 "," ALL -EXTRACT "," <FAMILY> "," TRUE "," TRUE "

推荐答案

我做了类似的事情

$a = "" | select "P1","P2"
$b = "" | select "P1","P2","P3"
$c = "" | select "P1","P2","P3","P4"
$array = $a,$b,$c
$array | Export-Csv c:\temp\t.csv

如您所愿:

#TYPE Selected.System.String
"P1","P2"
,
,
,

因此,想法是按照属性计数对对象进行排序:

So the idea is to sort the objects by the properties count :

$array | Sort-Object -Property @{expression={$_.psobject.properties.count}} -Descending| Export-Csv c:\temp\t.csv

它给出了:

#TYPE Selected.System.String
"P1","P2","P3","P4"
,,,
,,,
,,,


对于问题(对象创建时将成员添加到System.Object),您可以使用:


In the case of the question (object created adding members to a System.Object) you can use :

Sort-Object -Property @{expression={(($_.psobject.Properties)|Measure-Object).count}} -Descending

这篇关于如何在Powershell中的csv中包含所有数组对象的所有属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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