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,
breezbackAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

RubalJCommented:
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.
0
breezbackAuthor Commented:
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

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
harperseCommented:
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
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft IIS Web Server

From novice to tech pro — start learning today.