站点管理员值以CSV格式显示为Microsoft.SharePoint.SPUserCollection

时间:2017-02-14 14:01:20

标签: powershell sharepoint sharepoint-2013

我正在尝试从Web应用程序中提取特定的域用户,站点和网站集管理员,但是站点管理员列将以Microsoft.SharePoint.SPUserCollection的形式提取。请帮我用CSV格式获取它的值。

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue $currentLocation = (Get-Location).Path $outputReport = $currentLocation + "\" + "SharePointUsers2.csv"  $UserDataCollection = @()  $WebAppsColl = Get-SPWebApplication "https://Webapp.com" foreach($WebApp in $WebAppsColl) { Write-host "Scanning Web Application:"$WebApp.Name

$SitesColl = $WebApp.Sites
foreach ($Site in $SitesColl) 
{
    Write-host "Scanning Site Collection:"$Site.URL

    $WebsColl = $Site.AllWebs

        foreach ($web in $WebsColl) 
        {
            Write-host "Scanning Web:"$Web.URL

            $UsersColl = $web.AllUsers 

                foreach ($user in $UsersColl) 
                {
                       if($User.IsDomainGroup -eq $false -and $user -notlike "*XXXXXX*" -and $user -notlike "*global*" -and $user -notlike "*SHAREPOINT*" -and $user -notlike "*AUTHORITY*" -and $user -notlike "*windows*" -and $user -notlike "*312220573*" -and $user -notlike "*true*" -and $user -notlike "*KDS*") 
                        {
                            $UserData = New-Object PSObject

                            $UserData | Add-Member -type NoteProperty -name "UserLogin" -value $user.UserLogin.ToString()
                            $UserData | Add-Member -type NoteProperty -name "DisplayName" -value $user.displayName.ToString()
                            $UserData | Add-Member -type NoteProperty -name "E-mailID" -value $user.Email.ToString()
                            $userData | Add-Member -type NoteProperty -Name "Site" -Value $user.ParentWeb.Url.Tostring()
                            $scusers = $user.ParentWeb.SiteAdministrators
                            $siteadmin = @()


                            $userData | Add-Member -type NoteProperty -Name "SC Admins" -value $user.ParentWeb.SiteAdministrators.ToString()

                            $UserDataCollection += $UserData
                        }
                }
        $Web.dispose()
        }
     $site.dispose()
    }
}    

$UserDataCollection = $UserDataCollection   #|sort-object -Property  {$_.UserLogin } -Unique 


$UserDataCollection | Export-Csv -LiteralPath $OutputReport -NoTypeInformation

Write-host "Total Number of Unique Users found:"$UserDataCollection.Length

1 个答案:

答案 0 :(得分:1)

$userData | Add-Member -type NoteProperty -Name "SC Admins" -value $user.ParentWeb.SiteAdministrators.ToString()

这不会起作用,因为ToString()方法不会将数组转换为字符串,它只返回类型名称。

您基本上只需要使用$ user.ParentWeb.SiteAdministrators数组重复$ UsersColl的操作。

我没有SharePoint环境来测试它,但是这样的事情应该可行

$admins = @()
foreach($admin in $user.ParentWeb.SiteAdministrators){
  $admins += $admin.UserLogin.ToString()
}
$userData | Add-Member -type NoteProperty -Name "SC Admins" -value ($admins -join ";")

请注意,您不希望用逗号分隔生成的字符串,否则会破坏您的CSV,因此我使用了分号($admins -join ";")