I've written code in C# to start and stop the IIS Pool. I've used ServerManager method and trying to user ServerManager().ApplicationPools but it is only showing below five application pools and not the pool I've in my IIS. My IIS version is 10.0.19041.1 . Below is the mentioned code.
IIS Pool I'm getting
"Clr4IntegratedAppPool" "Clr4ClassicAppPool" "Clr2IntegratedAppPool" "Clr2ClassicAppPool" "UnmanagedClassicAppPool"
private void PerformAction(string action)
{
// Get selected server and app pools
string selectedServer = ddlServers.SelectedValue;
List<string> selectedAppPools = new List<string>();
foreach (ListItem item in cblAppPools.Items)
{
if (item.Selected)
{
selectedAppPools.Add(item.Value);
}
}
// Perform start or stop action on selected app pools
foreach (string appPool in selectedAppPools)
{
try
{
using (ServerManager serverManager = new ServerManager())
{
Console.WriteLine($"Trying to access App Pool: {appPool}");
ApplicationPool selectedAppPool = serverManager.ApplicationPools[appPool];
if (action.Equals("start", StringComparison.OrdinalIgnoreCase))
{
selectedAppPool.Start();
}
else if (action.Equals("stop", StringComparison.OrdinalIgnoreCase))
{
selectedAppPool.Stop();
}
}
}
catch (Exception ex)
{
// Handle exception (log it, display it to the user, etc.)
}
}
// Refresh the UI or display a message to the user
LoadAppPools(selectedServer);
}
}
You can use this code as a reference: