簡體   English   中英

以編程方式安裝IIS7的更好方法

[英]Better way to install IIS7 programmatically

我有一個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的答案也很受歡迎。

未來最好的選擇是使用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

你有兩個選擇。 Pkgmgr有效。 您可以使用ServerManagerCmd.exe(Windows Server),Dism.exe(較新的操作系統)並利用MS站點http://technet.microsoft.com/en-us/library/cc722041.aspx中的標志。

我建議線程化這個組件,如果可能的話,用進度通知/欄更新UI。 這樣,您的用戶就會知道事情正在取得進展。

Dism.exe應該適用於Windows 7,8,2008等。我會在安裝了這些操作系統的原始虛擬機上運行一些測試,拍攝快照然后運行安裝程序。 您可以隨意重新應用快照,並且您將能夠測試使軟件工作所需的所有標志。

我對提議的解決方案有點疑問,因為我希望安裝更多功能。 應用程序將運行,它將完成,但我的應用程序將等待process.WaitForExit()調用。

只是一個FYI給那里的其他人尋求答案。 如果你的結果輸出太大,而不是process.WaitForExit() ,你應該運行這樣的東西:

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

return results;

我在下一步中需要輸出,所以我將ReadLine()寫入我返回的字符串中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM