【问题标题】:Better way to install IIS7 programmatically以编程方式安装 IIS7 的更好方法
【发布时间】:2013-04-11 08:04:21
【问题描述】:

我有一个 webapp 安装程序,它安装了它的所有先决条件,其中也包括 IIS 7。

由于 IIS 不是 Visual Studio 安装项目的先决条件,我想出了以下代码来从代码安装 IIS(针对 Windows Vista 和 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;
}

到目前为止,此代码运行良好。我唯一担心的是安装部分需要相当长的时间。

现在,我有机会重写一些代码并更改安装程序 UI。我刚刚来到这部分,想知道这是否是从代码安装 IIS 的唯一解决方案,还是我还没有找到更好的方法?

我只是想知道安装 IIS 的其他方法是什么。还感谢针对 Windows 8 的答案。

【问题讨论】:

    标签: c# .net iis-7


    【解决方案1】:

    未来的最佳选择是使用DISM(部署映像服务和管理)。这适用于Windows 7/Windows server 2008 R2 及更高版本。所有其他选项均已弃用。

    这是一个包含所需功能最少的代码示例(如果您需要不同的功能,您可以轻松添加更多功能):

    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
    

    【讨论】:

    • 那么获取 int 输出呢? dism 返回文本墙
    • 好吧,如果这是编程方式,那么如果您的 ProcessEx.Run() 的结果返回一个代码会很方便。 Dism 返回字符串对吗?如何轻松解析输出以了解安装成功/失败,如果失败,获取正确的错误代码?
    • @Mike 这是一个完全不同的问题。基本上它是一个字符串,如果你知道除了你可以检查它是否在那里,但我真的不知道。
    • 好的。没什么不同:) 我一直在寻找安装 IIS 的编程方式,但是当该方法不返回错误代码时它并不可靠。我想这样做的方法是尝试解析字符串(如果知道它的结构)或使用 DismAPI.dll 但那是另一回事。
    • @Thameem 这只是帖子中Run 方法所在的类。你可以随意命名它..
    【解决方案2】:

    这里有几个选项。 Pkgmgr 工作。您可以使用 ServerManagerCmd.exe (Windows Server)、Dism.exe(较新的操作系统)并利用 MS 站点 http://technet.microsoft.com/en-us/library/cc722041.aspx 中的标志。

    我会建议线程化这个组件,如果可能的话,用进度通知/栏更新 UI。这样你的用户就会知道事情正在进展。

    Dism.exe 应该适用于 Windows 7、8、2008 等。我会在安装了这些操作系统的原始 VM 上运行一些测试,拍摄快照,然后运行安装程序。您可以随意重新应用快照,您将能够测试使软件工作所需的所有标志。

    【讨论】:

    • Dism.exe /online /enable-feature /featurename:IIS-WebServerRole 等
    • ServerManagerCmd.exe 从 Windows Server 2012 起已弃用
    【解决方案3】:

    我对建议的解决方案有一些疑问,因为我希望安装更多功能。应用程序将运行并完成,但我的应用程序将挂起等待process.WaitForExit() 调用。

    对于其他寻求答案的人来说,仅供参考。如果您的结果输出太大,而不是 process.WaitForExit(),您应该运行如下内容:

    string results = "";
    while (!process.StandardOutput.EndOfStream)
    {
        results += process.StandardOutput.ReadLine();
    }
    
    return results;
    

    我在下一步需要该输出,因此我将ReadLine() 写入了我返回的字符串。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-14
      • 2011-06-13
      • 2011-01-28
      • 2014-10-05
      相关资源
      最近更新 更多