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

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
delphi parse string to params 3 126
python sqlite question 11 46
Base1 Encode/Decode 3 77
How to Convert a Quote to an Order in SalesForce 2 46
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

825 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