SharePoint Online:使用 PowerShell 从 CSV 文件将网站集管理员添加到多个网站
要求:从 CSV 文件将网站集管理员批量添加到多个 SharePoint Online 网站。
如果您像大多数 SharePoint 管理员一样,您可能希望向许多 SharePoint Online 网站添加网站集管理员,以快速向某人授予权限!那么,PowerShell 可以帮助您自动执行此任务。在这篇博文中,我们将向您展示如何结合使用 CSV 文件和 PowerShell 将网站集管理员添加到多个 SharePoint Online 网站。
如何使用 PowerShell 将网站集管理员添加到多个 SharePoint Online 网站?
以下是如何使用 PowerShell 将网站集管理员添加到多个网站:
Import-Module Microsoft.Online.SharePoint.PowerShell
#Parameters
$TenantAdminURL = "https://crescent-admin.sharepoint.com"
$CSVFilePath = "C:\Temp\SiteCollectionAdmin.csv"
Try {
#Connect to Admin Center
Connect-SPOService -Url $TenantAdminURL
#Get data from the CSV file
$CSVData = Import-Csv $CSVFilePath
#Iterate through each row in the CSV
ForEach($Row in $CSVData)
{
Try{
#Add Site collection Admin
Set-SPOUser -site $Row.SiteURL -LoginName $Row.SiteCollectionAdmin -IsSiteCollectionAdmin $True | Out-Null
Write-host "Added Site collection Administrator to $($Row.SiteURL)" -f Green
}
Catch {
write-host -f Yellow "`tError Adding Site Collection Admin to $($Row.SiteURL) :" $_.Exception.Message
}
}
}
Catch {
write-host -f Red "`tError:" $_.Exception.Message
}
这是我的 CSV 文件:
您可以从此处下载 CSV 文件:
PnP PowerShell 从 CSV 批量添加网站集管理员
同样,可以在 PnP PowerShell 中重写上述脚本,将网站集管理员添加到多个网站,如下所示:
#Parameters
$TenantAdminURL = "https://crescent-admin.sharepoint.com"
$CSVFilePath = "C:\Temp\SiteCollectionAdmin.csv"
Try {
#Connect to Admin Center
Connect-PnPOnline -Url $TenantAdminURL -Interactive
#Get data from the CSV file
$CSVData = Import-Csv $CSVFilePath
#Iterate through each row in the CSV
ForEach($Row in $CSVData)
{
Try{
#Add Site collection Admin
Set-PnPTenantSite -Url $Row.SiteURL -Owners $Row.SiteCollectionAdmin
Write-host "Added Site collection Administrator to $($Row.SiteURL)" -f Green
}
Catch {
write-host -f Yellow "`tError Adding Site Collection Admin to $($Row.SiteURL) :" $_.Exception.Message
}
}
}
Catch {
write-host -f Red "`tError:" $_.Exception.Message
}
这将节省您的时间并确保一致地添加您的网站集管理员。如果要将网站集管理员添加到租户中的所有 SharePoint Online 网站,请使用:如何将网站集管理员添加到租户中的所有 SharePoint Online 网站?
