I would like to use DirectoryServices to list and recycle App Pools hosted on any machine in my Workgroup. My approach is similar to some of the answers posted to this question,but in my case I'd like to do this for a remote machine running IIS 6.
I'm prototyping this as a console app but will eventually be providing a web interface to allow recycling a selected app pool for a specified machine. Where can I specify the credentials to use for making Directory Services call to a remote machine.
Here is the code I'm using to retrieve App pools, it works for localhost but not for remote machines.
public class ApplicationPool
{
public string Name { get; set; }
}
class AppPoolManager
{
private DirectoryEntry GetDirectoryEntry(string Path)
{
DirectoryEntry root = null;
root = new DirectoryEntry(Path);
return root;
}
public ApplicationPool[] GetApplicationPools(string environmentUserDomainName)
{
DirectoryEntry root = this.GetDirectoryEntry("IIS://" + environmentUserDomainName + "/W3SVC/AppPools");
if (root == null)
return null;
List<ApplicationPool> Pools = new List<ApplicationPool>();
foreach (DirectoryEntry Entry in root.Children)
{
PropertyCollection Properties = Entry.Properties;
ApplicationPool Pool = new ApplicationPool();
Pool.Name = Entry.Name;
Pools.Add(Pool);
}
return Pools.ToArray();
}
public void RecycleApplicationPool(string machineName,string AppPoolName)
{
DirectoryEntry root = this.GetDirectoryEntry("IIS://" + machineName + "/W3SVC/AppPools");
DirectoryEntry AppPool = root.Invoke("Recycle", "IIsApplicationPool", AppPoolName) as DirectoryEntry;
AppPool.CommitChanges();
}}
I hope I'm phrasing this correctly.