在Windows Server2008中,您可以使用WMI和Win32_ServerFeature类以编程方式检测服务器功能和角色。
在Windows Server 2012中,Win32_ServerFeature类已被弃用,并且不包括2012年的新功能和角色。
据我所知,Win32_ServerFeature类已经被Server Manager Deployment取代了,没有关于如何使用它的例子。
我已经在网上搜索过了,除了没有帮助的文档之外,我找不到任何关于它的信息。
任何帮助都将不胜感激,我正在用c#开发一个4.5Dot Net Framework应用程序。
发布于 2013-06-05 02:48:06
我考虑这样做的方式是使用一段PowerShell脚本,然后在C#中“播放”输出。
如果添加对以下项的引用,您将能够在C#中与PowerShell脚本交互:
System.Management.Automation
然后使用下面的using语句来深入研究它的特性并与之交互:
using System.Collections.ObjectModel;
using System.Management.Automation;
using System.Management.Automation.Runspaces
下面的脚本将创建一个不错的sub,它将接受一个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
希望这能有所帮助!
发布于 2019-12-29 19:54:31
我建议您检查Windows注册表。例如,对于IIS的组件,您可以检查HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\InetStp\Components
文件夹
https://stackoverflow.com/questions/15582254
复制相似问题