Question

First of all sorry if this is an obvious question, I searched a lot but can't find the solution.

When I open an spsite in powershell like this:

 $spsite = Get-SPSite "https://adress"

And then I try to get all the webs like so

 $spsite.allwebs

I get the following error:

$spsite.allwebs : Exception has been thrown by the target of an invocation
+ CategoryInfo          : NotSpecified: (:) [], TargetInvocationExcept
+ FullyQualifiedErrorId : System.Reflection.TargetInvocationException

I don't understand what I am doing wrong, on my other SP server this works fine.

Thanks in advance,

Edit To be more clear about my problem I will post 2 screenshots Here you can see that I am logged in as sp_admin and I am primary admin in the site collection.

enter image description here

Here you see the commands I execute and the error I get.

enter image description here

Edit 2 $spsite.gettype output:

PS C:\Users\sp_admin> $spsite.GetType()

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     False    SPSite                                   System.Object
Was it helpful?

Solution

I think this is a straightforward permissions problem. Get-SPSite will allow you to retrieve a SPSite object (...at least a partial one) without having permissions to that site collection. But when you try to access the AllWebs collection it throws an error.

To validate this I create a new site collection and made someone other than me the primary and secondary site collection admins. I then ran Get-SPSite against that site collection and it return the SPSite object, but when I access the AllWebs collection it threw exactly the same error as you see above.

OTHER TIPS

All permissions required to correct this error:

  • Site Collection administrator on the SPSite (whether given through Central Admin or Site Settings)
  • PowerShell scripting admin: add-spshelladmin domain\username
  • PowerShell scripting admin on the content databases: get-spcontentdatabase | add-spshelladmin domain\username

note that the powershell commands must be run by the farm account (or someone else who already has these permissions)

The syntax is correct. Do you access to all sites in the site collection with the user that is executing the powershell prompt? Might be an access denied thing on one of the sites...

This code worked for me!

[Microsoft.SharePoint.SPSecurity]::RunWithElevatedPrivileges({
$Site = Get-SPSite $siteUrl
Foreach ($web in $Site.AllWebs)
{
#do something..
$web.Dispose()
}   
$Site.Dispose()
});  

See https://stackoverflow.com/questions/13142327/cannot-get-basic-sharepoint-powershell-script-to-run

I found this approach very reliable because the error message can also result from the site being locked from Central Administration. Elevating permissions in this scenario will still yield the error. Here is what worked for me.

try{
  $allWebs = ($site | Select-Object -ExpandProperty AllWebs)
  foreach($web in $allWebs){
    # do something
  }
} catch {
 if($_.Exception.Message -match "Access to this Web site has been blocked."){
    Write-Warning "`nSite Collection Locked: '{0}'`n" -f $site.Url
  } else {
    # something else is needed like script access to db or site collection full control
  }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top