簡體   English   中英

檢測 Windows Server 2012 上安裝了哪些服務器角色

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM