110 lines
2.0 KiB
PowerShell
110 lines
2.0 KiB
PowerShell
param(
|
|
[Parameter(Mandatory=$true)]
|
|
[string]$url
|
|
)
|
|
|
|
function Write-Log($msg)
|
|
{
|
|
Write-Host $msg -foregroundcolor green
|
|
($msg) | Out-File "log-list-passive-users.txt" -Append
|
|
}
|
|
|
|
function Write-Warn($msg)
|
|
{
|
|
Write-Host $msg -foregroundcolor yellow
|
|
($msg) | Out-File "log-list-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
|
|
}
|
|
|
|
function Is-User-Active($loginName)
|
|
{
|
|
$wa = Get-SPWebApplication $url
|
|
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)
|
|
{
|
|
continue
|
|
}
|
|
|
|
if ($user.Groups.Count -gt 0)
|
|
{
|
|
Write-Log (" User has groups")
|
|
return $true
|
|
}
|
|
}
|
|
return $false
|
|
}
|
|
|
|
function List-Passive-Users($site)
|
|
{
|
|
Write-Log ($site.Url)
|
|
#Write-Log ("Users length before: " + $global:users.Length)
|
|
$web = $site.RootWeb
|
|
foreach ($u in $web.AllUsers)
|
|
{
|
|
Write-Log (" " + $u.LoginName)
|
|
if (-not $u.LoginName.ToLower().StartsWith("i:0#.f|taloyhtio|"))
|
|
{
|
|
Write-Warn (" Skip windows users")
|
|
continue
|
|
}
|
|
|
|
$added = Is-User-Already-Added $u.LoginName
|
|
if ($added)
|
|
{
|
|
Write-Warn (" User already added")
|
|
continue
|
|
}
|
|
|
|
$isActive = Is-User-Active $u.LoginName
|
|
if ($isActive)
|
|
{
|
|
Write-Warn (" User is active. Skip it")
|
|
continue
|
|
}
|
|
|
|
$global:users += $u.LoginName
|
|
Write-Log (" User is added")
|
|
}
|
|
#Write-Log ("Users length after: " + $global:users.Length)
|
|
}
|
|
|
|
$global:users = @()
|
|
$webApp = Get-SPWebApplication $url
|
|
$webApp.Sites | ForEach-Object { List-Passive-Users $_ }
|
|
$global:users | ForEach-Object { $_ | Out-File "passive-users.txt" -Append } |