以更好的方式编程安装IIS7

15

我有一个 Web 应用程序安装程序,它安装了所有的先决条件,其中包括 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;
}

目前为止,这段代码运行得非常完美。我唯一关心的是安装部分需要相当长的时间。

现在,我有机会重写一些代码并修改安装程序用户界面。我刚刚来到这个部分,并想知道是否只有从代码中安装IIS的解决方案,或者还有其他更好的方法我尚未发现?

我只是好奇还有哪些其他方式可以安装IIS。针对Windows 8的答案也会很受欢迎。

3个回答

12

未来最佳选择是使用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返回大量文本。 - Mike
@Mike 这是完全不同的问题。基本上它是一个字符串,如果你知道要期望什么,你可以检查它是否在其中,但我不太确定。 - i3arnon
好的,没什么不同 :) 我正在寻找以编程方式安装IIS的方法,但当该方法不返回错误代码时,它并不是非常可靠。我想做到这一点的方法是尝试解析字符串(如果知道其结构)或使用DismAPI.dll,但那是另外一个故事了。 - Mike
我已经添加了我的示例 https://dev59.com/o2Qo5IYBdhLWcg3wZusc#30390088 - PSR
1
@Thameem 这只是帖子中“Run”方法所在的类。您可以随意命名它。 - i3arnon
显示剩余6条评论

4
你有几个选择。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 等等 - John
自 Windows Server 2012 起,ServerManagerCmd.exe 已被弃用。 - AlwaysAProgrammer

3

我对提出的解决方案有些问题,因为我想安装更多的功能。应用程序会运行并完成,但我的应用程序会在等待process.WaitForExit()调用时挂起。

如果你的结果输出太大,只是一个提示给其他人。你应该运行类似这样的东西,而不是使用process.WaitForExit()

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

return results;

我需要在下一步中使用该输出结果,所以我将ReadLine()的结果写入并返回一个字符串。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接