ลบผู้ใช้ออกจากไซต์ทั้งหมดใน SharePoint โดยใช้ PowerShell
จะลบผู้ใช้ออกจากคอลเลกชันไซต์ทั้งหมดใน SharePoint 2013 โดยใช้ PowerShell ได้อย่างไร
บางครั้ง เราอาจจำเป็นต้องลบผู้ใช้รายใดรายหนึ่งออกจากคอลเลกชันไซต์ทั้งหมด เช่น พนักงานลาออกจากบริษัท! ต่อไปนี้เป็นวิธีลบผู้ใช้ออกจากไซต์ SharePoint ทั้งหมดโดยใช้ 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
การดำเนินการนี้จะสแกนและลบผู้ใช้ SharePoint ออกจากคอลเลกชันไซต์ทั้งหมดในแอปพลิเคชันเว็บที่กำหนด
เมื่อต้องการลบผู้ใช้หลายรายออกจากไซต์คอลเลกชันทั้งหมด เพียงจัดเก็บบัญชีผู้ใช้ในอาร์เรย์แล้วเรียกใช้ฟังก์ชัน PowerShell เช่น.
#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
}