首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >正在检测Windows Server 2012上安装了哪些服务器角色

正在检测Windows Server 2012上安装了哪些服务器角色
EN

Stack Overflow用户
提问于 2013-03-23 09:14:19
回答 2查看 5.1K关注 0票数 8

在Windows Server2008中,您可以使用WMI和Win32_ServerFeature类以编程方式检测服务器功能和角色。

在Windows Server 2012中,Win32_ServerFeature类已被弃用,并且不包括2012年的新功能和角色。

据我所知,Win32_ServerFeature类已经被Server Manager Deployment取代了,没有关于如何使用它的例子。

我已经在网上搜索过了,除了没有帮助的文档之外,我找不到任何关于它的信息。

任何帮助都将不胜感激,我正在用c#开发一个4.5Dot Net Framework应用程序。

EN

回答 2

Stack Overflow用户

发布于 2013-06-05 02:48:06

我考虑这样做的方式是使用一段PowerShell脚本,然后在C#中“播放”输出。

如果添加对以下项的引用,您将能够在C#中与PowerShell脚本交互:

System.Management.Automation

然后使用下面的using语句来深入研究它的特性并与之交互:

代码语言:javascript
运行
复制
using System.Collections.ObjectModel;
using System.Management.Automation;
using System.Management.Automation.Runspaces

下面的脚本将创建一个不错的sub,它将接受一个PowerShell命令并返回一个可读的字符串,并将每个项目(在本例中为一个角色)添加为一个新行:

代码语言:javascript
运行
复制
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命令传递给脚本并接收输出,如下所示:

代码语言:javascript
运行
复制
RunScript("Import-module servermanager | get-windowsfeature");

或者,您可以只从C#脚本运行此PowerShell命令,然后在脚本完成处理后从C#读取输出文本文件:

代码语言:javascript
运行
复制
import-module servermanager | get-windowsfeature > C:\output.txt

希望这能有所帮助!

票数 8
EN

Stack Overflow用户

发布于 2019-12-29 19:54:31

我建议您检查Windows注册表。例如,对于IIS的组件,您可以检查HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\InetStp\Components文件夹

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15582254

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档