Domanda

Prima di tutto scusate se questo è una domanda ovvia, ho cercato un sacco, ma non riesce a trovare la soluzione.

Quando apro un spsite in PowerShell in questo modo:

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

E poi cerco di ottenere tutti i ragnatele in questo modo

 $spsite.allwebs

ottengo il seguente errore:

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

Non capisco che cosa sto facendo male, sul mio altro assistente SP questo funziona bene.

Grazie in anticipo,

Modifica Per essere più chiaro circa il mio problema mi post 2 screenshots Qui potete vedere che io sono entrato come sp_admin e io sono amministratore principale della raccolta siti.

entrare descrizione dell'immagine qui

Ecco i comandi eseguo e l'errore che ottengo.

entrare descrizione dell'immagine qui

Modifica 2 Uscita $spsite.gettype:

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

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     False    SPSite                                   System.Object
È stato utile?

Soluzione

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.

Altri suggerimenti

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
  }
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a sharepoint.stackexchange
scroll top