Xóa người dùng khỏi tất cả các trang web trong SharePoint bằng PowerShell
Làm cách nào để xóa người dùng khỏi tất cả tuyển tập trang trong SharePoint 2013 bằng PowerShell?
Đôi khi, chúng tôi có thể cần xóa một người dùng cụ thể khỏi tất cả tuyển tập trang. Ví dụ: một nhân viên rời công ty! Dưới đây là cách xóa người dùng khỏi tất cả các trang SharePoint bằng PowerShell:
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
Function Delete-UserFromAllSites([string]$WebAppURL, [string]$UserID)
{
#Get All Site collections
$SitesColl = Get-SPWebApplication $WebAppURL | Get-SPSite -Limit All
foreach($Site in $SitesColl)
{
write-host "Processing site:" $Site.RootWeb.URL
#Check if user Exists in the site collection
$User = $Site.RootWeb.SiteUsers | Where-Object {$_.LoginName -eq $UserID}
#If user account found
if($User -ne $null)
{
#Remove User from the Site
Remove-SPUser $UserID -web $Site.RootWeb.URL -confirm:$false
write-host "Removed user from site collection:"$Site.RootWeb.URL -f Green
}
}
}
#Variables for processing
$WebAppURL = "https://Portal.Crescent.com/"
$UserID="Crescent\DaveP"
#Call the function
Delete-UserFromAllSites $WebAppURL $UserID
Thao tác này sẽ quét và xóa người dùng SharePoint khỏi tất cả các tuyển tập trang trong ứng dụng web nhất định.
Để xóa nhiều người dùng khỏi tất cả các tuyển tập trang, chỉ cần lưu trữ tài khoản người dùng trong một mảng và gọi hàm PowerShell. Ví dụ.
#Array to store user accounts
$userNames= ("domain\user1", "global\user2", "domain\user3")
#Iterate through the array
foreach($user in $userNames)
{
#Call the function
Delete-UserFromAllSites "https://sharepoint.crescent.com" $user
}