如何检查是否已安装IIS 6管理兼容性功能?

时间:2010-04-23 21:58:54

标签: c# vb.net iis iis-7 vbscript

使用VB.NET,C#或VBScript,如何检查运行IIS 7.x的计算机上是否已安装IIS 6管理兼容性功能及其子功能?

1 个答案:

答案 0 :(得分:8)

我使用Registry Workshop的试用版(比较注册表功能)进行了一些测试,发现了以下内容:

如果安装了IIS 7.x,则以下注册表项包含有关已安装子组件的信息:

HKEY_LOCAL_MACHINE \ SOFTWARE \微软\ InetStp \组件

每个已安装的功能都使用值DWORD 0x00000001表示。如果未安装某个功能,则该值将丢失。

对于Web管理工具,值名称如下:

Web Management Tools
  IIS 6 Management Compatibility
    IIS 6 Management Console                              (LegacySnapin)
    IIS 6 Scripting Tools                                 (LegacyScripts)
    IIS 6 WMI Compatibility                               (WMICompatibility)
    IIS Metabase and IIS 6 configuration compatibility    (Metabase + ADSICompatibility)

  IIS Management Console                                  (ManagementConsole)
  IIS Management Scripts and Tools                        (ManagementScriptingTools)
  IIS Management Service                                  (AdminService)

请注意,这些组件名称来自Windows 7安装,可能与Windows Server 2008略有不同,但注册表项应该相同。

本文的注释中提到了其中一些内容: Using Managed Code to Detect if IIS is Installed and ASP/ASP.NET is Registered

可以在此处找到这些和其他子组件的列表: Discover Installed Components

更新

最终代码中的一些核心功能。这不是完整的代码,但对于那些花时间查找各种IIS版本的组件名称的人来说应该足够了:

Function IsIISComponentInstalled(ByVal ComponentName)
    Dim result
    Dim intProcessorArchitecture
    intProcessorArchitecture = GetProcessorArchitectureIIS()
    If intProcessorArchitecture = 64 Then
        '64-bit system
        On Error Resume Next
        Err.Clear
        result = RegReadDWORD(HKEY_LOCAL_MACHINE, "SOFTWARE\Microsoft\InetStp\Components", ComponentName, 64)
        If Err.Number <> 0 Then
            Err.Clear
            IsIISComponentInstalled = False
        Else
            If result = 1 Then
                IsIISComponentInstalled = True
            Else
                IsIISComponentInstalled = False
            End If
        End If
    Else
        '32-bit system
        If RegReadStringIIS("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\InetStp\Components\" & ComponentName) = "1" Then
            IsIISComponentInstalled = True
        Else
            IsIISComponentInstalled = False
        End If
    End If

End Function

Function GetProcessorArchitectureIIS()
    Dim strProcessorArchitecture
    Dim oShell

    Set oShell = CreateObject("Wscript.Shell")
    strProcessorArchitecture = oShell.RegRead("HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment\PROCESSOR_ARCHITECTURE")

    If strProcessorArchitecture = "x86" Then
        GetProcessorArchitectureIIS = 32
    Else
        If strProcessorArchitecture = "AMD64" Then
            GetProcessorArchitectureIIS = 64
        Else
            GetProcessorArchitectureIIS = 0
        End If
    End If

End Function

Function RegReadStringIIS(sRegValue)
    Set oShell = CreateObject("WScript.Shell")
    On Error Resume Next
    RegReadStringIIS = oShell.RegRead(sRegValue)
    If Err Then
        RegReadStringIIS = ""
        Err.clear
    End If
    If VarType(RegReadStringIIS) < vbArray Then
        If RegReadStringIIS = sRegValue Then
            RegReadStringIIS = ""
        End If
    End If
    On Error Goto 0
End Function

'-------------------------------------------------------------------
' Reads a REG_SZ value from the local computer's registry using WMI.
' Parameters:
'   RootKey - The registry hive (see http://msdn.microsoft.com/en-us/library/aa390788(VS.85).aspx for a list of possible values).
'   Key - The key that contains the desired value.
'   Value - The value that you want to get.
'   RegType - The registry bitness: 32 or 64.
'
'References:
'   http://stackoverflow.com/questions/1229760/how-do-i-read-64-bit-registry-values-from-vbscript-running-as-a-an-msi-post-inst
'   http://msdn.microsoft.com/en-us/library/aa393067(VS.85).aspx
'   http://msdn.microsoft.com/en-us/library/windows/desktop/aa390445(v=VS.85).aspx
'
Function RegReadDWORD(RootKey, Key, Value, RegType)
    Dim oCtx, oLocator, oReg, oInParams, oOutParams
    Set oCtx = CreateObject("WbemScripting.SWbemNamedValueSet")
    oCtx.Add "__ProviderArchitecture", RegType
    Set oLocator = CreateObject("Wbemscripting.SWbemLocator")
    Set oReg = oLocator.ConnectServer("", "root\default", "", "", , , , oCtx).Get("StdRegProv")
    Set oInParams = oReg.Methods_("GetDWORDValue").InParameters
    oInParams.hDefKey = RootKey
    oInParams.sSubKeyName = Key
    oInParams.sValueName = Value
    Set oOutParams = oReg.ExecMethod_("GetDWORDValue", oInParams, , oCtx)
    RegReadDWORD = oOutParams.uValue
End Function