We help IT Professionals succeed at work.

How to change default web site properties (path) with WMI on IIS 6.0 - C#?

Hi,

I'm trying to configure the default web site of my customer. The thing is ADS interface doesn't match because they don't have active directory.
They are working with IIS 6.0 on Windows Server 2003. According to what I read, I should be using WMI Interface which is quite similar.

I started with the code I found at Ramesh's Blog: http://blogs.msdn.com/ramesh_r/articles/170665.aspx

But I can't find a way to change properties for default web site. I just need to edit the default path of the default web site, set its ASP.NET version to 2 and Set the default Doc to my page.

Thanks,
Comment
Watch Question

Commented:
Hello,

Check out the following URL

http://blog.crowe.co.nz/category/11.aspx/rss

You'll find few sample C#/WMI scripts to manage your IIS there.
it uses Active Directory.

The answer I found is in fact just like Ramesh did, only that I must use it to add site and I have no need in Virtual Dirs nor App Pool.

I added to it the following code to set the asp.net version and  start the site (since it's stopped)
public void StartSite(string serverID)
        {
            try
            {
                string serverName = "W3SVC/" + serverID;
                ManagementObject webSite = new ManagementObject(_scope, new ManagementPath(@"IIsWebServer='" + serverName + "'"), null);
 
                App.StdOut("Starting..." + webSite.Path);
                // Check if the website is stopped or paused
                if ((int)webSite["ServerState"] == 4 || (int)webSite["ServerState"] == 6)
                {
                    webSite.InvokeMethod("Start", null);
                }
            }
            catch (Exception ex)
            {
                App.LogException(ex);
            }   
        }
 
        public void DeleteExistingSites()
        {
            try
            {
                ManagementObjectSearcher searcher = new ManagementObjectSearcher(_scope, new ObjectQuery("SELECT * FROM IIsWebServer"), null);
                
                ManagementObjectCollection webSites = searcher.Get();
                foreach (ManagementObject webSite in webSites)
                {
                    App.StdOut("Deleting... " + webSite.Path);
                    webSite.Delete();
                }
            }
            catch (Exception ex)
            {
                App.LogException(ex);
            }
        }
 
        public void SetAspNetVersion(string frameworkVersion, string IISWeb)
        {
            string frameworkDirectory;
            string directory;
            System.Collections.ArrayList al;
 
            //Execute the following command in the correct .net framework version directory.
            //Aspnet_regiis.exe -s W3SVC/1/ROOT/SampleApp1
            if (frameworkVersion != "v1.1.4322")
            {
                al = new System.Collections.ArrayList();
                directory = System.Runtime.InteropServices.RuntimeEnvironment.GetRuntimeDirectory();
 
                int dirPos = directory.Remove(directory.Length - 1, 1).LastIndexOf(@"\");
                frameworkDirectory = directory.Remove(dirPos, directory.Length - dirPos);
                frameworkDirectory = frameworkDirectory + @"\" + frameworkVersion + @"\";
 
                if (!Directory.Exists(frameworkDirectory))
                {
                    throw new Exception("Cannot find framework.  Check correct framework is installed");
                }
 
                Process process = new Process();
                process.StartInfo.UseShellExecute = false;
                process.StartInfo.RedirectStandardOutput = true;
                process.StartInfo.RedirectStandardError = true;
                process.StartInfo.FileName = frameworkDirectory + "aspnet_regiis";
                process.StartInfo.Arguments = @"-sn " + String.Format(@"W3SVC/{0}",IISWeb);
                process.Start();
                process.WaitForExit();
            }
        }

Open in new window

Commented:
Try the script center on microsoft.com for some sample vbscripts.  Here is the link:  http://www.microsoft.com/technet/scriptcenter/scripts/iis/iis6/default.mspx

You should be able to piece together what you need.

Best of luck,
harperse

Explore More ContentExplore courses, solutions, and other research materials related to this topic.