connecting to WMI on remote computer using IP Address

Posted on 2004-04-22
Last Modified: 2010-05-18

I am connecting to WMI on the remote computer using the computername as follows

here is the code.

      Set objSWbemLocator = CreateObject("WbemScripting.SWbemLocator")                        
'Set objSWbemServices = objSWbemLocator.ConnectServer _
 (Computername,"root\cimv2", "Computername\localadmin","password")

this code works perfectly fine..which actually connects using the LocalAdmin accounts of the remote computer.

but, in some of the cases...i do not get the "computername" for the remote computer.I only have the IP Address.

So, in that case when i try to use the code...

Set objSWbemLocator = CreateObject("WbemScripting.SWbemLocator")                        
'Set objSWbemServices = objSWbemLocator.ConnectServer _
 (IPAddress,"root\cimv2", "IPAddress\localadmin","password")

it give me an error as
"The object exporter specified was not found"

so, does anybody know how do i connect to the remote computer using the IP Address, using the above code ?

thanks a lot.
Question by:godkedar

Expert Comment

ID: 10900218
Hi godkedar,

think youre code is OK...

...except the use of "IPAddress\localadmin" - try ".\localadmin" instead of IPAddress.

The dot will be interpreted as the local-machinename - to wich you are connected successfully via an IPAddress.

Happy Wbemming!


Accepted Solution

Level10Access earned 125 total points
ID: 10902263
I think he wants to connect to a REMOTE computer. Also, i did notice that apostrophe in front of the second line. Did you remember to take that off? Also, try to just use the username and password without the "ip\":

Set objSWbemLocator = CreateObject("WbemScripting.SWbemLocator")                    
Set objSWbemServices = objSWbemLocator.ConnectServer_ IPAddress,"root\cimv2", "localadmin","password")

Assisted Solution

plq earned 125 total points
ID: 10912018
Here's the code to lookup a computer name from an ip address, using sockets.

Just put this code in a new module and use

remotecomputername = getname(sipaddress)

Then you can call this from your code before doing the WMI connectserver


Option Explicit
Option Compare Text

Private Const WS_VERSION_REQD = &H101
Private Const MIN_SOCKETS_REQD = 1
Private Const SOCKET_ERROR = -1
Private Const WSADESCRIPTION_LEN = 256
Private Const WSASYS_Status_Len = 128

Const AF_INET = 2

Private Type WSADATA
    wHighVersion As Long
    szDescription As String * WSADESCRIPTION_LEN
    szSystemStatus As String * WSASYS_Status_Len
    iMaxSockets As Long
    iMaxUdpDg As Long
    lpVendorInfo As Long
End Type

Private Type HOSTENT
    h_name As Long 'LPSTR
    h_aliases As Long 'LPLPSTR
    h_addrtype As Integer
    h_length As Integer
    h_addr_list As Long 'char FAR * FAR * h_addr_list
End Type
Private Type sockaddr_in
        sin_family As Long
        sin_port As Integer
        in_addr As Long
        sin_zero As String * 8
End Type

Private Type in_addr
    b1 As Byte
    b2 As Byte
    b3 As Byte
    b4 As Byte
End Type
Private Declare Sub RtlMoveMemory Lib "kernel32" (hpvDest As Any, ByVal hpvSource&, ByVal cbCopy&)
Private Declare Function gethostname Lib "WSOCK32.DLL" (ByVal hostname$, ByVal HostLen As Long) As Long
Private Declare Function WSAStartup Lib "WSOCK32.DLL" (ByVal wVersionRequested As Integer, ByRef lpWSADATA As WSADATA) As Long
Private Declare Function WSACleanup Lib "WSOCK32.DLL" () As Long
Private Declare Function gethostbyaddr Lib "WSOCK32.DLL" (addr As Any, ByVal iLen As Long, ByVal itype As Long) As Long
Private Declare Function getnameinfo Lib "WSOCK32.DLL" (addr As Any, ByVal iLen As Long, ByVal host As String, ByVal HostLen As Long, ByVal serv As String, ByVal servlen As Long, ByVal flags As Long) As Long
Private Declare Function inet_addr Lib "WSOCK32.DLL" (ByVal cp As String) As Long
Private Declare Function gethostbyname Lib "WSOCK32.DLL" (ByVal name As String) As Long 'HOSTENT
Private Declare Function WSAGetLastError Lib "WSOCK32.DLL" () As Long

Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
Private Declare Function lstrlen Lib "kernel32" Alias "lstrlenA" (ByVal lpString As Long) As Long
Private Declare Function lstrcpy Lib "kernel32" Alias "lstrcpyA" (ByVal lpString1 As String, lpString2 As Any) As Long

Public Function GetName(cpAddr As String) As String
    Dim Adata As WSADATA
    Dim addr As Long
    Dim p As Long
    Dim host As HOSTENT
    Dim buf As String
    Dim sHost As String
    Dim sServ As String
    Dim sArr() As String
    Dim sWork As String
    sWork = ""
    If WSAStartup(&H101, Adata) <> 0 Then Exit Function
    addr = inet_addr(cpAddr & Chr$(0))
    If addr <> INADDR_NONE Then
        p = gethostbyaddr(ByVal VarPtr(addr), 4, AF_INET)
        If p Then
            CopyMemory ByVal VarPtr(host), ByVal p, LenB(host)
            buf = Space(lstrlen(host.h_name))
            lstrcpy buf, ByVal host.h_name
            sWork = Trim(Left(buf, lstrlen(host.h_name)))
        End If
    End If
    If InStr(sWork, ".") > 0 Then
        sArr = Split(sWork, ".")
        If sArr(0) <> "" Then
            sWork = sArr(0)
        End If
    End If
    GetName = sWork
End Function

Private Function SocketsInitialize() As String
    Dim iReturn As Integer
    Dim sLowByte As String, sHighByte As String, sMsg As String

    iReturn = WSAStartup(WS_VERSION_REQD, WSAD)

    If iReturn <> 0 Then
        SocketsInitialize = "Socket error " & Trim$(Str$(iReturn)) & " occurred in Startup "
        Exit Function
    End If

    SocketsInitialize = sMsg
End Function

Private Function SocketsCleanup() As String
    Dim lReturn As Long
    lReturn = WSACleanup()

    If lReturn <> 0 Then
        SocketsCleanup = "Socket error " & Trim$(Str$(lReturn)) & " occurred in Cleanup "
    End If

End Function

Private Function hibyte(ByVal wParam As Integer)

    hibyte = wParam \ &H100 And &HFF&

End Function

Private Function lobyte(ByVal wParam As Integer)

    lobyte = wParam And &HFF&

End Function


Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
countX 22 84
How to get boolean result of md5sum from two files in powershell 8 72
Base1 Encode/Decode 3 68
Full Screen problem when auto scale IFRAME 2 65
This is an explanation of a simple data model to help parse a JSON feed
If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now