在Systemhell中将System.Object []导出为字符串

时间:2017-01-16 09:05:20

标签: string powershell sharepoint roles

我创建了一个脚本,它基本上遍历网站及其子网站中的所有列表,并列出每个文档或项目的权限。

但是,该脚本在写入CSV文件时,权限(成员+角色)字符串将被写为System.Object[],而不是CSV输出中的正确字符串(例如Member: user123 Role: Full Control)。

示例输出:

"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"

以下是剧本:

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

是否可以将用户名的System.Object []导出为CSV中的字符串?名称在GridView中显示为它们。

3 个答案:

答案 0 :(得分:2)

foreach语句返回一个数组,所以行

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

将System.Array分配给密钥“Permissions”。 System.Array的ToString()方法返回System.Object []。

您可以使用-join运算符将字符串数组“转换”为字符串。以下可行。

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

答案 1 :(得分:1)

有几种方法可以避免它。

  1. 使用–Join

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

    [pscustomobject]@{
        Value = (@(1,3,5,6) | Out-String).Trim()
    } | Export-Csv -notype output.csv
    
  3. 参考: Avoiding System.Object[] (or Similar Output) when using Export-Csv

答案 2 :(得分:0)

我所要做的就是:

$mystring = $myobject | Out-String