[英]Detecting what Server Roles are installed on Windows Server 2012
在 Windows Server 2008 中,您可以使用 WMI 和 Win32_ServerFeature 类以编程方式检测服务器功能和角色。
在 Windows Server 2012 中,Win32_ServerFeature 类已被弃用,并且不包括 2012 的新功能和角色。
据我所知Win32_ServerFeature类已被服务器管理器部署取代,并且没有关于如何使用它的示例。
我在网上搜索过,除了没有帮助的文档外,找不到任何关于它的信息。
任何帮助将不胜感激,我正在 4.5 点网络框架应用程序中使用 c# 进行开发。
我会考虑这样做的方法是使用一段 PowerShell 脚本,然后在 C# 中“播放”输出。
如果您添加对以下项目的引用,您将能够在 C# 中与 PowerShell 脚本进行交互:
系统.管理.自动化
然后使用以下using语句来深入研究 this 的功能并与之交互:
using System.Collections.ObjectModel;
using System.Management.Automation;
using System.Management.Automation.Runspaces
以下脚本将创建一个不错的子程序,它将采用 PowerShell 命令并返回一个可读字符串,并将每个项目(在本例中为一个角色)添加为一个新行:
private string RunScript(string scriptText)
{
// create a Powershell runspace then open it
Runspace runspace = RunspaceFactory.CreateRunspace();
runspace.Open();
// create a pipeline and add it to the text of the script
Pipeline pipeline = runspace.CreatePipeline();
pipeline.Commands.AddScript(scriptText);
// format the output into a readable string, rather than using Get-Process
// and returning the system.diagnostic.process
pipeline.Commands.Add("Out-String");
// execute the script and close the runspace
Collection<psobject /> results = pipeline.Invoke();
runspace.Close();
// convert the script result into a single string
StringBuilder stringBuilder = new StringBuilder();
foreach (PSObject obj in results)
{
stringBuilder.AppendLine(obj.ToString());
}
return stringBuilder.ToString();
}
然后,您可以将以下 PowerShell 命令传递给脚本并像这样接收输出:
RunScript("Import-module servermanager | get-windowsfeature");
或者,您可以从 C# 脚本运行此 PowerShell 命令,然后在脚本完成处理后从 C# 读取输出文本文件:
import-module servermanager | get-windowsfeature > C:\output.txt
希望这可以帮助!
我宁愿建议您检查 Windows 注册表。 例如对于 IIS 的组件,您可以检查HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\InetStp\\Components
文件夹
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.