views:

899

answers:

2

I am looking to do this step: 'Creating a New FTP Site by Editing the IIS 7.0 Configuration Files' with a batch file and was wondering if anybody has done this already?

http://learn.iis.net/page.aspx/301/creating-a-new-ftp-site/

A: 

Does this help?: http://blogs.iis.net/jaroslad/archive/2007/06/13/how-to-programatically-create-an-ftp7-site.aspx

Mike_G
That looks like it could be good. Is there a similar snippet written in c#?
Or as a batch file?
+1  A: 

Try this. You need to reference the COM component "AppHostAdminLibrary"

using AppHostAdminLibrary;

...

public void AddFtp7Site(String siteName, String siteId, String siteRoot) {
    String configPath;
    String configSectionName;
    var fNewSite = false;
    var fNewApplication = false;
    var fNewVDir = false;
    //
    // First setup the sites section
    //
    configPath = "MACHINE/WEBROOT/APPHOST";
    configSectionName = "system.applicationHost/sites";

    var adminManager = new AppHostAdminLibrary.AppHostWritableAdminManager();
    adminManager.CommitPath = configPath;

    try {
        var sitesElement = adminManager.GetAdminSection(configSectionName, configPath);
        IAppHostElement newSiteElement = null;

        //
        // check if site already exists
        //
        for (var i = 0; i < sitesElement.Collection.Count; i++) {
            var siteElement = sitesElement.Collection[i];
            if (siteElement.Properties["name"].Value.Equals(siteName) &&
                 siteElement.Properties["id"].Value.Equals(siteId)) {
                newSiteElement = siteElement;
                break;
            }
        }

        if (newSiteElement == null) {
            //
            // Site doesn't exist yet. Add new site node
            //

            newSiteElement = sitesElement.Collection.CreateNewElement("");
            newSiteElement.Properties["id"].Value = siteId;
            newSiteElement.Properties["name"].Value = siteName;
            fNewSite = true;
        }

        // setup bindings for the new site

        var ftpBindingString = "*:21:";

        var Bindings = newSiteElement.GetElementByName("bindings");
        var BindingElement = Bindings.Collection.CreateNewElement("");
        BindingElement.Properties["protocol"].Value = "ftp";
        BindingElement.Properties["bindingInformation"].Value = ftpBindingString;

        try {
            Bindings.Collection.AddElement(BindingElement, 0);
        }
        catch (Exception ex) {
            if (ex.Message != "") // ERROR_ALREADY_EXISTS ?
            {
                throw;
            }
        }

        IAppHostElement newApplication = null;
        //
        // check if root application already exists
        //
        for (var i = 0; i < newSiteElement.Collection.Count; i++) {
            var applicationElement = newSiteElement.Collection[i];

            if (applicationElement.Properties["path"].Value.Equals("/")) {
                newApplication = applicationElement;
                break;
            }
        }

        if (newApplication == null) {
            newApplication = newSiteElement.Collection.CreateNewElement("application");
            newApplication.Properties["path"].Value = "/";
            fNewApplication = true;
        }

        IAppHostElement newVirtualDirectory = null;
        //
        // search for the root vdir
        //
        for (var i = 0; i < newApplication.Collection.Count; i++) {
            var vdirElement = newApplication.Collection[i];
            if (vdirElement.Properties["path"].Value.Equals("/")) {
                newVirtualDirectory = vdirElement;
                break;
            }
        }
        if (newVirtualDirectory == null) {
            newVirtualDirectory = newApplication.Collection.CreateNewElement("");
            newVirtualDirectory.Properties["path"].Value = "/";
            fNewVDir = true;
        }
        newVirtualDirectory.Properties["physicalPath"].Value = siteRoot;
        if (fNewVDir) {
            newApplication.Collection.AddElement(newVirtualDirectory, 0);
        }
        if (fNewApplication) {
            newSiteElement.Collection.AddElement(newApplication, 0);
        }


        var ftpSiteSettings = newSiteElement.GetElementByName("ftpServer").GetElementByName("security").GetElementByName("authentication");

        Console.WriteLine("Enable anonymous authentication");

        var anonAuthSettings = ftpSiteSettings.GetElementByName("anonymousAuthentication");
        anonAuthSettings.Properties["enabled"].Value = "true";

        Console.WriteLine("Disable basic authentication");
        var basicAuthSettings = ftpSiteSettings.GetElementByName("basicAuthentication");
        basicAuthSettings.Properties["enabled"].Value = "false";

        BindingElement.Properties["bindingInformation"].Value = "*:21:";
        //
        // Time to add new site element and commit changes
        //
        if (fNewSite) {
            sitesElement.Collection.AddElement(newSiteElement, 0);
        }
        adminManager.CommitChanges();
    }
    catch (Exception ex) {
        Console.WriteLine("Error occured in AddDefaultFtpSite: " + ex.Message);
    }

    //
    // Add <authorization> section to allow everyone Read
    //
    Console.WriteLine("Enable everyone Read access");
    try {
        configPath = "MACHINE/WEBROOT/APPHOST/" + siteName;
        configSectionName = "system.ftpServer/security/authorization";

        var azSection = adminManager.GetAdminSection(configSectionName, configPath);

        azSection.Collection.Clear();

        var newAzElement = azSection.Collection.CreateNewElement("");
        newAzElement.Properties["accessType"].Value = "Allow";
        newAzElement.Properties["users"].Value = "*";
        newAzElement.Properties["permissions"].Value = "Read";

        azSection.Collection.AddElement(newAzElement, 0);
        adminManager.CommitChanges();
    }
    catch (Exception ex) {
        Console.WriteLine("Error occured while adding authorization section: " + ex.Message);
    }
}
TFD