Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I am using this fine script to build a script, which iterates over all sitecollections in a web application. The purpose of this is, to list all users, with their permissions, and from which objected they did get it granted. For a start the raw information on this are sufficient for me, but the script is failing and I can not narrow down the source of the error.

This is the code I am using right now without the function above ommitted:

$siteList = get-spwebapplication 
foreach($wsite in $siteList)
{
    foreach ($SiteCollection in $wsite.sites)
    {
        write-host $SiteCollection -foregroundcolor Blue    
        foreach ($web in $SiteCollection.Allwebs)
        { 
            foreach ($wuser in $web.users)
            {
                $permsCollection += $web | Get-SPUserEffectivePermissions $wuser 
            }
            $web.Dispose()
        }
    }
}
#Display result in GridView
$permCollection | Out-GridView

This is the output I am getting:

SPSite Url=http://intranet
SPSite Url=http://intranet/cluster/es
SPSite Url=http://intranet/cluster/it
Exception has been thrown by the target of an invocation.
    + CategoryInfo          : NotSpecified: (:) [SecurityReport.ps1], TargetInvocationException
    + FullyQualifiedErrorId : System.Reflection.TargetInvocationException,SecurityReport.ps1

So at the site collection /cluster/it I am getting this error, which tells me just about nothing. The question is: Is there by any chane a way to tell, what the error actually states?

The Cluster site collections are for our Spanish and Italian colleagues - FYI. After this there are about 75 more to follow, so the error is almost bound to happen again.

If you need further information, I'll be happy to oblige.

//EDIT: From the ULS Logs:

01/15/2014 08:48:50.28  PowerShell_ISE.exe (0x0F00)                 0x16D0  SharePoint Foundation           PowerShell                      6tf0    Medium      Entering BeginProcessing Method of get-spwebapplication.    466d8510-e35d-4b3e-bbf2-d5701bd8b979
01/15/2014 08:48:50.28  PowerShell_ISE.exe (0x0F00)                 0x16D0  SharePoint Foundation           PowerShell                      6tf0    Medium      Leaving BeginProcessing Method of get-spwebapplication. 466d8510-e35d-4b3e-bbf2-d5701bd8b979
01/15/2014 08:48:50.28  PowerShell_ISE.exe (0x0F00)                 0x16D0  SharePoint Foundation           PowerShell                      6tf0    Medium      Entering ProcessRecord Method of get-spwebapplication.  466d8510-e35d-4b3e-bbf2-d5701bd8b979
01/15/2014 08:48:50.28  PowerShell_ISE.exe (0x0F00)                 0x16D0  SharePoint Foundation           PowerShell                      6tf0    Medium      Leaving ProcessRecord Method of get-spwebapplication.   466d8510-e35d-4b3e-bbf2-d5701bd8b979
01/15/2014 08:48:50.28  PowerShell_ISE.exe (0x0F00)                 0x16D0  SharePoint Foundation           PowerShell                      6tf0    Medium      Entering EndProcessing Method of get-spwebapplication.   
01/15/2014 08:48:50.28  PowerShell_ISE.exe (0x0F00)                 0x16D0  SharePoint Foundation           PowerShell                      6tf0    Medium      Leaving EndProcessing Method of get-spwebapplication.    
01/15/2014 08:48:50.28  PowerShell_ISE.exe (0x0F00)                 0x16D0  SharePoint Foundation           Database                        4ohp    High        Enumerating all sites in SPWebApplication Name=SharePoint - 80.  
01/15/2014 08:48:50.28  PowerShell_ISE.exe (0x0F00)                 0x16D0  SharePoint Foundation           Database                        4ohq    Medium      Site Enumeration Stack:    at Microsoft.SharePoint.SPBaseCollection.GetEnumerator()     at System.Management.Automation.LanguagePrimitives.GetEnumerator(Object obj)     at System.Management.Automation.foreachStatementNode.Execute(Array input, Pipe outputPipe, ArrayList& resultList, ExecutionContext context)     at System.Management.Automation.StatementListNode.ExecuteStatement(ParseTreeNode statement, Array input, Pipe outputPipe, ArrayList& resultList, ExecutionContext context)     at System.Management.Automation.StatementListNode.Execute(Array input, Pipe outputPipe, ArrayList& resultList, ExecutionContext context)     at System.Management.Automation.foreachStatementNode.Execute(Array input, Pipe outputPipe, ArrayList& resultList, ExecutionContext context)     at System.Management.Automa...  
01/15/2014 08:48:50.28* PowerShell_ISE.exe (0x0F00)                 0x16D0  SharePoint Foundation           Database                        4ohq    Medium      ...tion.StatementListNode.ExecuteStatement(ParseTreeNode statement, Array input, Pipe outputPipe, ArrayList& resultList, ExecutionContext context)     at System.Management.Automation.StatementListNode.Execute(Array input, Pipe outputPipe, ArrayList& resultList, ExecutionContext context)     at System.Management.Automation.ParseTreeNode.Execute(Array input, Pipe outputPipe, ExecutionContext context)     at System.Management.Automation.ScriptCommandProcessor.ExecuteWithCatch(ParseTreeNode ptn, Array inputToProcess)     at System.Management.Automation.ScriptCommandProcessor.RunClause(ParseTreeNode clause, Object dollarUnderbar, Object inputToProcess)     at System.Management.Automation.CommandProcessorBase.DoComplete()     at System.Management.Automation.Internal.PipelineProcessor.SynchronousExe...   
01/15/2014 08:48:50.28* PowerShell_ISE.exe (0x0F00)                 0x16D0  SharePoint Foundation           Database                        4ohq    Medium      ...cuteEnumerate(Object input, Hashtable errorResults, Boolean enumerate)     at System.Management.Automation.Runspaces.LocalPipeline.InvokeHelper()     at System.Management.Automation.Runspaces.LocalPipeline.InvokeThreadProc()     at System.Management.Automation.Runspaces.PipelineThread.WorkerProc()     at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)     at System.Threading.ThreadHelper.ThreadStart()     
01/15/2014 08:48:50.41  PowerShell_ISE.exe (0x0F00)                 0x16D0  SharePoint Foundation           General                         8e2s    Medium      Unknown SPRequest error occurred. More information: 0x80070005   
01/15/2014 08:48:50.41  PowerShell_ISE.exe (0x0F00)                 0x16D0  SharePoint Foundation           General                         8e2s    Medium      Unknown SPRequest error occurred. More information: 0x80070005  

//edit: for the sake of completeness: This is the code I ended up with. Feel free to use it and alter it in any way you like:

The script which I linked at the top has been altered. It does not return a PSObject anymore, but a string:

$hash = $resource + ";" + $so.GetType().Name + ";" + $loginName + ";" + ($permName -join ', ') + ";" + $assignment + "`r`n"  
return $hash

This is the iteration logic:

[Microsoft.SharePoint.SPSecurity]::RunWithElevatedPrivileges(
{
#Get all WebApplications
$sitelist = get-spwebapplication 
foreach($wsite in $sitelist)
{
    foreach ($SiteCollection in $wsite.sites)
    {
        #Write-Hoste for debugging purposes
        write-host $SiteCollection -foregroundcolor Blue    
        foreach ($web in $SiteCollection.Allwebs)
        { 
            foreach ($user in $web.users)
            {   
              $webPermissions = Get-SPWeb $web.Url | Get-SPUserEffectivePermissions $user
              $listPermissions = Get-SPWeb $web.Url -Limit All | %{$_.Lists |Get-SPUserEffectivePermissions $user}
              $webPermissions + $listPermissions | out-file -FilePath "Path/to/file/omitted"  -append

            }
            foreach ($group in $web.Groups)
            {
                foreach ($user in $group.users)
                {
                  $webPermissions = Get-SPWeb $web.Url | Get-SPUserEffectivePermissions $user
                  $listPermissions = Get-SPWeb $web.Url -Limit All | %{$_.Lists |Get-SPUserEffectivePermissions $user}
                  $webPermissions + $listPermissions | out-file -FilePath "Path/to/file/omitted"  -append
                }
            }
            $web.Dispose()
        }
    }
}
})

You can then easily read the csv with excel or Linqpad and do some magic with it. To read the csv with Linqpad I am using this line:

var csvData = from row in File.ReadLines(@"path to csv").Where(arg => !string.IsNullOrWhiteSpace(arg) && arg.Length > 0).AsEnumerable()
let lines = row.Split(';')
//...
select { ... }

A word of warning: If you try to read all the SPLists like I am, your csv might get VERY large, depending on the size of your farm and the iteration depth (webapp -> sitecollection -> SpWeb -> SPList)

share|improve this question
    
Did you check the SharePoint ULS logs at the time you run the script? My guess would be that you have different level of permission on some of the site collections. What account are you using to execute the script? How are you executing the script? via the Powershell console or from the cmd prompt (using the powershell command), etc. –  lwbecker2 Jan 14 at 18:54
    
I am executing the script via the Powershell ISE with a local administrator account. –  Serv Jan 15 at 7:16

1 Answer 1

up vote 1 down vote accepted

This error is a generic "Access denied" message. Does your user have access to all of the site collections and webs?

If not: You should grant that permission via a Web App policy, or run your code with elevated privileges:

[Microsoft.SharePoint.SPSecurity]::RunWithElevatedPrivileges(
{
    function GetPrivileges(){
         $siteList = get-spwebapplication 
         foreach($wsite in $siteList)
         {
             foreach ($SiteCollection in $wsite.sites)
             {
                 write-host $SiteCollection -foregroundcolor Blue    
                 foreach ($web in $SiteCollection.Allwebs)
                 { 
                     foreach ($wuser in $web.users)
                     {
                         $web | Get-SPUserEffectivePermissions $wuser
                     }
                     $web.Dispose()
                 }
             }
         }
    }

     #Display result in GridView
     GetPrivileges | Out-GridView
})
share|improve this answer
    
I just realised the same fact: In detail this means, that being farm administrator is not sufficient to run this. You need to be SiteCollection Administrator on every SC to run this. Robert, there is a syntax error in you code. A closing parenthesis is missing in your last line. –  Serv Jan 15 at 8:11
    
Fixed the syntax error! –  Robert Lindgren Jan 15 at 8:48
    
It definitly does work now. I just need to figure out, how to get the information into my $permsCollection because, this doesn't :> –  Serv Jan 15 at 8:52
    
Not fully sure, but you should declare your permsCollection outside of the foreaches to not get it overwritten all the time –  Robert Lindgren Jan 15 at 9:18
    
I have updated my code a bit, I do not have access to Get-SPUserEffectivePermissions so I do not know the output from it (I replaced it with web.Title just to try the output). –  Robert Lindgren Jan 15 at 9:29

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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