How can I programmatically stop or start a website in IIS (6.0 and 7.0) using MsBuild?

后端 未结 3 1384
我寻月下人不归
我寻月下人不归 2020-12-01 01:24

I have Windows Server 2003 (IIS 6.0) and Windows Server 2008 (IIS 7.0) servers, and I use MSBuild for deploying web applications.

I need to do a safe deploy, and do

相关标签:
3条回答
  • 2020-12-01 01:54

    By adding a reference to Microsoft.Web.Administration (which can be found inX:\Windows\System32\inetsrv, or your systems equivalent) you can achieve nice managed control of the situation with IIS7, as sampled below:

    namespace StackOverflow
    {
        using System;
        using System.Linq;
        using Microsoft.Web.Administration;
    
        class Program
        {
            static void Main(string[] args)
            {
                var server = new ServerManager();
                var site = server.Sites.FirstOrDefault(s => s.Name == "Default Web Site");
                if (site != null)
                {
                    //stop the site...
                    site.Stop();
                    if (site.State == ObjectState.Stopped)
                    {
                        //do deployment tasks...
                    }
                    else
                    {
                        throw new InvalidOperationException("Could not stop website!");
                    }
                    //restart the site...
                    site.Start();
                }
                else
                {
                    throw new InvalidOperationException("Could not find website!");
                }
            }
        }
    }
    

    Obviously tailor this to your own requirements and through your deployment build script execute the resulting application.

    Enjoy. :)

    0 讨论(0)
  • 2020-12-01 02:12
    • Write a script, e.g. PowerShell, which will stop/start IIS web site programmatically relying on command-line argument, e.g. start-stop.ps1 /stop 1

    • Put it into MsBuild script as a custom step


    Check this to find out how to restart IIS AppPool

    IIS WMI objects reference

    0 讨论(0)
  • 2020-12-01 02:13

    So you have your answer above for IIS7. What you're missing is IIS6. So here you go. This is using a COM interop object as that's all that is available for IIS 6. Also, because it's in vb, you'll have to figure out how to convert it. http://www.codeproject.com/Articles/16686/A-C-alternative-for-the-Visual-Basic-GetObject-fun should get you on the right track. you could also create a vb project just for this code but that's kind of silly.

    Dim WebServiceObj As Object
    dim IisSiteId as Integer = 0
    WebServiceObj = GetObject("IIS://localhost/W3SVC/" & IisSiteId)
    WebServiceObj.Stop()
    WebServiceObj.Start()
    
    0 讨论(0)
提交回复
热议问题