2013-03-23 82 views
7

在Windows Server 2008中,你可以编程方式检测使用WMI和Win32_ServerFeature类服务器功能和角色。检测哪些服务器角色安装了Windows Server 2012

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

至于我可以告诉Win32_ServerFeature类已通过Server Manager Deployment更换和有如何没有例子用它。

我在网上搜索了再也找不到比这是没有帮助的文档上的其他任何信息。

任何援助,将不胜感激,我在C#中正在开发的4.5点NET框架中的应用。

回答

8

我会考虑这样做的方法是使用了一块PowerShell脚本,然后“玩”与C#中的输出。

如果您添加到以下项目的引用,你将能够在C#中使用PowerShell脚本进行交互:采用语句

System.Management.Automation

然后使用以下深入研究并与此特征互动:

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命令到脚本和receieve像这样的输出:

RunScript("Import-module servermanager | get-windowsfeature"); 

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

import-module servermanager | get-windowsfeature > C:\output.txt 

希望这会有所帮助!

+0

呀。我很容易就在PS中发现它。我很想实际上重新构建整个事情,做在PowerShell中..我。假定可以编译到一个EXE文件.. – 2013-06-04 23:20:40

+1

呀,何乐而不为!通过上述过程,您可以通过C#管理任何PowerShell命令,并且仍然可以选择一个良好的GUI,并且与PowerShell几乎无法与网络/ OS进行交互。任何你无法通过PowerShell实现的东西,你只需要使用C#的奇数位。 无论如何,我很高兴这工作! – chrismason954 2013-06-05 08:09:11

相关问题