We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now

x

Code not working on Windows Server 2008

spinzr0
spinzr0 asked
on
Medium Priority
811 Views
Last Modified: 2012-05-11
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

Comment
Watch Question

CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2014

Commented:
You may need to check the pingstatus.  See the code here:
https://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.
CERTIFIED EXPERT

Author

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?
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2014

Commented:
If you do
ping -a 10.10.10.1

at a command prompt, does it resolve?

Rob.
CERTIFIED EXPERT

Author

Commented:
Thanks for the quick response.  As mentioned in my original post, yes it does.
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2014

Commented:
Ooops, missed that....sorry.

Does
nbtstat -a 10.10.10.1

return the netbios name as well?

Rob.
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2014

Commented:
Is it also in the same subnet as the computer running the script?
CERTIFIED EXPERT

Author

Commented:
Yes to both.
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2014

Commented:
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.
CERTIFIED EXPERT

Author

Commented:
I know it worked on Server 2003. Assume it must be a setting. Can you confirm what "features" are enabled on your server?
CERTIFIED EXPERT

Author

Commented:
Also, if it helps I have Windows Server 2008 Standard SP2

Commented:
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?

Commented:
My apologies, did some simple testing...  

ProtocolAddressResolved always returns the IP, no matter what you do.
CERTIFIED EXPERT

Author

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?
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2014

Commented:
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.
CERTIFIED EXPERT

Author

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?
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2014

Commented:
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!
CERTIFIED EXPERT

Author

Commented:
Yeah, thats exactly the issue I'm running up against.
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2014

Commented:
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

CERTIFIED EXPERT

Author

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.
CERTIFIED EXPERT
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2014

Commented:
Fair enough.  Who knows why WMI doesn't work!?!?

Thanks for posting an alternative.

Rob.
CERTIFIED EXPERT

Author

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
CERTIFIED EXPERT

Author

Commented:
The win32_pingstatus wouldn't provide the needed result, the webservice works perfectly.
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.