【问题标题】:How to get IP Address for a cluster in Windows 2008 Server如何在 Windows 2008 Server 中获取群集的 IP 地址
【发布时间】:2013-02-21 15:44:12
【问题描述】:

我创建了一个 vbscript 从服务器获取一堆系统信息,我需要获取的一件事是服务器的 IP 地址。显然我只得到了“心跳 ip 地址”,这不是我需要的。

vbscript 有没有办法让我与 wmi 绑定并获取 ip 地址,我假设是私有的。

我试过这个 WMI 类 http://msdn.microsoft.com/en-us/library/windows/desktop/aa371441(v=vs.85).aspx

这就是我所拥有的

Option Explicit
'On Error Resume Next

Dim strIPAddress,objItem,colItems,objWMISrvc,strComputer,objAddr

strComputer = "."
Set objWMISrvc = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

getIPAddress

Sub getIPAddress()
  'Check if system is a cluster'
  strIPAddress = ""
  Set colItems = objWMISrvc.ExecQuery("SELECT * FROM MSCluster_Network")
  If Err.Number <> 0 Then
    For Each objItem in colItems
      'strIPAddress = "IP Address: " & objItem.
      For i = 0 to UBound(objItem.IPv4Addresses)
         strIPAddress = "IP Address (" & i & "): " & objItem.IPv4Addresses(i)
      Next
    Next
  End If
'############################################################
' These are all commented because this is how I WAS getting 
' the ip address but it only returned the heartbeat. 
' I will still be using this if I can somehow tell if a server is a cluster or not.
'If strIPAddress = "" Then
''  Dim i
  '' Set colItems = objWMISrvc.ExecQuery("SELECT * FROM Win32_NetworkAdapterConfiguration")
   'For Each objItem in colItems
    ''  If Not IsNull(objItem.IPAddress) Then
     ''    strIPAddress = objItem.IPAddress(0)
      ''   'For i = 0 to UBound(objItem.IPAddress)
       '' ' '   strIPAddress = strIPAddress & " | " & objItem.IPAddress(i)
        '' Next
     '' End If 
   'Next
'End If

WScript.Echo strIPAddress

End Sub

当我的客户端在他们的集群服务器上运行它时,它提供了一个空的 msgbox,因此它没有提取 IP 地址。

【问题讨论】:

    标签: vbscript wmi cluster-computing


    【解决方案1】:

    首先,您需要mscluster 命名空间,而不是cimv2 命名空间。

    Set cluster = GetObject("winmgmts://./root/mscluster")
    

    其次,你需要的地址是什么?一个集群通常有多个具有不同 IP 地址范围的网络。 MSCluster_Network 类允许您枚举网络及其网络地址:

    For Each nw In cluster.ExecQuery("SELECT * FROM MSCluster_Network")
      WScript.Echo nw.Name & vbTab & nw.Address
    Next
    

    输出应该是这样的(注意最后一个八位字节中的0,它是网络地址,而不是实际的主机地址):

    iSCSI   192.168.26.0
    Live Migration  192.168.25.0
    Management      192.168.23.0
    Heartbeat       192.168.24.0
    

    如果您想要单个集群成员的 IP 地址,可以通过 MSCluster_NetworkInterface 类获取它们:

    hostname = "foobar"
    
    For Each iface In cluster.ExecQuery("SELECT * FROM MSCluster_NetworkInterface")
      If Left(iface.Name, Len(hostname)) = hostname Then
        WScript.Echo iface.Name & vbTab & iface.Address
      End If
    Next
    

    应该产生这样的输出:

    foobar - iSCSI-0     192.168.26.17
    foobar - Live Migration      192.168.25.23
    foobar - Management  192.168.23.42
    foobar - Heartbeat   192.168.24.13
    

    【讨论】:

    • 漂亮!让我试试这个然后回复你。它看起来正是我所需要的,感谢您指出我使用了错误的命名空间。我需要了解每个命名空间中的内容,以便将来使用正确的命名空间。
    • @Kiquenet C# 也应该能够查询 WMI。
    猜你喜欢
    • 2021-11-26
    • 1970-01-01
    • 2023-03-16
    • 1970-01-01
    • 2012-04-14
    • 1970-01-01
    • 2011-07-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多