更好的办法以编程方式安装IIS7办法、方式

2023-09-02 01:42:40 作者:狂飙的蜗牛

我有一个web应用程序的安装程序,安装所有的prerequisites,其中包括IIS 7了。

I have a webapp installer that installs all of its prerequisites, which includes IIS 7 too.

由于IIS不来像prerequisite在Visual Studio安装项目,我想出了以下code从code安装IIS(针对Windows Vista和7)。

Since IIS doesn't come as a prerequisite in a Visual Studio setup project, I came up with the following code to install IIS from code (targeting Windows Vista and 7).

private string ConfigureIIS7()
{
    string output = string.Empty;
    if (Environment.OSVersion.ToString().Contains("Microsoft Windows NT 5"))  // Its WindowsXP [with or without SP2]
    {
        MessageBox.Show("IIS 6.0 is not installed on this machine. Please install the same and proceed with the installation or contact your administrator","Installer",MessageBoxButtons .OK ,MessageBoxIcon .Warning);
        throw new System.Exception("IIS 6.0 is not installed on this machine.");
    }
    else
    {
        string CmdToExecute;
        CmdToExecute = "cmd /c start /w pkgmgr /l:log.etw /iu:IIS-WebServerRole;IIS-WebServer;IIS-CommonHttpFeatures;IIS-StaticContent;IIS-DefaultDocument;IIS-DirectoryBrowsing;IIS-HttpErrors;IIS-HttpRedirect;IIS-ApplicationDevelopment;IIS-ASPNET;IIS-NetFxExtensibility;IIS-ASP;IIS-CGI;IIS-ISAPIExtensions;IIS-ISAPIFilter;IIS-ServerSideIncludes;IIS-HealthAndDiagnostics;IIS-HttpLogging;IIS-LoggingLibraries;IIS-RequestMonitor;IIS-HttpTracing;IIS-CustomLogging;IIS-Security;IIS-BasicAuthentication;IIS-URLAuthorization;IIS-RequestFiltering;IIS-IPSecurity;IIS-Performance;IIS-HttpCompressionStatic;IIS-HttpCompressionDynamic;IIS-WebServerManagementTools;IIS-ManagementConsole;IIS-ManagementScriptingTools;IIS-ManagementService;IIS-IIS6ManagementCompatibility;IIS-Metabase;IIS-WMICompatibility;IIS-LegacyScripts;IIS-LegacySnapIn;WAS-WindowsActivationService;WAS-ProcessModel;WAS-NetFxEnvironment;WAS-ConfigurationAPI";
        Process prRunIIS = new Process();
        prRunIIS.StartInfo = new ProcessStartInfo("cmd.exe", CmdToExecute);
        prRunIIS.StartInfo.UseShellExecute = false;
        prRunIIS.StartInfo.RedirectStandardOutput = true;
        prRunIIS.StartInfo.CreateNoWindow = true;
        prRunIIS.Start();
        prRunIIS.WaitForExit();
        output = prRunIIS.StandardOutput.ReadToEnd();
    }
    return output;
}

这code已经完美工作至今。我只关注的安装部需要花费相当长的时间。

This code has worked perfectly so far. My only concern is that the installation part takes a considerable amount of time.

现在,我有机会改写一些codeS和改变安装程序的用户界面。我刚来到这个部分,不知道这是否是从code安装IIS唯一的解决办法,还是有可能会出现一些更好的方法我还没有发现?

Now, I have the opportunity to rewrite some of the codes and alter the installer UI. I just came to this part and wondered if this was the only solution to install IIS from code, or is there may be some better way I haven't found?

我只是好奇,想知道什么是安装IIS的其他方式。针对Windows 8的答案也是AP preciated。

I am just curious to know what are the other ways to install IIS. Answers targeted for Windows 8 are also appreciated.

推荐答案

最好的选择前进用的 DISM (部署映像服务和管理)。这适用于 Windows 7的 / 的Windows Server 2008 R2 以上。所有其他选项都是去precated。

The best option going forward is using DISM (Deployment Image Servicing and Management). This works on Windows 7/Windows server 2008 R2 and above. All other options are deprecated.

下面是与所需的最小功能的code样品(你可以很容易地添加更多,如果你需要不同的):

Here's a code sample with the minimum features needed (you can easily add more if you require different ones):

string SetupIIS()
{
    var featureNames = new [] 
    {
        "IIS-ApplicationDevelopment",
        "IIS-CommonHttpFeatures",
        "IIS-DefaultDocument",
        "IIS-ISAPIExtensions",
        "IIS-ISAPIFilter",
        "IIS-ManagementConsole",
        "IIS-NetFxExtensibility",
        "IIS-RequestFiltering",
        "IIS-Security",
        "IIS-StaticContent",
        "IIS-WebServer",
        "IIS-WebServerRole",
    };

    return ProcessEx.Run(
        "dism",
        string.Format(
            "/NoRestart /Online /Enable-Feature {0}",
            string.Join(
                " ", 
                featureNames.Select(name => string.Format("/FeatureName:{0}",name)))));
}           

static string Run(string fileName, string arguments)
{
    using (var process = Process.Start(new ProcessStartInfo
    {
        FileName = fileName,
        Arguments = arguments,
        CreateNoWindow = true,
        WindowStyle = ProcessWindowStyle.Hidden,
        RedirectStandardOutput = true,
        UseShellExecute = false,
    }))
    {
        process.WaitForExit();
        return process.StandardOutput.ReadToEnd();
    }
} 

这将导致下面的命令:

dism.exe /NoRestart /Online /Enable-Feature /FeatureName:IIS-ApplicationDevelopment /FeatureName:IIS-CommonHttpFeatures /FeatureName:IIS-DefaultDocument /FeatureName:IIS-ISAPIExtensions /FeatureName:IIS-ISAPIFilter /FeatureName:IIS-ManagementConsole /FeatureName:IIS-NetFxExtensibility /FeatureName:IIS-RequestFiltering /FeatureName:IIS-Security /FeatureName:IIS-StaticContent /FeatureName:IIS-WebServer /FeatureName:IIS-WebServerRole