使用powershell在线检索sharepoint的所有子网站

时间:2014-03-24 08:13:59

标签: sharepoint powershell

我试图编写一个powershell脚本来检索我们的SharePoint Online租赁中的所有网站集,它们的组和子网站。到目前为止,我已经能够获得每个组中的所有站点,他们的组和用户。我只是在获取子网站时遇到问题并且识别具有唯一权限的任何列表(并希望检索这些权限!)

到目前为止,我有这个来获取列表:

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" 

$password = Read-Host -Prompt "Enter password" -AsSecureString
$credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials("username", $password)

$siteURL = "https://site/sites/test"

$ctx = New-Object Microsoft.SharePoint.Client.ClientContext($siteUrl)  
$ctx.Credentials = $credentials 
$web = $ctx.Web

$lists = $web.Lists 
$ctx.Load($lists)
$ctx.ExecuteQuery()

foreach($list in $lists)
{
    Write-Host $list.Title
}

这是为了获取网站群组和用户:

Connect-SPOService –url https://site-admin.sharepoint.com


$sites = Get-SPOSite -Detailed

foreach ($site in $sites)
{

    Write-Host $site.Title
    $siteGroups = Get-SPOSiteGroup -Site $site.Url

    foreach ($group in $siteGroups)
        {
              $users = Get-SPOUser -Site $site.Url  -Group $group.Title -Limit All |ft -wrap 
              $url = $site.Url
              $groupName = $group.Title
              Write-Host $groupName + ' ' + $group.Users



        }


} 

因此,对于列表,我尝试使用$ list.HasUniqueRoleAssignments来确定返回的列表是否具有唯一权限,但即使有唯一权限,它也总是返回false。 对于子网站,我可以获得子网站的计数,但是如何获取URL?

提前致谢!

编辑:所以我已经能够使用我用于获取列表的客户端上下文代码获取子网站的URL:

$subs = $web.Webs
$ctx.Load($subs)
$ctx.ExecuteQuery()

foreach($sub in $subs)
{
    Write-Host $sub.Url
}

仍然坚持获得权限但是......

1 个答案:

答案 0 :(得分:2)

对于SharePoint Online(SPO),Microsoft发布了SharePoint Online Management Shell,其中包含Get-SPOSite cmdlet以返回一个或多个网站集。

示例:

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"

$AdminUrl = "https://tenant-admin.sharepoint.com/"
$UserName = "username@tenant.onmicrosoft.com"
$Password = "password"
$SecurePassword = $Password | ConvertTo-SecureString -AsPlainText -Force
$Credentials = New-Object -TypeName System.Management.Automation.PSCredential -argumentlist $userName, $SecurePassword

#Retrieve all site collection infos
Connect-SPOService -Url $AdminUrl -Credential $Credentials
$sites = Get-SPOSite 

如何通过PowerShell中的CSOM检索所有网站

由于SharePoint Online Management Shell不包含任何用于处理网站的cmdlet,因此我们会将CSOM API用于此目的。以下函数检索网站集中的所有网站:

function Get-SPOWebs(){
param(
   $Url = $(throw "Please provide a Site Collection Url"),
   $Credential = $(throw "Please provide a Credentials")
)

  $context = New-Object Microsoft.SharePoint.Client.ClientContext($Url)  
  $context.Credentials = $Credential 
  $web = $context.Web
  $context.Load($web)
  $context.Load($web.Webs)
  $context.ExecuteQuery()
  foreach($web in $web.Webs)
  {
       Get-SPOWebs -Url $web.Url -Credential $Credential 
       $web
  }
}

示例:在SPO中打印网站集的所有网站

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" 


$UserName = "username@tenant.onmicrosoft.com"
$Password = "password"
$SecurePassword = $Password | ConvertTo-SecureString -AsPlainText -Force
$SPOCredentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($UserName, $SecurePassword)

$AllWebs = Get-SPOWebs -Url 'https://tenant.sharepoint.com' -Credential $SPOCredentials
$AllWebs | %{ Write-Host $_.Title }

结果

通过结合这两种技术,您可以获得所需的结果:

#Retrieve all site collection infos
Connect-SPOService -Url $AdminUrl -Credential $Credentials
$sites = Get-SPOSite 

#Retrieve and print all sites
foreach ($site in $sites)
{
    Write-Host 'Site collection:' $site.Url     
    $AllWebs = Get-SPOWebs -Url $site.Url -Credential $SPOCredentials
    $AllWebs | %{ Write-Host $_.Title }   
    Write-Host '-----------------------------' 
}