Automation, Scripts, VB, Windows 2008, Windows 2012

Network Info / inventory

  • Creates an Excel File with the Network Info / inventory of all the computers that are listed in servers.txt
  • Needs Excel to be installed on the machine that the script will be run from.

Queries and reports the listed items:

Machine Name [Which is provided in the Servers.txt]
Status
NIC Label
IP Address
Subnet Mask
Default Gateway
MAC Address
DNS Servers
WINS Servers
WINS Servers


'==========================================================================
' VBScript Network Inventory 
' NAME: Network_Inventory.vbs
' AUTHOR: Murali Palla
' Contact [email protected]
' DATE  : 5/4/2013
' Creates an Excel File with the Network Info / inventory of all the computers that are listed in servers.txt
' Needs Excel to be installed on the machine that the script will be run from.
'==========================================================================
ScriptStartTime = Now()
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objShell = CreateObject("WScript.Shell")
If Instr(1, WScript.FullName, "CScript", vbTextCompare) = 0 Then
    objShell.Run "cscript """ & WScript.ScriptFullName & """", 1, False
    WScript.Quit
End If
If not objFSO.FileExists("servers.txt") Then
	wscript.echo "Please create a file named 'servers.txt' with one Server name per line,"
	wscript.quit
End If

Set objExcel = CreateObject("Excel.Application")
objExcel.Visible = True
objExcel.Workbooks.Add
intRow = 2
objExcel.Cells(1, 1).Value = "Machine Name"
objExcel.Cells(1, 2).Value = "Status"
objExcel.Cells(1, 3).Value = "NIC Label"
objExcel.Cells(1, 4).Value = "IP Address"
objExcel.Cells(1, 5).Value = "Subnet Mask" 
objExcel.Cells(1, 6).Value = "Default Gateway" 
objExcel.Cells(1, 7).Value = "MAC Address"
objExcel.Cells(1, 8).Value = "DNS Servers" 
objExcel.Cells(1, 9).Value = "WINS Servers"
objExcel.Cells(1, 10).Value = "WINS Servers"
objExcel.Cells.EntireColumn.AutoFit
objExcel.Range("A1:I1").Select
objExcel.Selection.Interior.ColorIndex = 19
objExcel.Selection.Font.ColorIndex = 11
objExcel.Selection.Font.Bold = True

WScript.Echo "Script Started, You will be notified once complete... Please be patient."
tempobj="temp.txt"
Set objTextFile = objFSO.OpenTextFile("servers.txt", 1)
strText = objTextFile.ReadAll
objTextFile.Close
arrComputers = Split(strText, vbCrLF)
for each item in arrcomputers
WScript.Echo item
objShell.Run "cmd /c ping -n 1 -w 1000 " & item & " >temp.txt", 0, True
Set tempfile = objFSO.OpenTextFile(tempobj,1)
Do Until tempfile.AtEndOfStream  
temp=tempfile.readall
 striploc = InStr(temp,"[")
               If striploc=0 Then
                       strip=""
               Else
                       strip=Mid(temp,striploc,16)
                       strip=Replace(strip,"[","")
                       strip=Replace(strip,"]","")
                       strip=Replace(strip,"w"," ")
                       strip=Replace(strip," ","")
               End If     
		
			If InStr(temp, "Reply from") Then
				strMStatus = "Success"                
				callf = GetIPDetails(item,strMStatus,strip)
			ElseIf InStr(temp, "Request timed out.") Then
				strMStatus = "RTO"
				callf = GetIPDetails(item,strMStatus,strip)
			ElseIf InStr(temp, "try again") Then
				strMStatus = "NDS"
				callf = GetIPDetails(item,strMStatus,strip)							
			End If    
Loop
Next

intRow = intRow + 2
objExcel.Cells(intRow, 3).Value = "Script Start Time"
objExcel.Cells(intRow, 4).Value = ScriptStartTime
intRow = intRow + 1
objExcel.Cells(intRow, 3).Value = "Script Completed At"
objExcel.Cells(intRow, 4).Value = Now()
objExcel.Cells.EntireColumn.AutoFit
strScriptName=WScript.ScriptName
strFullPath = WScript.ScriptFullName
strFileCompleteDate = Replace(Replace(Replace(Now(),"/","_"),":","_")," ","_")
strPath=Replace(strFullPath,strScriptName,"")&replace(strScriptName,".vbs","")&"_"&strFileCompleteDate&".xlsx"
objExcel.ActiveWorkbook.Saveas strPath
objExcel.ActiveWorkbook.Close
tempfile.close
objfso.deletefile(tempobj) 

Wscript.Echo "Done"
'*************** Functions Start *******************************************************************************************************
Function GetIPDetails(strComputer,strStatus,pingip)
On Error Resume Next

If strStatus = "Success" Then 
	'WScript.Echo vbTab &vbTab &"Checking DSM.OPT"
	'DSMOPT=readfile(strComputer) 
			
	Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CimV2")
	Set colItems = objWMIService.ExecQuery("Select * From Win32_NetworkAdapterConfiguration Where IpEnabled = True" )
		If Err.Number Then 
			WScript.Echo vbTab &vbTab &"Unable to Establish a WMI Session, Error: " &Err.Number &vbTab &Err.Description
			objExcel.Cells(intRow, 1).Value = strComputer
			objExcel.Cells(intRow, 2).Value = "Online."
			objExcel.Cells(intRow, 3).Value = pingip
			objExcel.Cells(intRow, 4).Value = Err.Number
			objExcel.Cells(intRow, 5).Value = Err.Description
			intRow = intRow + 1
		Else
			
			WScript.Echo vbTab &vbTab &"Checking Network INFO"
			
			For Each objItem in colItems
			strIpAddress = Join(objItem.IpAddress,";")
			strIpSubnet = Join(objItem.IpSubnet,";")
			strDNSDomain = Join(objItem.DNSServerSearchOrder,";")
			strDefaultGateway = Join(objItem.DefaultIpGateway,";")
			If strDefaultGateway <> "" Then 			 
				objExcel.Cells(intRow, 1).Value = UCase(objItem.DnsHostName)
				objExcel.Cells(intRow, 2).Value = "Online."
				objExcel.Cells(intRow, 3).Value = AdapterName(objItem.Caption)
				objExcel.Cells(intRow, 4).Value = strIPAddress
				strIPAddress=Null
				objExcel.Cells(intRow, 5).Value = strIPSubnet
				strIPSubnet=Null
				objExcel.Cells(intRow, 6).Value = strDefaultGateway
				strDefaultGateway=Null
				objExcel.Cells(intRow, 7).Value = objItem.MacAddress
	 			objExcel.Cells(intRow, 8).Value = strDNSDomain 		
	 			strDNSDomain=Null
	 			objExcel.Cells(intRow, 9).Value = objItem.WINSPrimaryServer							
				objExcel.Cells(intRow, 10).Value = objItem.WINSSecondaryServer	
				intRow = intRow + 1
			Else 
			End If 
			Next
			
		End If
ElseIf strStatus = "RTO" Then 
			WScript.Echo vbTab &vbTab &"No response (Offline)."
			objExcel.Cells(intRow, 1).Value = strComputer
			objExcel.Cells(intRow, 2).Value = "No response (Offline)." 
			objExcel.Cells(intRow, 4).Value = pingip
			intRow = intRow + 1
ElseIf strStatus = "NDS" Then
			WScript.Echo vbTab &vbTab &"Unknown host (no DNS entry)."
			objExcel.Cells(intRow, 1).Value = strComputer
			objExcel.Cells(intRow, 2).Value = "Unknown host (no DNS entry)."
			objExcel.Cells(intRow, 4).Value = pingip
			intRow = intRow + 1
End If

End Function 

Function AdapterName(strAdapter)
Dim colItems,objItem,ObjWMIService
Set ObjWMIService = GetObject("winmgmts:\\" & item & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * From Win32_NetworkAdapter where caption ='"&strAdapter&"'",,48)                                     
                For Each objItem in colItems
                            AdapterName = objItem.NetConnectionID
				Next
End Function 

Function readfile(strComputer)
Dim objFSO,strDSMOPT,strLine,strReadInput
Set objFSO = CreateObject("Scripting.FileSystemObject")
strReadInput= "\\"&strComputer&"\c$\Program Files\Tivoli\TSM\baclient\dsm.opt"

If objFSO.FileExists(strReadInput) Then 
	Set strDSMOPT = objFSO.OpenTextFile(strReadInput, 1)
	WScript.Sleep (1000)
	Do Until strDSMOPT.AtEndOfStream
		strLine=ucase(strDSMOPT.ReadLine)		
			If InStr(strLine,"*") Then 
			
			ElseIf  InStr(strLine,"TCPSERVERADDRESS") Then 
				readfile = trim(Mid(strLine,18,20))
				strDSMOPT.Close
				Exit Function 
			End If				
	Loop
	readfile="Not Able to Find TCPSERVERADDRESS"
	strDSMOPT.Close
	WScript.Sleep(2000)
Else
 
	readfile="Not Able to Find: "&strReadInput

End If 
End Function


'*************** Functions End *******************************************************************************************************

Loading