6

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
7
  • I'm assuming the first powershell line should be $spsite = Get-SPSite "https://address", right?
    – user298
    Aug 3, 2011 at 12:31
  • @user298 yes sorry i will edit that ;)
    – nldev
    Aug 3, 2011 at 12:50
  • Please post full stack trace Aug 3, 2011 at 16:26
  • What does $spsite.gettype() outputs? Aug 5, 2011 at 12:24
  • @Janis Veinbergs Added the output to the main question.
    – nldev
    Aug 8, 2011 at 6:51

5 Answers 5

7

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.

6
  • 1
    Okay that is strange, when i change myself to be the primary site collection administrator i still get the same error. I am logged in under the correct account. Any ideas?
    – nldev
    Aug 4, 2011 at 12:10
  • Could be a remoting issue or maybe even the dreaded UAC. Are you running this on a SP server in the farm hosting the site you are running the Get-SPSite against or are you doing this remotely?
    – Rob D'Oria
    Aug 4, 2011 at 17:35
  • First of all thanks for the help, i uploaded some images in my main post, i am logged in as admin, i am local admin, i am primary site admin, i am running it on the machine that runs the central admin.
    – nldev
    Aug 5, 2011 at 9:58
  • 1
    It was a permissions problem, i logged in using sp_farm and everything worked fine. I think the difference was in the db_creator and db_securityadmin roles, but do not know for sure, but for future reference thry that!
    – nldev
    Aug 9, 2011 at 11:21
  • 4
    To use the shell the user must have special previliges. In SharePoint 2010 you can use the add-shelladmin cmdlt: technet.microsoft.com/en-us/library/ee806878.aspx#section3 I think your reference to the missing db permissions is the right way to go. Aug 10, 2011 at 14:34
3

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)

1
  • Just had the same issue, the third bullet here worked for me.
    – James Love
    Jul 10, 2014 at 11:24
0

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...

4
  • I run as the farm admin, is that the right way?
    – nldev
    Aug 3, 2011 at 12:51
  • Yeah, sounds good. Sorry, so I have no idea... Aug 3, 2011 at 13:08
  • x2 The syntax works on my box, displays all the webs. Did you get an incident ID with the error? Aug 3, 2011 at 13:48
  • Nope, this is the complete error as i got it. Any ideas?
    – nldev
    Aug 3, 2011 at 13:51
0

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

0

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
  }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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