The application requires 3 parameters passed as arguments:
1) Authentication type: "w" (Windows authentication) or "f" (Forms authentication)
2) Domain name in case of Windows authentication or MembershipProvider name in case of Forms authentication
3) User name
using System;
using System.Web.Configuration;
using Microsoft.SharePoint.Administration;
using Microsoft.SharePoint;
namespace DeleteUserFromAllSiteCollections
{
class Program
{
static void Main(string[] args)
{
try
{
if (args == null || args.Length != 3)
{
throw new ArgumentException("Invalid parameters.");
}
string authenticationType = args[0];
string domainName = args[1];
string userName = args[2];
if (authenticationType.ToLower() != "w" && authenticationType.ToLower() != "f")
{
throw new ArgumentException("Invalid authentication type parameters.");
}
string longUserName = string.Empty;
AuthenticationMode authenticationMode;
if (authenticationType.ToLower() == "w")
{
longUserName=string.Format(@"{0}\{1}",domainName.ToUpper(),userName.ToLower());
authenticationMode = AuthenticationMode.Windows;
}
else
{
longUserName = string.Format(@"{0}:{1}", domainName.ToLower(), userName.ToLower());
authenticationMode = AuthenticationMode.Forms;
}
SPFarm farm = SPFarm.Local;
Console.WriteLine(string.Format("Connected with farm: {0}", farm.DisplayName));
SPWebService service = farm.Services.GetValue<SPWebService>("");
Console.WriteLine("Services has been retrieved.");
foreach (SPWebApplication webApp in service.WebApplications)
{
Console.WriteLine(string.Format("Connected with WebApp: {0}", webApp.DisplayName));
if (!webApp.IsAdministrationWebApplication)
{
foreach (SPUrlZone zone in webApp.IisSettings.Keys)
{
SPIisSettings setting = webApp.IisSettings[zone];
if (setting.AuthenticationMode == authenticationMode)
{
bool ok = true;
if (authenticationMode == AuthenticationMode.Forms)
{
if (setting.MembershipProvider.ToLower() != domainName.ToLower())
{
ok = false;
}
else
{
Console.WriteLine(string.Format("{0} installed.", domainName.ToLower()));
}
}
if(ok)
{
foreach (SPSite siteCollection in webApp.Sites)
{
Guid siteCollectionId = siteCollection.ID;
SPSecurity.RunWithElevatedPrivileges(delegate
{
using (SPSite elevatedSiteCollection = new SPSite(siteCollectionId))
{
Console.WriteLine(string.Format("Connected with SiteCollection: {0}", elevatedSiteCollection.Url));
Guid siteId = elevatedSiteCollection.RootWeb.ID;
using (SPWeb elevatedSite = elevatedSiteCollection.OpenWeb(siteId))
{
Console.WriteLine(string.Format("Connected with root site: {0}", elevatedSite.Url));
SPUser userToRemove = null;
try
{
userToRemove = elevatedSite.SiteUsers[longUserName];
}
catch { }
if (userToRemove != null)
{
elevatedSite.SiteUsers.RemoveByID(userToRemove.ID);
Console.WriteLine("{0} has been deleted.", longUserName);
}
else
{
Console.WriteLine("{0} not found.", longUserName);
}
}
}
});
}
}
}
}
}
}
}
catch (Exception ex)
{
Console.WriteLine(string.Format("Message: {0} {1} Stack:{2}", ex.Message, System.Environment.NewLine, ex.StackTrace));
}
}
}
}