0

im writing a simple function in order to list subfolders of a particular server share. My problem is that i want all informations in the same array of custom objects. Here is what i did:

function Get-tDollar {

    param(

        [string]$srv

    )

    $share = Get-WmiObject -ComputerName $srv -Class Win32_Share -Filter "Name='share`$'" | Select __SERVER,@{n="nPath";e={"\\" + $_.__SERVER + ($_.Path -replace "C:")}}

    $inc = 0

    Get-ChildItem $share.nPath | % {

        $inc++
        Add-Member -InputObject $share -MemberType NoteProperty -Name "folder_$inc" -Value $_.Name

    }

    $share


}

And i use this function like that: @("srv1","srv2") | % { Get-TDollar -srv $_ }

All works fine when the number of subfolders is the same, but when its different, the array contains only the number of folders listed in the first share. For example, for my two first servers, i have this output:

__SERVER                                                 nPath                                                    folder_1                                            
--------                                                 -----                                                    ------------                                            
srv1                                                 \\srv1\share                                     scripts                                                 
srv2                                                \\srv2\share                                     scripts 

But, because srv2 has more folders than the first server, i want this output:

__SERVER                                                 nPath                                                    repertoire_1                                            repertoire_2
--------                                                 -----                                                    ------------                                            ------------
srv1                                                 \\srv1\share                                     scripts                                                 
srv2                                                \\srv2\share                                     scripts                                     config

I know i can first calculate which server has the higher number of folders and then place it in fisrt position, but it seems there are enough lines for something like that. Is there a more efficient/elegant way to do that?

1 Answer 1

1

You're getting bitten by the default formatting gremlin:

http://blogs.msdn.com/b/powershell/archive/2006/04/30/586973.aspx

Get-tDollar  | 
 foreach { $_ | format-table } 
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks mjolinor but... :( Maybe im wrong, but i try to use Format-Table and what i obtain is an array of ... FormatStartData objects(?). I just want an array of custom objects, but with different sizes. Maybe it isnt possible without knowing the max number of properties for a custom object?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.