Pregunta

I have a number of user profiles that appear in my "Profiles Missing From Import" screens in CA (for operational reasons, we have disabled the Profile cleanup service, and are now investigating our own scripted replacement).

When I attempt to identify those same profiles using switch "-GetNonImportedObjects" in PowerShell, I get a far shorter list. This is clearly wrong - I can verify accounts that have been disabled in AD which still have an outstanding user profile in SharePoint, and they are not appearing in these PowerShell results.

Does anyone know why this might be? Or how I can identify and remove these accounts using PowerShell?

¿Fue útil?

Solución

If anyone is interested in this in the future, I found the following answer - courtesy of Bugra Postaci's blog (https://blog.bugrapostaci.com/2016/03/30/delete-inactive-users-in-user-profiles)

Solution in PowerShell:

#PowerShell Script – Delete Inactive User Profiles – SharePoint 2010/2013
# Credit to Bugra Postaci's blog (https://blog.bugrapostaci.com/2016/03/30/delete-inactive-users-in-user-profiles)

#The scripts is distributet "as-is." Use it on your own risk. The author give no warranties, guarantees or conditions.

if ((Get-PSSnapin "Microsoft.SharePoint.PowerShell" -ErrorAction SilentlyContinue) -eq $null) {
    Add-PSSnapin "Microsoft.SharePoint.PowerShell"
}

$site = Get-SPSite "<site url>"
$ctx = Get-SPServiceContext $site
$pm = New-Object Microsoft.Office.Server.UserProfiles.UserProfileManager($ctx)

$ProfileDB = Get-SPDatabase | ? { $_.Type -eq "Microsoft.Office.Server.Administration.ProfileDatabase"} 

$SqlConnection = New-Object System.Data.SqlClient.SqlConnection
$SqlConnection.ConnectionString = $ProfileDB.DatabaseConnectionString
$SqlCmd = New-Object System.Data.SqlClient.SqlCommand
$SqlCmd.CommandText = "select NTName,RecordId from UserProfile_Full where bDeleted=1"
$SqlCmd.Connection = $SqlConnection
$SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter
$SqlAdapter.SelectCommand = $SqlCmd
$DataSet = New-Object System.Data.DataSet
$SqlAdapter.Fill($DataSet)
$SqlConnection.Close()

Write-host "Total Count: " $DataSet.Tables[0].Rows.Count
Write-Host "Following Inactive Accounts will be deleted !"

foreach($user in $DataSet.Tables[0].Rows)
{
   write-host "Planning to delete :" $user["NTName"] -ForegroundColor Green
   $profile = $pm.GetProfile($user["RecordId"])
    #To enable delete operation remove comment out for below line
    #$pm.RemoveProfile($profile)
    #write-host $user["NTName"] is deleted!!! -ForegroundColor Red
}
write-host "Operation Completed !" 

Hope this helps.

Licenciado bajo: CC-BY-SA con atribución
scroll top