如何以编程方式检查Windows Server 2008中是否安装了服务器功能?

时间:2009-03-10 20:56:53

标签: windows-server-2008

具体来说,如果安装了“桌面体验”功能,我正在使用仅在Server 2008上运行的应用程序,并且我希望该应用程序的安装程序验证它在那里。

我目前唯一知道的方法是运行ServerManagerCmd -query并解析输出;我更喜欢更轻量级的东西(比如检查注册表项)。

2 个答案:

答案 0 :(得分:8)

这是我在运行时使用它的代码片段。

public static bool isServerFeatureInstalled(Win32_ServerFeature_ID id)
{
    bool idFound = false;
    ConnectionOptions connectionOptions = new ConnectionOptions();
    ManagementScope managementScope =
        new ManagementScope(
            @"\\localhost\root\cimv2", connectionOptions);

    ObjectQuery oQuery =
        new ObjectQuery("SELECT Id FROM Win32_ServerFeature");
    ManagementObjectSearcher oSearcher =
        new ManagementObjectSearcher(managementScope, oQuery);
    ManagementObjectCollection oReturnCollection = oSearcher.Get();

    foreach (ManagementObject oReturn in oReturnCollection)
    {
        if ((uint) (oReturn["ID"]) == (uint) id)
        {
            return true;
        }
    }
    return idFound;
}

// short list of names and values taken from MSDN. 
public enum Win32_ServerFeature_ID
{
    Application_Server = 1,
    Web_Server = 2,
    Media_Server = 3,
    Windows_Sharepoint_Services = 4,
    Fax_Server = 5,
    File_Services = 6,
    Print_Services = 7,
    Active_Directory_Federation_Services = 8,
    Active_Directory_Lightweight_Directory_Services = 9,
    Active_Directory_Domain_Services = 10,
    UDDI_Services = 11,
    DHCP_Server = 12,
    DNS_Server = 13,
    Network_Policy_and_Access_Services = 14,
    Certificate_Server = 16,
    Active_Directory_Rights_Management_Services = 17,
    Terminal_Services = 18,
    Windows_Deployment_Services = 19,
    Failover_Clustering = 33,
    Network_Load_Balancing = 34,
    Desktop_Experience = 35,
    DOTNET_Framework_30 = 36,
}

答案 1 :(得分:0)

在我测试运行此命令的Windows 2008 x64 std服务器上(添加角色):

ServerManagerCmd.exe -install AS-AppServer-Foundation 

添加此注册表项:

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\AppServer

此命令(删除角色):

ServerManagerCmd.exe -remove AS-AppServer-Foundation

删除密钥。所以我认为这是一个很好的指标。 这些只是我自己的研究/实验的结果,并不是检测AppServer角色配置的官方/支持方式。