header
header Register : : Login header
header
divider
menuleft
menuright
submenu
left

[August 25th, 2008] Check the home page regarding PowerShell related news from a brand new sponsor: Idera

Subject: Array of Custom Objects - How to export to a csv
Prev Next
You are not authorized to post a reply.

Author Messages
KarlMitschkeUser is Offline
Power User
Power User
Posts:144

07/24/2008 10:39 AM  

I have an array of objects that I created with subinacl such as this:

$allservers [ 0 ]

Servername       :
Share            :
SharePath        :
ShareDescription : Default share
Hidden Share     : True

$allservers[ 430 ]

Servername       :
Share            :
SharePath        :
ShareDescription :
Hidden Share     : False
DACL 1            :
DACL 1 Permission : Read
DACL 2            :
DACL 2 Permission : Read
DACL 3            :
DACL 3 Permission : Read
DACL 4            :
DACL 4 Permission : Read
DACL 5            :
DACL 5 Permission : Read
DACL 6            :
DACL 6 Permission : Read
DACL 7            :
DACL 7 Permission : Read
DACL 8            :
DACL 8 Permission : Read

Some shares have no DACL's, some have 2, some have 8.

If I do a

$allservers |export-csv test.csv, all I get is

Servername,Share,SharePath,ShareDescription,"Hidden Share" for my header row, and any row that actually has DACL's loses them.

If I send $allservers to out-file, the data is correct.

How can I export these data to a csv file?

glnsizeUser is Offline
Shell Enthusiast
Shell Enthusiast
Posts:60

07/24/2008 11:11 AM  

export-csv is used to export objects. There are many examples of custom object creation either using select-object | New-Object/Add-member... if your not already familiar with the techniques.  Take 15 min and watch BSonPOSH presentation to the PowerShell VUG.
 

If the data is exporting correctly into a csv using out-file.  Then you can create a custom object after the fact.  Just add the following to the first line in the file.

#TYPE System.Management.Automation.PSCustomObject


You can use a similar technique to turn any CSV into a PowerShell custom object... just follow the format First line declares the Type of the object, the next line defines the properties, your data starts on line three.

#TYPE System.Management.Automation.PSCustomObject
name, ip, mac
pc1,10.10.10.1,xxxx.xxxx.xxxx.xxxx

~Glenn

 

 

KarlMitschkeUser is Offline
Power User
Power User
Posts:144

07/24/2008 11:56 AM  
Glenn;
I'm not sure if you understood me? I have an array of objects that I need to export.
Here is an example of how I create an object and add it to the array:

$allservers=@()
$obj = new-object psObject
$obj | Add-Member -membertype noteproperty -name Servername -Value $servername
$obj | Add-Member -MemberType noteproperty -Name Share -Value $share.name
$obj | Add-Member -MemberType noteproperty -Name SharePath -Value $share.path
$obj | Add-Member -MemberType noteproperty -Name ShareDescription -Value $share.description
$obj | Add-Member -MemberType noteproperty -Name "Hidden Share?" -Value "False"
$dacl = subacl /verbose /share $sharepath /display=dacl /testmode 
$dacl = $dacl -replace "`0",""
$dacl = $dacl -replace "`t",""
$line = New-Object System.Collections.ArrayList
$linenumber = 0
$daclnumber = 0
foreach ($line in $dacl)
{
    if ($line -match "/pace")
    {
        $daclnumber ++
        $who = $line -replace("/pace =","")
        $permission =  $dacl[$linenumber+2]
        $obj | Add-Member -MemberType noteproperty -Name "DACL $daclnumber" -Value $who
        $obj | Add-Member -MemberType noteproperty -Name "DACL $daclnumber Permission" -Value $permission
    }
    $linenumber ++
}
$allservers += $obj


I can do this and I get the data: $allservers |out-file test.txt
If $allservers[ 0 ] contains no dacl's, then the following would show in a csv file using export-csv:
Servername,Share,SharePath,ShareDescription ,Hidden Share

If the first item in $allservers has 5 DACL's the header will show the five dacl's and permissions.

But, if item 200 has 10 DACL's, the header still shows only what item 0 had, and all the data is gone.

$allservers |gm returns:
TypeName: System.Management.Automation.PSCustomObject

Name MemberType Definition
---- ---------- ----------
Equals Method System.Boolean Equals(Object obj)
GetHashCode Method System.Int32 GetHashCode()
GetType Method System.Type GetType()
ToString Method System.String ToString()
Hidden Share NoteProperty System.String Hidden Share=True
Servername NoteProperty System.String Servername=
Share NoteProperty System.String Share=
ShareDescription NoteProperty System.String ShareDescription=Default share
SharePath NoteProperty System.String SharePath=

Where $allservers[ 700 ] |gm returns:
TypeName: System.Management.Automation.PSCustomObject

Name MemberType
---- ----------
Equals Method
GetHashCode Method
GetType Method
ToString Method
DACL 1 NoteProperty
DACL 1 Permission NoteProperty
DACL 2 NoteProperty
DACL 2 Permission NoteProperty
DACL 3 NoteProperty
DACL 3 Permission NoteProperty
Hidden Share? NoteProperty
Servername NoteProperty
Share NoteProperty
ShareDescription NoteProperty
SharePath NoteProperty

Karl
ShayUser is Offline
Shell Enthusiast
Shell Enthusiast
Posts:68


07/24/2008 1:27 PM  
Karl,

I've never used subinacl before and its hard to tell with no sample output, but I can see that you are building custom objects on the fly and it can give you the flexibility you need. Because you can't tell how many DCLs each share has, I would combine all DACLs/'Permission NoteProperties' into one NoteProperty respectiviley (deleimited by some char) and populate only one NotePropertiy for each. This way your CSV file headers are fixed.

Shay Levy
Windows PowerShell MVP
http://blogs.microsoft.co.il/blogs/ScriptFanatic
glnsizeUser is Offline
Shell Enthusiast
Shell Enthusiast
Posts:60

07/24/2008 1:29 PM  

nope totaly did not read that right the first time... sorry about that!  I guess I got a little too used to answering that question.  try...

 

$allservers=@()
$obj = "" | Select-Object Servername, Share, SharePath, ShareDescription, HiddenShare, DACL
$obj.Servername = $servername
$obj.Share = $share.name
$obj.SharePath = $share.path
$obj.ShareDescription = $share.description
$obj.HiddenShare = $False"
$dacl = subacl /verbose /share $sharepath /display=dacl /testmode 
$dacl = $dacl -replace("`0","")
$dacl = $dacl -replace("`t","")
$line = New-Object System.Collections.ArrayList
$linenumber = 0
$daclnumber = 0
$obj2 = "" | select-object object, Perms
foreach ($line in $dacl)
{
    if ($line -match "/pace")
    {
        $who = $line -replace("/pace =","")
        $permission =  $dacl[$linenumber+2]
        $obj2.object = $who
        $obj2.Perms = $permission
    }
    $linenumber ++
}
$obj.DALC = $obj2

$allservers += $obj 

 

Heading home for the day... ran out of time so I havent test it.  I just changed your object creation method. by nesting $obj2 within $obj it doesn't matter how many DACL's are returned. Sorry I screwed that up earlier...

~glenn

KarlMitschkeUser is Offline
Power User
Power User
Posts:144

07/24/2008 1:32 PM  

Thanks, Shay & Glenn - I have worked around the issue by adding a final noteproperty "# of DACL's" which I can then sort on before exporting to a csv.

Karl

You are not authorized to post a reply.
Forums > Using PowerShell > General PowerShell > Array of Custom Objects - How to export to a csv



ActiveForums 3.7
right
   
footer Sponsored by Quest Software • SAPIEN Technologies • ShellTools, LLC • Microsoft Windows Server 2008 footer
footer