VBScript: Find Hostname from a IP Address

I had a requirement to find computer name from a list of IP addersses. I wrote the following vb script function for my requirement. I made it more generic function so any one can use it. Here it is.

‘Function to find Hostname from IP address. To run this function,
‘you have to be administrator to the targer computer.

‘ Function Name: HostName
‘ Parameters: IP Address as a string
‘ Returns Hostname string

Function HostName (IPAddress)
 Dim objWMI, objItem, colItems

 On Error Resume Next
 ‘ Get local WMI CIMv2 object
 Set objWMIService = GetObject(“winmgmts:\\” & IPAddress & “\root\cimv2”)
 If Err.Number <> 0 Then
  HostName = “Error”
   On Error Goto 0
  Exit Function
 End If
 On Error Goto 0

 Set colItems = objWMIService.ExecQuery(“SELECT * FROM Win32_ComputerSystem”, “WQL”, _
                                          wbemFlagReturnImmediately + wbemFlagForwardOnly)

 For Each objItem In colItems
  HostName = objItem.Name

 Set colItems = Nothing
 Set objWMI = Nothing
End Function
‘ – – – – End of Function  – – – –

5 thoughts on “VBScript: Find Hostname from a IP Address

    1. Great script trashd by formatting! 🙂
      Replac all the ‘ with ‘ and “ and ″with ” and it all works a treat. Much more satisfactory than runnign NSLookup in a shell. Thanks to poster.

    2. Yep, I even fixed all the formatting errors due to the website. All these websites screw up the ” marks. I don’t get an error after that but I did get a big fat nothing.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s