74 lines
1.3 KiB
PowerShell
74 lines
1.3 KiB
PowerShell
param(
|
|
[Parameter(Mandatory=$true)]
|
|
[string]$url,
|
|
[Parameter(Mandatory=$true)]
|
|
[string]$path
|
|
)
|
|
|
|
function Write-Log($msg)
|
|
{
|
|
Write-Host $msg -foregroundcolor green
|
|
($msg) | Out-File "log-delete-passive-users.txt" -Append
|
|
}
|
|
|
|
function Write-Warn($msg)
|
|
{
|
|
Write-Host $msg -foregroundcolor yellow
|
|
($msg) | Out-File "log-delete-passive-users.txt" -Append
|
|
}
|
|
|
|
function Is-User-Already-Added($loginName)
|
|
{
|
|
foreach ($u in $global:users)
|
|
{
|
|
if ($u.ToLower() -eq $loginName.ToLower())
|
|
{
|
|
return $true
|
|
}
|
|
}
|
|
return $false
|
|
}
|
|
|
|
|
|
|
|
$users = [System.IO.File]::ReadAllLines($path)
|
|
$wa = Get-SPWebApplication $url
|
|
foreach ($loginName in $users)
|
|
{
|
|
Write-Log ($loginName)
|
|
foreach ($site in $wa.Sites)
|
|
{
|
|
Write-Log (" " + $site.Url)
|
|
$user = $null
|
|
$web = $site.RootWeb
|
|
foreach ($u in $web.AllUsers)
|
|
{
|
|
if ($u.LoginName.ToLower() -eq $loginName.ToLower())
|
|
{
|
|
$user = $u
|
|
break
|
|
}
|
|
}
|
|
|
|
if ($user -eq $null)
|
|
{
|
|
foreach ($u in $web.SiteUsers)
|
|
{
|
|
if ($u.LoginName.ToLower() -eq $loginName.ToLower())
|
|
{
|
|
$user = $u
|
|
break
|
|
}
|
|
}
|
|
}
|
|
|
|
if ($user -eq $null)
|
|
{
|
|
Write-Warn (" User not found")
|
|
continue
|
|
}
|
|
|
|
Remove-SPUser $user.LoginName -Web $site.Url -Confirm:$false
|
|
Write-Log (" User is deleted")
|
|
}
|
|
} |