【问题标题】:Detecting what Server Roles are installed on Windows Server 2012检测 Windows Server 2012 上安装了哪些服务器角色
【发布时间】:2013-03-23 01:14:18
【问题描述】:

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

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

据我所知,Win32_ServerFeature 类已被 Server Manager Deployment 替换,并且没有关于如何使用它的示例。

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

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

【问题讨论】:

    标签: c# .net windows-server-2012


    【解决方案1】:

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

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

    系统管理自动化

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

    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
    

    希望这会有所帮助!

    【讨论】:

    • 是的。我很容易在PS中找到它。我很想真正重新架构整个事情并在 Powershell 中完成。假设我可以将其编译成一个 Exe 文件..
    • 是的,为什么不呢!使用上述过程,您可以通过 C# 管道任何 PowerShell 命令,并且仍然可以选择漂亮的 GUI,此外,与网络/操作系统交互的选项几乎是 PowerShell 的无穷无尽。任何你无法通过 PowerShell 实现的东西,你只需要使用一些奇怪的 C#。我很高兴这仍然有效!
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-04-23
      • 2018-03-16
      • 2014-05-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多