Questo script
PowerShell permette di ciclare su tutte le site collection di tutte le web application di una farm
SharePoint 2010 ed elencare gli utenti che
non esistono più in
Active Directory.
Da come risultato un file CSV con l'elenco degli utenti da cancellare
#File: CheckIfUserExistInAD.ps1
#verifica se gli utenti della farm di ogni site collection esistono in AD
#esporta il risultato in un file .\CheckIfUserExistInAD.txt
$ErrorActionPreference = "Stop"
function ConvertTo-Sid ($NtAccount) {
(new-object system.security.principal.NtAccount($NTaccount)).translate([system.security.principal.securityidentifier])
}
$dic =@{}
Get-SPWebApplication | Get-SPSite -Limit ALL | foreach {
$w = $_.RootWeb
#elenco di tutti gli utenti della site collection
$users = $w.SiteUsers
write-host "$($w.url) - $($users.count)" -foregroundcolor green
$users | ? {$_.UserLogin -ne "SHAREPOINT\system"} | foreach {
$u = $_
if (($u.IsSiteAdmin -or $u.IsDomainGroup) -eq $false -and $u -match ".*\\.*"){
#se è un utente claims lo converto in formato dominio\nomeutente
$userlogin = $u.UserLogin.Replace("i:0#.w|", "")
if ($dic.Contains($u.UserLogin) -eq $false){
try {
#se risolvo il SID l'utente esiste
$sid = (ConvertTo-Sid $userlogin).Value.ToLower()
#write-host " Exist $u" -foregroundcolor green
} catch {
$dic.Add($u.UserLogin, $u.DisplayName)
write-host " $($u.UserLogin) : $($u.DisplayName)" -foregroundcolor red
}
}
}
}
$w.Dispose()
}
#scrivo su file CSV gli utenti da cancellare
$dic.GetEnumerator() | select Name, Value | sort Name | Export-Csv ".\CheckIfUserExistInAD.txt"