Link to home
Start Free TrialLog in
Avatar of sunwise
sunwise

asked on

VBS script to resolve IP from a list of hostnames and then the current/last logged on username

I have taken the script from someone else and have tried to adjust it to suit my needs but just can't get it to work. I have been given 400 hostnames in a text file (hosts.txt) 1 hostname per line. I am a local admin to all machines so have enough rights. I just need the script to read each line(host) ping and make sure its alive, then resolve to IP along with the username on that machine. All machines are on the same domain.
'pingpcs.vbs
 
Dim objFSO: Set objFSO = CreateObject("Scripting.FileSystemObject")
Dim objShell: Set objShell = CreateObject("Wscript.shell")
Dim objFile: Set objFile = objFSO.OpenTextFile("hosts.txt")
Dim objWS, iRow
 
strReport = "Output.csv"  'Output report
Dim objOutput: Set objOutput=objFSO.CreateTextFile(strReport)
 
'Create and setup worksheet
objOutput.WriteLine "ComputerName" & "," & "IP Address" & "," & "Username"
 
Do Until objFile.AtEndOfStream
  strData = objFile.ReadLine
 
getIP(strData)
Loop
 
objOutput.Close
 
'Cleanup
Set objShell = Nothing
Set objWS = Nothing
Set objOutput = Nothing
WScript.Quit
 
function getIP(strComputer)
 
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") 
 
Set colComputer = objWMIService.ExecQuery _
    ("Select * from Win32_ComputerSystem")
 
For Each objComputer in colComputer
objOutput.WriteLine objComputer & "," & "IP Address HERE !!!" & "," & objComputer.UserName
Next
 
end function

Open in new window

Avatar of EDDYKT
EDDYKT
Flag of Canada image


Set objShell = Wscript.CreateObject("WScript.Shell")
Set objExecObject = objShell.Exec("%comspec% /c ping -n 1 localhost")

Do Until objExecObject.StdOut.AtEndOfStream
 strLine = objExecObject.StdOut.ReadLine()
 strIP = InStr(strLine, "Reply")
 If strIP <> 0 Then
 Wscript.Echo strLine
 End If
Loop

The output may be similiar like this if the machine alive
Reply from 127.0.0.1: bytes=32 time<1ms TTL=128
Avatar of sunwise
sunwise

ASKER

Thanks for the info, I have now built the script and works how I want it to. But the only problem I have left is. If a host is dead, the script returns an error instead of skipping and going to the next host in the text file. See atttached code below, this works great if you want to have hostname,hosts ip and username in a CSV format.
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objLogFile = objFSO.CreateTextFile("results.csv")
 
Set objComputers = objFSO.OpenTextFile("ips.txt")
Do While Not objComputers.AtEndOfStream
strComputer = objComputers.ReadLine
 
 
 
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
 
Set colItems = objWMIService.ExecQuery _
    ("Select * From Win32_NetworkAdapterConfiguration Where IPEnabled = True")
 
Set colComputer = objWMIService.ExecQuery _
    ("Select * from Win32_ComputerSystem")
 
 
 
 
 
For Each objComputer in colComputer
On Error Resume Next
username = objComputer.UserName
Next
 
for each objitem in colitems
On Error Resume Next
 
strIPAddress = Join(objitem.IPAddress, ",")
 
objLogFile.WriteLine strComputer & "," & strIPAddress & "," & username
 
Exit For
 
next
 
Loop
WScript.Echo "List has finished!"

Open in new window

Avatar of sunwise

ASKER

Got it to work, if the host is dead it will say so and if its alive it will carry out the rest of the script and get an IP and username. The only problem is its not clearing the variables properly and as a result it displays the same Ip address on alive hosts along with the same username. new code is below.
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objLogFile = objFSO.CreateTextFile("results.csv")
 
Set objComputers = objFSO.OpenTextFile("ips.txt")
Do While Not objComputers.AtEndOfStream
strComputer = objComputers.ReadLine
 
 
 
Set objPing = GetObject("winmgmts:{impersonationLevel=impersonate}")._
ExecQuery ("select Replysize from Win32_PingStatus where address = '" & strComputer & "'")
  
For Each objStatus In objPing
 If IsNull(objStatus.ReplySize) Then
 
 
  objLogFile.WriteLine strComputer & ", DEAD" & ", NULL"
 
set strIPAddress = Nothing
set strComputer = Nothing
Set username = Nothing
 
 Else
 
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
 
Set colItems = objWMIService.ExecQuery _
    ("Select * From Win32_NetworkAdapterConfiguration Where IPEnabled = True")
 
Set colComputer = objWMIService.ExecQuery _
    ("Select * from Win32_ComputerSystem")
 
 
For Each objComputer in colComputer
On Error Resume Next
username = objComputer.UserName
Next
 
for each objitem in colitems
On Error Resume Next
 
strIPAddress = Join(objitem.IPAddress, ",")
 
objLogFile.WriteLine strComputer & "," & strIPAddress & "," & username
 
set strIPAddress = Nothing
set strComputer = Nothing
Set username = Nothing
 
Exit For
 
next
 
 
 
End If
Next
 
Loop
WScript.Echo "List has finished!"

Open in new window

SOLUTION
Avatar of EDDYKT
EDDYKT
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial