1

I'm pulling the Report for whole web application with the users who have permission. I can able to pull the data with below code. but I want to pull the department of the user in the last column. I have pulled login name and display name. and I tried with Department everywhere by replacing Department but I'm getting empty values in the last column. Please find the code below,

    Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue 

    Function GetUserAccessReport($WebAppURL, $FileUrl) 
    { 
        Write-Host "Generating permission report..." 

        #Get All Site Collections of the WebApp 
        $SiteCollections = Get-SPSite -WebApplication $WebAppURL -Limit All 

        #Write CSV- TAB Separated File) Header 
        "URL`tSite/List`tTitle/Name`tPermissionType`tPermissions `tLoginName `tDisplayName"| out-file $FileUrl 

        #Check Web Application Policies 
        $WebApp= Get-SPWebApplication $WebAppURL 

        foreach ($Policy in $WebApp.Policies)  
          { 
            $PolicyRoles=@() 
            foreach($Role in $Policy.PolicyRoleBindings) 
            { 
                $PolicyRoles+= $Role.Name +";" 
            } 

            "$($AdminWebApp.URL)`tWeb Application`t$($AdminSite.Title)`tWeb Application Policy`t$($PolicyRoles)`t$($Policy.UserName) `t$($Policy.DisplayName)" | Out-File $FileUrl -Append 
        } 

        #Loop through all site collections 
        foreach($Site in $SiteCollections)  
        { 
          #Check Whether the Search User is a Site Collection Administrator 
          foreach($SiteCollAdmin in $Site.RootWeb.SiteAdministrators) 
              { 
                "$($Site.RootWeb.Url)`tSite`t$($Site.RootWeb.Title)`tSite Collection Administrator`tSite Collection Administrator`t$($SiteCollAdmin.LoginName) `t$($SiteCollAdmin.DisplayName)" | Out-File $FileUrl -Append 
            } 

           #Loop throuh all Sub Sites 
           foreach($Web in $Site.AllWebs)  
           {     
                if($Web.HasUniqueRoleAssignments -eq $True)
                {

                    #Get all the users granted permissions to the list 
                    foreach($WebRoleAssignment in $Web.RoleAssignments )  
                    {  
                        #Is it a User Account? 
                        if($WebRoleAssignment.Member.userlogin)     
                        { 
                            #Get the Permissions assigned to user 
                            $WebUserPermissions=@() 
                            foreach ($RoleDefinition  in $WebRoleAssignment.RoleDefinitionBindings) 
                            { 
                                $WebUserPermissions += $RoleDefinition.Name +";" 
                            } 

                            #Send the Data to Log file 
                            "$($Web.Url)`tSite`t$($Web.Title)`tDirect Permission`t$($WebUserPermissions) `t$($WebRoleAssignment.Member.LoginName) `t$($WebRoleAssignment.Member.DisplayName)" | Out-File $FileUrl -Append 
                        } 
                        #Its a SharePoint Group, So search inside the group and check if the user is member of that group 
                        else   
                        { 
                            foreach($user in $WebRoleAssignment.member.users) 
                            { 
                                #Get the Group's Permissions on site 
                                $WebGroupPermissions=@() 
                                foreach ($RoleDefinition  in $WebRoleAssignment.RoleDefinitionBindings) 
                                { 
                                    $WebGroupPermissions += $RoleDefinition.Name +";" 
                                } 

                                #Send the Data to Log file 
                                "$($Web.Url)`tSite`t$($Web.Title)`tMember of $($WebRoleAssignment.Member.Name) Group`t$($WebGroupPermissions)`t$($user.LoginName) `t$($user.DisplayName)" | Out-File $FileUrl -Append 
                            } 
                        } 
                    } 
    }

                #********  Check Lists, Folders, and Items with Unique Permissions ********/ 
                foreach($List in $Web.lists) 
                { 
                    if($List.HasUniqueRoleAssignments -eq $True -and ($List.Hidden -eq $false))
                    { 
                        #Get all the users granted permissions to the list 
                        foreach($ListRoleAssignment in $List.RoleAssignments )  
                        {  
                            #Is it a User Account? 
                            if($ListRoleAssignment.Member.userlogin)     
                            { 
                                #Get the Permissions assigned to user 
                                $ListUserPermissions=@() 
                                foreach ($RoleDefinition  in $ListRoleAssignment.RoleDefinitionBindings) 
                                { 
                                    $ListUserPermissions += $RoleDefinition.Name +";" 
                                } 

                                #Send the Data to Log file 
                                "$($List.ParentWeb.Url)/$($List.RootFolder.Url)`tList`t$($List.Title)`tDirect Permission`t$($ListUserPermissions) `t$($ListRoleAssignment.Member.LoginName) `t$($ListRoleAssignment.Member.DisplayName)" | Out-File $FileUrl -Append 
                            } 
                            #Its a SharePoint Group, So search inside the group and check if the user is member of that group 
                            else   
                            { 
                                foreach($user in $ListRoleAssignment.member.users) 
                                { 
                                    #Get the Group's Permissions on site 
                                    $ListGroupPermissions=@() 
                                    foreach ($RoleDefinition  in $ListRoleAssignment.RoleDefinitionBindings) 
                                    { 
                                        $ListGroupPermissions += $RoleDefinition.Name +";" 
                                    } 

                                    #Send the Data to Log file 
                                    "$($List.ParentWeb.Url)/$($List.RootFolder.Url)`tList`t$($List.Title)`tMember of $($ListRoleAssignment.Member.Name) Group`t$($ListGroupPermissions)`t$($user.LoginName)`t$($user.DisplayName)" | Out-File $FileUrl -Append 
                                } 
                            }     
                        } 
                     }
              } 
            }     
        } 
    } 

#Call the function to Check User Access 
GetUserAccessReport "sitecollection URL" "C:\UsersList\OP.csv" 
Write-Host "Complete" 

Can Someone help getting the department for that user while pulling webapplication/site colection/ subsite/ list detailsetc. or syntax like "User.Department" I have tried that but its not working.

0

The RolAssignment member object returns SPUser object which doesn`t have a department property.

You have to get the department from the user profile properties.

Here is a sample PowerShell script which you can use to extend your code to get the department.

$mysite = Get-SPSite "my site url"
$profileContext = Get-SPServiceContext $mysite
$profileManager = New-Object Microsoft.Office.Server.UserProfiles.UserProfileManager($profileContext)

$up = $profileManager.GetUserProfile($user.LoginName)
$deperatment = $up["department"].Value

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.