Sharepoint Online - 使用PowerShell列出sitecollection中的sharepoint站点

时间:2016-09-05 09:14:28

标签: powershell sharepoint-online

我想要使用powershell在sharepoint中的sitecollection中列出所有网站。我目前正在使用以下脚本:

Add-Type -Path "c:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.dll"  
Add-Type -Path "c:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"  

$siteUrl = “https://mytenant.sharepoint.com/sites/mysitecollection” 
$username = "myusername" 
$password = Read-Host -Prompt "Enter password" -AsSecureString  
$ctx = New-Object Microsoft.SharePoint.Client.ClientContext($siteUrl)  
$credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($username, $password)  
$ctx.Credentials = $credentials 

$rootWeb = $ctx.Web  
$sites  = $rootWeb.Webs 

$ctx.Load($rootWeb) 
$ctx.Load($sites) 
$ctx.ExecuteQuery() 

foreach($site in $sites) 
{ 
    $ctx.Load($site) 
    $ctx.ExecuteQuery() 

    Write-Host $site.Title "-" $site.Url  
} 

错误如下:

Exception calling " executeQuery " with 0 Argument ( s) : " The remote server returned an error : ( 401 ) Unauthorized . "
Line : 16 Char: 1

The collection has not been initialized. It has not been requested or the request has not been executed. It may need to be explicitly requested.
In Zeile:18 Zeichen:9
+ foreach($site in $sites)

有人可以帮帮我吗?

1 个答案:

答案 0 :(得分:0)

我的猜测是你不是网站集管理员。

实施此更改:

$sites = $rootWeb.GetSubwebsForCurrentUser($null)