9

I created a script that basically iterates through all lists in a site and its subsites and lists the permissions for each document or item.

The script works, however, when being written to the CSV file the permissions (Member + Role) string gets written as System.Object[] instead of a proper string in the CSV output (e.g. Member: user123 Role: Full Control).

Sample output:

"Common Hover Panel Actions", "https://#######.####.###.##", "https://######.######.#####.####", "System.Object[]", "                           Ministry for Finance", "Master Page Gallery", "12/22/2015 8:34:39 AM", "_catalogs/masterpage/Display Templates/Search/Item_CommonHoverPanel_Actions.js", "12/22/2015 8:34:39 AM", "6.2763671875"

The below is the script:

Add-PSSnapin Microsoft.SharePoint.PowerShell

function Get-DocInventory([string]$siteUrl) {
    $site = New-Object Microsoft.SharePoint.SPSite $siteUrl

    $i = 0;
    foreach ($web in $site.AllWebs) {

        $count = $site.AllWebs.Count
        foreach ($list in $web.Lists) {

            if ($list.BaseType -ne “DocumentLibrary”)
            {
               continue
            }

            foreach ($item in $list.Items) {
                $data = @{
                    "Web Application" = $web.ToString()
                    "Site" = $site.Url
                    "Web" = $web.Url
                    "list" = $list.Title
                    "Item URL" = $item.Url
                    "Item Title" = $item["Title"]
                    "Item Created" = $item["Created"]
                    "Item Modified" = $item["Modified"]
                    "File Size" = $item.File.Length/1KB
                    "Permissions" = foreach($roleAssignment in $item.RoleAssignments)
                    {
                        $Permission = ""

                        foreach($roleDefinition in $roleAssignment.RoleDefinitionBindings)
                        {
                            $Permission = $(foreach($roleAssignment in $item.RoleAssignments){$roleAssignment}
                            {
                                "Member: " +  $roleAssignment.Member.LoginName + " Role: " + $roleDefinition.Name + " "
                            } -join ',')

                            $Permission
                            Write-Progress -activity "Working" -status "Checked Sub-Site $i of $count" -percentComplete (($i / $count)/100)
                        }
                    }
                }
                New-Object PSObject -Property $data
            }
        }
        $web.Dispose();
    }
    $i++
    $site.Dispose()
}
<# Get-DocInventory "https://#####.####.###.##/" | Out-GridView #>
Get-DocInventory "https://#####.####.###.##/" | Export-Csv -NoTypeInformation -Path C:\Users\livob002-lap\Desktop\Permissions-FinanceIntranet.csv

Is it possible to export the System.Object[] for the username as string in the CSV? The names appear as they should in the GridView.

1

3 Answers 3

10

All I had to do was:

$mystring = $myobject | Out-String
Sign up to request clarification or add additional context in comments.

1 Comment

This made my life much easier for just converting a list of servers into a string I could attach in an email. Was having a bear of a time finding this elsewhere. :)
6

There are a few ways to avoid it.

  1. Using –Join

    [pscustomobject]@{
       Value = (@(1,3,5,6) -join ',')
    } | Export-Csv -notype output.csv
    
  2. Out-String and Trim()

    [pscustomobject]@{
        Value = (@(1,3,5,6) | Out-String).Trim()
    } | Export-Csv -notype output.csv
    

Reference: Avoiding System.Object[] (or Similar Output) when using Export-Csv

2 Comments

learn-powershell.net/2014/01/24/… is the article which talks about the details of this, I think we need to give credit to the author.
Lets give it now :)
4

The foreach statement returns an array, so the line

"Permissions" = foreach($roleAssignment in $item.RoleAssignments)

assigns a System.Array to the key "Permissions". The ToString() method for a System.Array returns System.Object[].

You can "convert" a string array to a string by using the -join operator. The following could work.

"Permissions" = foreach($roleAssignment in $item.RoleAssignments)
{
    ...
} -join ', '

5 Comments

You'll need to wrap the foreach block in a subexpression $(foreach($item in $list){$item})-join', ', for the -join operation to apply to the output
@Andreas Thanks however the output in the CSV is still the same. Upodated code
@MathiasR.Jessen Can you kindly explain further?
The foreach loop statement is not a value expression, and the last } effectively terminates the statement, so -join ', ' ends up just being a unary operation that outputs ,
@MathiasR.Jessen Thanks!

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.