?
Solved

Code not working on Windows Server 2008

Posted on 2011-04-27
23
Medium Priority
?
777 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

0
Comment
Question by:spinzr0
  • 12
  • 9
  • 2
23 Comments
 
LVL 65

Expert Comment

by:RobSampson
ID: 35479970
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
 
LVL 8

Author Comment

by:spinzr0
ID: 35480115
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
 
LVL 65

Expert Comment

by:RobSampson
ID: 35480165
If you do
ping -a 10.10.10.1

at a command prompt, does it resolve?

Rob.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 8

Author Comment

by:spinzr0
ID: 35480190
Thanks for the quick response.  As mentioned in my original post, yes it does.
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 35480213
Ooops, missed that....sorry.

Does
nbtstat -a 10.10.10.1

return the netbios name as well?

Rob.
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 35480215
Is it also in the same subnet as the computer running the script?
0
 
LVL 8

Author Comment

by:spinzr0
ID: 35480219
Yes to both.
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 35480232
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
 
LVL 8

Author Comment

by:spinzr0
ID: 35480257
I know it worked on Server 2003. Assume it must be a setting. Can you confirm what "features" are enabled on your server?
0
 
LVL 8

Author Comment

by:spinzr0
ID: 35480261
Also, if it helps I have Windows Server 2008 Standard SP2
0
 
LVL 3

Expert Comment

by:raiERB
ID: 35482532
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
 
LVL 3

Expert Comment

by:raiERB
ID: 35482558
My apologies, did some simple testing...  

ProtocolAddressResolved always returns the IP, no matter what you do.
0
 
LVL 8

Author Comment

by:spinzr0
ID: 35483293
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
 
LVL 65

Expert Comment

by:RobSampson
ID: 35509995
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
 
LVL 8

Author Comment

by:spinzr0
ID: 35510016
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
 
LVL 65

Expert Comment

by:RobSampson
ID: 35510023
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
 
LVL 8

Author Comment

by:spinzr0
ID: 35510086
Yeah, thats exactly the issue I'm running up against.
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 35510129
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
 
LVL 8

Author Comment

by:spinzr0
ID: 35511905
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
 
LVL 8

Accepted Solution

by:
spinzr0 earned 0 total points
ID: 35553791
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
 
LVL 65

Expert Comment

by:RobSampson
ID: 35554642
Fair enough.  Who knows why WMI doesn't work!?!?

Thanks for posting an alternative.

Rob.
0
 
LVL 8

Author Comment

by:spinzr0
ID: 35555232
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
 
LVL 8

Author Closing Comment

by:spinzr0
ID: 35714927
The win32_pingstatus wouldn't provide the needed result, the webservice works perfectly.
0

Featured Post

Has Powershell sent you back into the Stone Age?

If managing Active Directory using Windows Powershell® is making you feel like you stepped back in time, you are not alone.  For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why.

Question has a verified solution.

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

If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Resolving an irritating Remote Desktop connection that stops your saved credentials from being used.
This tutorial will give a short introduction and overview of Backup Exec 2012 and how to navigate and perform basic functions. Click on the Backup Exec button in the upper left corner. From here, are global settings for the application such as conne…
This tutorial will walk an individual through the steps necessary to join and promote the first Windows Server 2012 domain controller into an Active Directory environment running on Windows Server 2008. Determine the location of the FSMO roles by lo…
Suggested Courses

850 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