VBScript for displaying Computer Name of a remote PC

Could someone point me towards a simple vbscript that would display a remote computer's Computer Name if I type in the IP address? I don't want to use nslookup because my DNS server is pretty dirty (which is why I'm needing this).
trent_shaneAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Darrell PorterEnterprise Business Process ArchitectCommented:
There are likely 2 things you want the script to do:
1) Tell you the computer name of the machine whose IP address you enter
2) Tell the computer to perform an IPCONFIG /REGISTERDNS to update its A and PTR records on DNS

The code below should do item 1.
For Item 2, I would suggest using PSExec from Microsoft, found at http://technet.microsoft.com/en-us/sysinternals/bb896649, and perform a call within the VBScript to execute PSExec and pass it the appropriate parameters to run the IPCONFIG command - Hint: you will need to run CMD.EXE /C "IPCONFIG /REGISTERDNS"

So your PSEXEC line will look something like
PSEXEC \\IPAddress -u domain\username -p password CMD.EXE /C IPCONFIG /REGISTERDNS
Function IsAlive(strTarget)
	' Create a object reference to WMI on the local system
	Set objLocalWMI = GetObject("winmgmts:\\.\root\cimv2")
	' The neat little WMI ping statement
	Set colPingStatus = objLocalWMI.ExecQuery ("Select * from Win32_PingStatus Where Address = '" & strTarget & "'")	
	' The command above returns a collection, even tho we only have a single object we care about, we need to get refernces
	' to the object, and not the collection to do stuff
	For Each objPingStatus in colPingStatus
		' If the ping returned 0 Then it's successful and we contine to check
		If objPingStatus.StatusCode = 0 Then
			' If the Constant value for PING_ONLY is set to TRUE, then it will skip running the WMI Check
			' This is a little less accurate in my experience, but can speed up queries to lots of systems
			' if the target list is valid (and just some systems may be powered off.)
			If NOT PING_ONLY Then
				' We need to turn off the VBScript behavior of stopping when it encounters an error
				' This is called Error Trapping, VBScript will ignore errors and continue onto the next line
				On Error Resume Next
				' We now just try to create a refernce to the remote systems WMI, if it's not responding or
				' we don't have priviledges on the remote system, then VBScript will throw an error, but continue on
				' since we stated On Error Resume Next
				Set objWMIService = GetObject("winmgmts:\\" & strTarget & "\root\cimv2")
				' Now we check if VBScript did throw an error, if the last statement was successful, then the
				' Number property of the Err object will be 0, othterwise it's a numerical reference to an error
				If Err.number <> 0 Then
					' return a descriptive Error, along with the error number, to be reported
					' In this case, the PING was successful, but we couldn't connect to it
					IsAlive = "WMI Failed to connect, error " & Err.Number
					' Tidy up by destroying the objects created
					Set objWMIService = Nothing
					Set objLocalWMI = Nothing
					Exit Function
				Else
					' If we make it then the Ping was Successful, and WMI connect was successful, so return OK
					IsAlive = "OK"
						Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_ComputerSystem", "WQL", wbemFlagReturnImmediately + wbemFlagForwardOnly)
						For Each objItem In colItems
							WScript.Echo "Name: " & objItem.Name
						Next

				' End If Err.number <> 0
				End If
				' This statement turns the Error Trapping off, so VBScript will stop if it encounters an error again
				On Error Goto 0
			Else
				' We Pinged OK and PING_ONLY was set TRUE, so we return OK here
				IsAlive = "OK"
			' End If NOT PING_ONLY
			End If
		Else
			' The Ping Check failed, and we didn't bother doing the WMI check, so we just return a status of Could not be pinged
			IsAlive = "Could not be pinged"
		' End If objPingStatus.StatusCode = 0
		End If
	' Loop back to the next object in the colPingStatus colletion (even though there shouldn't be any other objects.)
	Next
	' Tidy up by destroying the objects
	Set objWMIService = Nothing
	Set objLocalWMI = Nothing
	Set colPingStatus = Nothing

End Function

Open in new window

0
trent_shaneAuthor Commented:
Thanks for the response!

For item 1, would I just copy and paste it into notepad and save it as a vbscript?

If so, nothing happens when I run it.
0
Darrell PorterEnterprise Business Process ArchitectCommented:
You need to pass it an IP address.
Do you have more than one subnet you want to run this from?
0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

trent_shaneAuthor Commented:
It would be nice to run it on just one subnet. I don't know anything about progamming or scripting so if you could tell me exactly where to enter the IP address or subnet that the remote computer resides, that would be great.

Thanks!

-Trent
0
Darrell PorterEnterprise Business Process ArchitectCommented:
What is the IP address of your computer?

Go to a command prompt and type in IPCONFIG and then post your IP address here so I can add the piece to scan your subnet.
0
trent_shaneAuthor Commented:
Its 172.16.136.32

But I have about 200 other subnets I will want to eventually scan. Maybe if you create the example, I'll change it as needed.
0
Darrell PorterEnterprise Business Process ArchitectCommented:
sounds good - give me a moment
0
David Johnson, CD, MVPOwnerCommented:
I use angry ip scanner and then output to a csv
0
cantorisCommented:
If you want to just find a computername from its IP and don't want to worry about scripting, enter this command

wmic /node:<IP address> os get csname

eg test your own PC:  wmic /node:127.0.0.1 os get cname
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
trent_shaneAuthor Commented:
cantoris, that works well with Windows 2000/XP/2003, but by default the RPC Server service is disabled on Windows 7 machines and this won't allow the command to get the name.
0
cantorisCommented:
That's because the firewall is blocking it on Windows 7 by default.  You would want to enable your firewall to allow remote management via WMI (which is what wmic.exe uses).

Try this (from an elevated prompt) on a Windows 7 machine
 
netsh advfirewall firewall set rule group="Windows Management Instrumentation (WMI)" new enable=yes

Open in new window

and then see if you can connect to it.

In practise, you'd want to configure this using Group Policy at the domain level.
If the PCs are in a workgroup, then a reghack is needed too - I think it's the one at the bottom of the following page:
http://msdn.microsoft.com/en-us/library/windows/desktop/aa822854(v=vs.85).aspx
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Legacy OS

From novice to tech pro — start learning today.

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.