gpt4 book ai didi

sharepoint - 使用 powershell 检索在线共享点的所有子站点

转载 作者:行者123 更新时间:2023-12-02 01:51:09 24 4
gpt4 key购买 nike

我正在尝试编写一个 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
}

虽然仍然无法获得权限...

最佳答案

对于 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 '-----------------------------'
}

关于sharepoint - 使用 powershell 检索在线共享点的所有子站点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22604076/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com