Search code examples
stringpowershellsharepointroles

Exporting System.Object[] to string in PowerShell


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.


Solution

  • 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 ', '