Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

vbscript to scan network for computer names.

Status
Not open for further replies.

rgudino

Technical User
Jan 29, 2004
12
US
I'm looking for a vbscript to scan my network for computer names. I would like to put that script into my web form.
I would like to have it when I hit the scan button it goes and does it's thing and have it the results put into access database.

PS. Newbie to vbscripts

thank you
 
i would never recommend piping results of shell commands and then reading the file but off the top of my head 'net view' might help you.

you can pipe it to a text file and then read teh text file or you can use the WshShell.Exec method to read it in from standard output

i am sure there is a better way of doing what you want. what can i say its 9am on a friday!!! and its my stag weekend!!!!...again
 
This script returns the name and location for all the computer accounts in Active Directory. Make sure you edit the LDAP path with your DC info.

Script Code

Const ADS_SCOPE_SUBTREE = 2
Set objConnection = CreateObject("ADODB.Connection")
Set objCommand = CreateObject("ADODB.Command")
objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"
Set objCOmmand.ActiveConnection = objConnection
objCommand.CommandText = _
"Select Name, Location from 'LDAP://DC=spidersparlor,DC=com' " _
& "where objectClass='computer'"
objCommand.Properties("Page Size") = 1000
objCommand.Properties("Timeout") = 30
objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE
objCommand.Properties("Cache Results") = False
Set objRecordSet = objCommand.Execute
objRecordSet.MoveFirst
Do Until objRecordSet.EOF
Wscript.Echo "Computer Name: " & objRecordSet.Fields("Name").Value
Wscript.Echo "Location: " & objRecordSet.Fields("Location").Value
objRecordSet.MoveNext
Loop

 
Thank you, But we don't use Active Directory.
 
OK, then you need to go at it a little less high tech.

You will need to edit the IP information for your subnet as well as the logfile names.

This is crude, so you will still need to clean up the data, but you can do that with some word macros in a few minutes if you get creative.


'==========================================================================
'
' PingFromHell.vbs
'
' AUTHOR: Mark D. MacLachlan, The Spider's Parlor
' URL : ' DATE : 1/30/2004
'
' COMMENT: Pings and entire subnet then records computer names to text file.
'
'==========================================================================

logFile = "C:\Docs\IP.Log"
cleanedLog = "C:\Docs\SortedIP.Log"


Set WshShell = WScript.CreateObject("WScript.Shell")

WshShell.Run "CMD.EXE"
WScript.Sleep 500

'Ping all the IPs in our subnet and write results to text file
For x = 1 To 254
WshShell.SendKeys("ping -a 10.0.0." & x & " >>"& logFile & "~")
WScript.Sleep 10000
Next

'now read the text file and strip out the junk

Report = ""
Dim oFSO

ForReading = 1
ForWriting = 2

Set oFSO=CreateObject("Scripting.FileSystemObject")
MyList= ofso_OpenTextFile(logFile, ForReading).ReadAll
myArray=Split(MyList,vbCrLf, -1, vbtextcompare)


For Each Val In myArray

If Left(Val,7)= "Pinging" Then
Report = Report & Val & vbCrLF
End If
Next

Set ts = oFSO.CreateTextFile (cleanedLog, ForWriting, True)
ts.write report
MsgBox "Done"
 
I use this script to output the information to the screen. Shouldn't be to hard to modify to write to a file.

Dim args, i

If Right(UCase(WScript.FullName),11)= "WSCRIPT.EXE" Then
args=""
For i=0 to WScript.Arguments.Count-1
args = args & WScript.Arguments(i) & " "
Next
Set WshShell=WScript.CreateObject("WScript.Shell")
WshShell.Run WshShell.ExpandEnvironmentStrings("%comspec%") & _
" /C cscript.exe """ & WScript.ScriptFullName & """" & args
Set WshShell = Nothing
WScript.Quit
End If

set wshshell = CreateObject("WScript.Shell")
set fs = CreateObject("Scripting.FileSystemObject")


DIM BRARRAY(0)
BRARRAY(0) = "firstthreeoctetsofip(ie xxx.xxx.xxx.)"
startoct = 1
endoct = 10
i = startoct
z = 0

on Error Resume Next
DO UNTIL Z = 1
STRCOMPUTER1 = Left(brarray(z),11)
hostname = strcomputer1 & startoct
do until i = endoct
if not reachable(hostname) then
wscript.echo iplit & hostname & " : " & " unused" & vbcr
else
cname hostname,compname
wscript.echo iplit & hostname & " : " & COMPNAME & vbcr
end if
i = i + 1
hostname = strcomputer1 & i
loop
z = z + 1
loop

wscript.quit

'*************************************************************
'*** Is Server Available Function ***
'*************************************************************
function Reachable(Hostname)
Dim WshShell, oExec
Set WshShell = CreateObject("WScript.Shell")

'The tool is launched
Set oExec = WshShell.EXEC("ping -n 2 -w 500 " & hostname)

'wait for the end of process
Do While oExec.Status = 0
WScript.Sleep 100
Loop

'scan and display the command output flow
Do While oExec.StdOut.AtEndOfStream <> True
retstring = oExec.StdOut.ReadLine
if instr(retString, &quot;Reply&quot;)>0 then
reachable = true
exit do
end if
Loop
end function

sub cname(hostname,compname)
Dim WshShell, oExec
Set WshShell = CreateObject(&quot;WScript.Shell&quot;)

'The tool is launched
Set oExec = WshShell.Exec(&quot;nbtstat -A &quot; & hostname)

'wait for the end of process
Do While oExec.Status = 0
WScript.Sleep 100
Loop

'scan and display the command output flow
Do While oExec.StdOut.AtEndOfStream <> True
retstring = oExec.StdOut.ReadLine
if instr(retString, &quot;UNIQUE&quot;)>0 then
COMPNAME = MID(RETSTRING,5,7)
exit do
end if
Loop
end sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top