SharePoint Server - Recycle / Stop-Start Application/Site pool using C#
As a continuation of my previous post
List All Servers in SharePoint Farm using C#
Now w'll see to Start/Stop Pools without login to the server each time.
{ Please check the NOTE at the very end on this post }
List All Application/Site Pools
/// <summary> /// Load Pools from the given server /// </summary> /// <param name="serverName">Server name</param> /// <param name="pool">Pool type</param> public void LoadPools(string serverName, PoolType pool) { var server = ServerManager.OpenRemote(serverInstance); if (pool == PoolType.AppPool) { appPoolCollection = new List<ApplicationPool>(); appPoolCollection = server.ApplicationPools.ToList(); } else if (pool == PoolType.SitePool) { siteCollection = new List<Site>(); siteCollection = server.Sites.ToList(); } }
Start/Stop Application Pool
/// <summary> /// Reset Application Pool /// </summary> /// <param name="serverName">Server name to be opened</param> /// <param name="resetAction">Start/Stop</param> /// <param name="applicationPool">Application pool name to perform action</param> /// <param name="message">Success/Exception message</param> /// <returns>true/false</returns> public bool ResetApplicationPool(string serverName, Action resetAction, string applicationPool, out string message) { bool IsSuccess = false; try { var server = ServerManager.OpenRemote(serverName); #region START/STOP LOGIC ApplicationPool pool = server.ApplicationPools[applicationPool]; if (pool != null) { try { if (pool.State != ObjectState.Unknown) { if (resetAction == Action.Stop) { if (pool.State == ObjectState.Started) { // Stoping attempt pool.Stop(); do { // sitting duck... } while (pool.State == ObjectState.Stopping); System.Threading.Thread.Sleep(3000); IsSuccess = true; } else if (pool.State == ObjectState.Stopped || pool.State == ObjectState.Stopping) { message = "Already in " + pool.State + " state"; } } if (resetAction == Action.Start) { if (pool.State == ObjectState.Stopped) { // Starting attempt pool.Start(); do { // sitting duck... } while (pool.State == ObjectState.Starting); IsSuccess = true; } else if (pool.State == ObjectState.Started || pool.State == ObjectState.Starting) { message = "Already in " + pool.State + " state"; } } } else { message = pool.Name + " - has unknown state"; } } catch (Exception x) { message = "unhandled exception: " + x.Message; } } else { message = applicationPool + "- does not exist in this server"; } #endregion } catch (Exception ex) { message = ex.Message; } return IsSuccess; }
Start/Stop Site Pool
/// <summary> /// Reset SitePool /// </summary> /// <param name="serverName">Server name to be opened</param> /// <param name="resetAction">Start/Stop</param> /// <param name="sitePool">Site pool name to perform action</param> /// <param name="message">Success/Exception message</param> /// <returns>true/false</returns> public bool ResetSitePool(string serverName, Action resetAction, string sitePool, out string message) { bool IsSuccess = false; try { var server = ServerManager.OpenRemote(serverName); Site site = server.Sites[sitePool]; #region START/STOP logic if (site != null) { try { // SitePool's App pool name // // site.Applications[0].ApplicationPoolName; // if (site.State != ObjectState.Unknown) { if (resetAction == Action.STOP) { if (site.State == ObjectState.Started) { //Stoping attempt site.Stop(); do { // sitting duck... } while (site.State == ObjectState.Stopping); System.Threading.Thread.Sleep(3000); IsSuccess = true; } else if (site.State == ObjectState.Stopped || site.State == ObjectState.Stopping) { resultpool.isAlready = true; resultpool.alreadyDescription = "Already in " + site.State + " state"; } } if (resetAction == Action.Start) { if (site.State == ObjectState.Stopped) { //Starting attempt site.Start(); do { // sitting duck... } while (site.State == ObjectState.Starting); IsSuccess = true; } else if (site.State == ObjectState.Started || site.State == ObjectState.Starting) { message = "Already in " + site.State + " state"; } } resultpool.CurretnState = site.State.ToString(); } else { message = site.Name + " - has unknown state"; } } catch (Exception ex) { message = "unhandled exception: " + ex.Message; } } else { message = siteInstance + " - does not exit in this server"; } #endregion } catch (Exception ex) { message = ex.Message; } return IsSuccess; }
🔔⏳🔶🔷 Note 🔷🔶⏳🔔
Have to host this solution as a website in IIS with the same Identity or Service Account used by SharePoint Application/Site Pools and host it in any one of the server in SharePoint farm. Then access it from anywhere using browser **[Highly recommended] Enable windows authentication for this website
-Ratsub
Comments
Post a Comment
Enter your comments..