以编程方式启用IIS角色和功能
本文关键字:角色 功能 IIS 启用 编程 方式 | 更新日期: 2023-09-27 17:50:41
我正在尝试通过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的更好方法
我终于找到原因了。在项目属性中,目标平台是"任意CPU"但选中了"首选32位"选项。这就导致了问题。在服务器2012 R2我的应用程序试图使用32位版本的DISM。这就是问题所在。
一旦我选中了这个选项。