2

我正在尝试通过 c# 控制台应用程序启用 IIS 功能。它在 Windows 7 和 Windows 8.1 机器上运行良好。但是当我在 windows server 2008 R2 和 windows server 2012 R2 上运行相同的代码时,它不起作用。我在这段代码中缺少什么?

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Management;
using System.Text.RegularExpressions;

namespace EnableIISComponents
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                SetupIIS();
                Console.WriteLine("Done. Press any key to close.");
            }
            catch (Exception ex)
            {
                Console.WriteLine("Exception occurred:" + ex.Message);
            }
            Console.ReadLine();
        }
    static string SetupIIS()
    {
        // In command prompt run this command to see all the features names which are equivalent to UI features.
        // c:\>dism /online /get-features /format:table 
        var featureNames = new List<string> 
                {
                    "IIS-ApplicationDevelopment",                        
                    "IIS-ISAPIExtensions",
                    "IIS-ISAPIFilter",
                    "IIS-CommonHttpFeatures",
                    "IIS-DefaultDocument",
                    "IIS-HttpErrors",
                    "IIS-StaticContent",
                    "IIS-HealthAndDiagnostics",
                    "IIS-HttpLogging",
                    "IIS-HttpTracing",
                    "IIS-WebServer",
                    "IIS-WebServerRole",
                    "IIS-ManagementConsole",
                };

        Console.WriteLine("Checking the Operating System...\n");

        ManagementObjectSearcher obj = new ManagementObjectSearcher("select * from Win32_OperatingSystem");
        try
        {
            foreach (ManagementObject wmi in obj.Get())
            {
                string Name = wmi.GetPropertyValue("Caption").ToString();

                // Remove all non-alphanumeric characters so that only letters, numbers, and spaces are left.
                // Imp. for 32 bit window server 2008
                Name = Regex.Replace(Name.ToString(), "[^A-Za-z0-9 ]", "");

                if (Name.Contains("Server 2012 R2") || Name.Contains("Windows 81"))
                {
                    featureNames.Add("IIS-ASPNET45");
                    featureNames.Add("IIS-NetFxExtensibility45");
                }
                else if (Name.Contains("Server 2008 R2") || Name.Contains("Windows 7"))
                {
                    featureNames.Add("IIS-ASPNET");
                    featureNames.Add("IIS-NetFxExtensibility");
                }
                else
                {
                    featureNames.Clear();
                }

                string Version = (string)wmi["Version"];
                string Architecture = (string)wmi["OSArchitecture"];

                Console.WriteLine("Operating System details:");
                Console.WriteLine("OS Name: " + Name);
                Console.WriteLine("Version: " + Version);
                Console.WriteLine("Architecture: " + Architecture + "\n");
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine("Exception occurred:" + ex.Message);
        }

        return 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)
    {
        Console.WriteLine("Enabling IIS features...");
        Console.WriteLine(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 命令,则 IIS 功能将被启用。为什么不使用该程序?

我通过右键单击并选择“以管理员身份运行”以管理员身份运行我的程序。

我创建了此示例,并从此链接获取帮助。 以编程方式安装 IIS7 的更好方法

4

1 回答 1

3

最后我找到了原因。在项目属性中,目标平台是“任何 CPU”,但选中了“首选 32 位”选项。这导致了问题。在服务器 2012 R2 中,我的应用程序尝试使用 32 位版本的 DISM。这就是问题所在。

一旦我取消选中该选项。它工作正常。

于 2015-05-26T04:47:33.670 回答