• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 785
  • Last Modified:

Code not working on Windows Server 2008

I have some code that I use on windows 7 and Windows Server 2008 to get a computer name from an IPAddress.  On Windows Server 2008, it only returns the IP Address.

So for Win7, I call Msgbox GetComputerNameFromIP("10.10.10.1") and get "MY-PC-NAME"
On Server 2008, I call Msgbox GetComputerNameFromIP("10.10.10.1") and get "10.10.10.1"

Any ideas why?  Is there a setting wrong in the server?

As a note, if I use the command prompt and do "ping -a 10.10.10.1" I do get the hostname
Msgbox GetComputerNameFromIP("10.10.10.1")

Function GetComputerNameFromIP(sComputer)
    Set oWMI = GetObject("winmgmts:\\.\root\cimv2")
    Set cPings = oWMI.ExecQuery("SELECT * FROM Win32_PingStatus " &_
           "WHERE Address = '" & sComputer & "' AND Timeout = 1000 AND ResolveAddressNames = TRUE AND RecordRoute = 1")
    For Each oPing In cPings
        GetComputerNameFromIP = oPing.ProtocolAddressResolved
    Next
    Set cPings = Nothing
    Set oWMI = Nothing
End Function

Open in new window

0
spinzr0
Asked:
spinzr0
  • 12
  • 9
  • 2
1 Solution
 
RobSampsonCommented:
You may need to check the pingstatus.  See the code here:
http://www.experts-exchange.com/Software/Office_Productivity/Office_Suites/MS_Office/Excel/Q_24055470.html

Or if the server *does* respond, maybe the Server 2008 firewall is blocking the request.

Regards,

Rob.
0
 
spinzr0Author Commented:
The server responds correctly and the firewall is turned off for testing.  The ping works, its just not returning the name.  Any more advice?
0
 
RobSampsonCommented:
If you do
ping -a 10.10.10.1

at a command prompt, does it resolve?

Rob.
0
Creating Active Directory Users from a Text File

If your organization has a need to mass-create AD user accounts, watch this video to see how its done without the need for scripting or other unnecessary complexities.

 
spinzr0Author Commented:
Thanks for the quick response.  As mentioned in my original post, yes it does.
0
 
RobSampsonCommented:
Ooops, missed that....sorry.

Does
nbtstat -a 10.10.10.1

return the netbios name as well?

Rob.
0
 
RobSampsonCommented:
Is it also in the same subnet as the computer running the script?
0
 
spinzr0Author Commented:
Yes to both.
0
 
RobSampsonCommented:
The code works for me against a Server 2008 machine.  I'll have to do a bit of research to see what I can find....

Rob.
0
 
spinzr0Author Commented:
I know it worked on Server 2003. Assume it must be a setting. Can you confirm what "features" are enabled on your server?
0
 
spinzr0Author Commented:
Also, if it helps I have Windows Server 2008 Standard SP2
0
 
raiERBCommented:
Here is what ProtocolAddressResolved officially returns:

ProtocolAddressResolved
Data type: string
Access type: Read-only
Qualifiers: MaxLen (4096)

Resolved address corresponding to the ProtocolAddress property. The default is "".

Therefore it gets the "ProtocolAddress".

In your code line 6 you specify:

""WHERE Address = '" & sComputer &... 

Open in new window


Therefore you set the address to the IP.  I can only assume that under windows sever 2008 the IP is not looked up and replaced with the server name (which was probably done unter previous versions).  Since you already checked the reverse lookup (with nbtstat) you can exclude that the DNS is missing the pointer record.

What is the goal of your script?  Do you need to find out the computer name that responds to a certain IP?

0
 
raiERBCommented:
My apologies, did some simple testing...  

ProtocolAddressResolved always returns the IP, no matter what you do.
0
 
spinzr0Author Commented:
I'm passing in the IP, the variable being called sComputer was just because I repurposed a function where I was passing in a computer name instead of an IP address.  On every OS but Windows Server 2008 it does return the computer name.  Rob said his server 2008 does return the computer name so I'm thinking its a setting that is off.

Any ideas?
0
 
RobSampsonCommented:
Against my Server 2008 Standard with SP2, I run this from my local Windows XP workstation:
If LCase(Right(Wscript.FullName, 11)) = "wscript.exe" Then
    strPath = Wscript.ScriptFullName
    strCommand = "%comspec% /k cscript  """ & strPath & """"
    Set objShell = CreateObject("Wscript.Shell")
    objShell.Run(strCommand), 1, True
    Wscript.Quit
End If

On Error Resume Next

Const wbemFlagReturnImmediately = &h10
Const wbemFlagForwardOnly = &h20

strIP = "10.10.10.1"

Set objWMIService = GetObject("winmgmts:\\.\root\CIMV2")
Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_PingStatus WHERE Address = '" & strIP & "' AND ResolveAddressNames = TRUE", "WQL", wbemFlagReturnImmediately + wbemFlagForwardOnly)

For Each objItem In colItems
	WScript.Echo "Address: " & objItem.Address
	WScript.Echo "BufferSize: " & objItem.BufferSize
	WScript.Echo "NoFragmentation: " & objItem.NoFragmentation
	WScript.Echo "PrimaryAddressResolutionStatus: " & objItem.PrimaryAddressResolutionStatus
	WScript.Echo "ProtocolAddress: " & objItem.ProtocolAddress
	WScript.Echo "ProtocolAddressResolved: " & objItem.ProtocolAddressResolved
	WScript.Echo "RecordRoute: " & objItem.RecordRoute
	WScript.Echo "ReplyInconsistency: " & objItem.ReplyInconsistency
	WScript.Echo "ReplySize: " & objItem.ReplySize
	WScript.Echo "ResolveAddressNames: " & objItem.ResolveAddressNames
	WScript.Echo "ResponseTime: " & objItem.ResponseTime
	WScript.Echo "ResponseTimeToLive: " & objItem.ResponseTimeToLive
	strRouteRecord = Join(objItem.RouteRecord, ",")
	   WScript.Echo "RouteRecord: " & strRouteRecord
	strRouteRecordResolved = Join(objItem.RouteRecordResolved, ",")
	   WScript.Echo "RouteRecordResolved: " & strRouteRecordResolved
	WScript.Echo "SourceRoute: " & objItem.SourceRoute
	WScript.Echo "SourceRouteType: " & objItem.SourceRouteType
	WScript.Echo "StatusCode: " & objItem.StatusCode
	WScript.Echo "Timeout: " & objItem.Timeout
	strTimeStampRecord = Join(objItem.TimeStampRecord, ",")
	   WScript.Echo "TimeStampRecord: " & strTimeStampRecord
	strTimeStampRecordAddress = Join(objItem.TimeStampRecordAddress, ",")
	   WScript.Echo "TimeStampRecordAddress: " & strTimeStampRecordAddress
	strTimeStampRecordAddressResolved = Join(objItem.TimeStampRecordAddressResolved, ",")
	   WScript.Echo "TimeStampRecordAddressResolved: " & strTimeStampRecordAddressResolved
	WScript.Echo "TimestampRoute: " & objItem.TimestampRoute
	WScript.Echo "TimeToLive: " & objItem.TimeToLive
	WScript.Echo "TypeofService: " & objItem.TypeofService
	WScript.Echo
Next

Open in new window


and end up with this output:
Address: 10.10.10.1
BufferSize: 32
NoFragmentation: False
PrimaryAddressResolutionStatus: 0
ProtocolAddress: 10.10.10.1
ProtocolAddressResolved: hostname.mydomain.com
RecordRoute: 0
ReplyInconsistency: False
ReplySize: 32
ResolveAddressNames: True
ResponseTime: 0
ResponseTimeToLive: 128
RouteRecord:
RouteRecordResolved:
SourceRoute:
SourceRouteType: 0
StatusCode: 0
Timeout: 1000
TimeStampRecord:
TimeStampRecordAddress:
TimeStampRecordAddressResolved:
TimestampRoute: 0
TimeToLive: 128
TypeofService: 128

Open in new window


So I don't know if that could help identify differing settings, but I'm not sure.  Can you run the same code *from* the Server 2008 box, entering it's own IP as strIP?

Regards,

Rob.
0
 
spinzr0Author Commented:
Running it from my Windows 7 or XP box against the server works.  Running it ON the server does not.  The ProtocolAddressResolved just resolves to the IP address.

If you run the code on the Server pinging anything, do you get the ProtocolAddressResolved to resolve to the name?
0
 
RobSampsonCommented:
No, running the code *from* the Server 2008 box against any other machine does not return the resolved host name, but the ping -a and nbtstat -a *do* return the host name.  That's odd!
0
 
spinzr0Author Commented:
Yeah, thats exactly the issue I'm running up against.
0
 
RobSampsonCommented:
I can't figure out why that's happening, but WMI can do some pretty strange things....anyway, try this code, it connects directly to the Win32_ComputerSystem class of the remote machine to get the hostname, if your first method fails.

Regards,

Rob.
strIP = "10.10.10.1"
strHostName = GetComputerNameFromIP(strIP)
If strIP = strHostName Then strHostName = GetComputerNameViaWMI(strIP)
MsgBox strHostName

Function GetComputerNameFromIP(sComputer)
    Set oWMI = GetObject("winmgmts:\\.\root\cimv2")
    Set cPings = oWMI.ExecQuery("SELECT * FROM Win32_PingStatus " &_
           "WHERE Address = '" & sComputer & "' AND Timeout = 1000 AND ResolveAddressNames = TRUE AND RecordRoute = 1")
    For Each oPing In cPings
        GetComputerNameFromIP = oPing.ProtocolAddressResolved
    Next
    Set cPings = Nothing
    Set oWMI = Nothing
End Function 

Function GetComputerNameViaWMI(sComputer)
	Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & sComputer & "\root\cimv2") 
	Set colComputer = objWMIService.ExecQuery("Select Name From Win32_ComputerSystem")
	For Each objComputer in colComputer
	    strHN = objComputer.Name
	Next
	GetComputerNameViaWMI = strHN
	Set colComputer = Nothing
	Set objWMIService = Nothing
End Function

Open in new window

0
 
spinzr0Author Commented:
Yeah, I originally was using Win32_ComputerSystem to get this info but the speed is too slow for my needs.  Its accessing machines all over the world and could take up to a minute in some cases.  That was why I ended up pursuing ping.
0
 
spinzr0Author Commented:
Ok, I ended up writing a C# webservice and calling into it to resolve this.  I don't know why pingstatus doesn't work, but for anyone who needs, just use this function and create a webservice.  Or, you can create a com object.

public string GetHostnameFromIP(string IPAddress)
        {
            IPHostEntry IpToDomainName = Dns.GetHostEntry(IPAddress);
            return IpToDomainName.HostName;
        }
0
 
RobSampsonCommented:
Fair enough.  Who knows why WMI doesn't work!?!?

Thanks for posting an alternative.

Rob.
0
 
spinzr0Author Commented:
Yeah, I didn't want to have to add the webservice but it was the only usable solution I could find.

Thanks for working on this with me
0
 
spinzr0Author Commented:
The win32_pingstatus wouldn't provide the needed result, the webservice works perfectly.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

  • 12
  • 9
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now