0
    [System.Reflection.Assembly]::LoadWithPartialName(“Microsoft.SharePoint”) > $null
 function EnumerateUserRolesPermissions ([string]$webURL){
 $site = new-object Microsoft.SharePoint.SPSite($webURL)
 $web = $site.OpenWeb()
 $webUsers = $web.Users
 $groups = $web.sitegroups
 foreach($webUser in $webUsers){
 $Permissions = $web.Permissions
 foreach($group in $groups)
 {
 foreach($Permission in $Permissions){
 if($webUser.ID -eq $Permission.Member.ID){
 foreach ($role in $webUser.Roles){
 if ($role.Type -ne [Microsoft.SharePoint.SPRoleType]::None)
 {
 write-output $webURL,“;“,$webUser.LoginName,“;“,$webUser.Name,“;",$role.Type.ToString(),";",$webUser.groups
 }
 }
 }
 if($group.ID -eq $Permission.Member.ID){
 foreach ($role in $group.Roles){
 if ($role.Type -ne [Microsoft.SharePoint.SPRoleType]::None)
 {
 foreach($user in $group.users){
 write-output $webURL,“;“,$user.LoginName,“;“,$user.Name,“;",$role.Type.ToString(),";",$group.name
 }
 }
 }
 }
 }
 }
 }
 }
 function EnumerateSiteUsers ()
 {
 [void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")
 $farm = [Microsoft.SharePoint.Administration.SPFarm]::Local
 foreach ($spService in $farm.Services) {
 if (!($spService -is [Microsoft.SharePoint.Administration.SPWebService])) {
 continue;
 }
 foreach ($webApp in $spService.WebApplications) {
 if ($webApp -is [Microsoft.SharePoint.Administration.SPAdministrationWebApplication]) { continue }
 $webAppUrl = $webApp.GetResponseUri('Default').AbsoluteUri
 foreach ($site in $webApp.Sites) {
 foreach ($web in $site.AllWebs) {
 EnumerateUserRolesPermissions $web.url
 }
 }
 }
 }
 }
EnumerateSiteUsers 

i am new to fairly new to sharepoint. i found this script that will list me all the sites and their users/groups in a farm. when i run this i get results in the manangment shell and its a long list. i am trying to get this export to a csv file. N i tried adding export-csv at the end and its not working. I tried few other answers i found on google and i end up getting empty files. Can anyone help me with this please?

0

Lots of ways to do this in PS but maybe the easiest is Out-File:

"$webURL,$($user.LoginName),$($user.Name),$($role.Type.ToString()),$($group.name)" | Out-File $logFile -Append

You could Create the file at the beginning of your script something like this (you may want to add a date stamp to the file name):

$log = "C:\path\to\log.csv"
if (!(Test-Path $log)) {
    $logFile = New-Item $log -ItemType file
    #add Headers if required:
    "WebUrl, UserName, Role, GroupName" | Out-File $logFile
}
else {
    Clear-Content $log
    $logFile = $log
    "WebUrl, UserName, Role, GroupName" | Out-File $logFile
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.