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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

A short article about a problem I had getting the GPS LocationListener working.
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

733 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