Solved

VBScript to write to csv

Posted on 2009-05-13
3
830 Views
Last Modified: 2013-11-10
I have a script that I'm using to pull service tag information across the network.  I am currently passing a variable to it via command line of a particular host.  "wscript servicetag.vbs %hostname%"

Can someone help me modify this to scan a range of addresses to find the service tag and write the information to a csv?  I would want it to say the ip and/or hostname as well as the service tag.  Thanks!
On Error Resume Next 
Dim ArgObj, strcomputer
Set ArgObj = WScript.Arguments
' First Parameter
strcomputer = ArgObj(0)
 
On Error Resume Next 
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
if err.number <> 0 then 
if err.number = -2147217405 then  
wscript.echo "You do not have sufficient access rights to this computer"  
wscript.quit  
else  
wscript.echo "Could not locate computer" &vbcrlf& "Please check IP Address/Computer Name and try again"  
wscript.quit 
end if
end if
 
Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_BIOS")
Set colItems1 = objWMIService.ExecQuery("SELECT * FROM Win32_ComputerSystem")
Set colItems2 = objWMIService.ExecQuery("SELECT * FROM Win32_NetworkAdapterConfiguration Where IPEnabled = True")
Set colitems3 = objWMIService.ExecQuery("SELECT * FROM Win32_computersystem")
Set colitems4 = objWMIService.ExecQuery("SELECT * FROM Win32_NetworkLoginProfile")
Set colitems5 = objWMIService.ExecQuery("SELECT * FROM Win32_LogicalDisk Where DriveType = 3")  
 
Set objExplorer = CreateObject("InternetExplorer.Application")
objExplorer.Navigate "about:blank"
objExplorer.ToolBar = 0
objExplorer.StatusBar = 0
objExplorer.Width = 350
objExplorer.Height = 250
objExplorer.Left = 100
objExplorer.Top = 100
objExplorer.Visible = 1 
 
Do While (objExplorer.Busy)
Loop   
 
Set objDocument = objExplorer.Document
objDocument.Open
objDocument.Writeln "<html><head><title>Computer Information</title></head>"
objDocument.Writeln "<body bgcolor='white'>" 
 
' Computer Detals 
For Each objItem In colItems  
serial = objitem.serialnumber
next 
For Each objItem In colItems1 
hostname = objitem.caption 
make = objitem.manufacturer 
model = objitem.model
 
next 
 
objDocument.Writeln "<FONT color='red' size=4>Computer Information For: " & Ucase(hostname) & "</FONT><BR><BR>"
objDocument.Writeln "<FONT face='Courier' color='black'>Service Tag : " & Serial & "</FONT><BR>"
objDocument.Writeln "<FONT face='Courier' color='black'>Make : " & make & "</FONT><BR>"
objDocument.Writeln "<FONT face='Courier' color='black'>Model : " & Model & "</FONT><BR>" 
 
For Each objItem In colItems5 
driveletter = objitem.name 
capacity = ((objitem.size / 1024) / 1024) / 1024 
free = ((objitem.FreeSpace / 1024) / 1024) / 1024 
free = FormatNumber (free,2) 
capacity = FormatNumber (capacity,2)  
 
objDocument.Writeln "<FONT face='Courier' color='black'>Capacity of " &driveletter& " - " & capacity & "GB</FONT><BR>"
objDocument.Writeln "<FONT face='Courier' color='black'>Free Space on " &driveletter& " - " & Free & "GB</FONT><BR>" 
next

Open in new window

0
Comment
Question by:caseassoc
  • 2
3 Comments
 
LVL 10

Accepted Solution

by:
cdebel earned 500 total points
ID: 24380322
Here's a piece of code...

You have to use it this way:

MyFile.vbs 192.168.0.0 192.168.2.157 "./MyOutput.csv"
RunMyCode
 
Sub RunMyCode()
    On Error Resume Next
    Dim ArgObj, strcomputer, fso, f, strFileName, strIP1, strIP2
    Set ArgObj = wscript.Arguments
    
    ' First Parameter
    strIP1 = ArgObj(0)
    strIP2 = ArgObj(1)
    strFileName = ArgObj(2)
 
     
    On Error Resume Next
    
    ' Create your text file
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set f = fso.CreateTextFile(strFileName, True)
 
    Do
	if IsReachable(strIP1) = "ok" then
	        Set objWMIService = GetObject("winmgmts:\\" & strIP1 & "\root\CIMV2")
	        If Err.Number <> 0 Then
	            Err.Clear
		Else
	            Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_BIOS")
	            Set colItems1 = objWMIService.ExecQuery("SELECT * FROM Win32_ComputerSystem")
	            Set colItems2 = objWMIService.ExecQuery("SELECT * FROM Win32_NetworkAdapterConfiguration Where IPEnabled = True")
	            Set colitems3 = objWMIService.ExecQuery("SELECT * FROM Win32_computersystem")
	            Set colitems4 = objWMIService.ExecQuery("SELECT * FROM Win32_NetworkLoginProfile")
	            Set colItems5 = objWMIService.ExecQuery("SELECT * FROM Win32_LogicalDisk Where DriveType = 3")
	      
	            ' Computer Detals
	            For Each objitem In colItems
	                Serial = objitem.serialnumber
	            Next
	            
	            For Each objitem In colItems1
	                hostname = objitem.Caption
	                make = objitem.manufacturer
	                Model = objitem.Model
	            Next
	
	            f.WriteLine (strIP1 & ", " & UCase(hostname) & ", " & Serial)             
		end if
	end if
    Loop While NextIP(strIP1, strIP2)
 
    f.close
    MsgBox "Finished!"
End Sub
 
 
Function NextIP(sActualIP, sUpperIP)
    Dim SubArray
    
    If sActualIP = sUpperIP Then
        NextIP = False
        Exit Function
    End If
    
    SubArray = Split(sActualIP, ".")
    If SubArray(3) < 255 Then
        SubArray(3) = SubArray(3) + 1
    ElseIf SubArray(2) < 255 Then
        SubArray(2) = SubArray(2) + 1
        SubArray(3) = 0
    ElseIf SubArray(1) < 255 Then
        SubArray(1) = SubArray(1) + 1
        SubArray(2) = 0
        SubArray(3) = 0
    End If
    
    sActualIP = Join(SubArray, ".")
    NextIP = True
End Function
 
 
Function IsReachable(strComputer)
    Dim strCmd
    Dim strTemp
    Dim objShell
    Dim objExec
    
    strCmd = "ping -n 1 " & strComputer
    Set objShell = CreateObject("WScript.Shell")
    Set objExec = objShell.Exec(strCmd)
    strTemp = UCase(objExec.StdOut.ReadAll)
 
    If InStr(strTemp, "REPLY FROM") Then
        IsReachable = "ok"
    ElseIf InStr(strTemp, "TIMED OUT") Then
        IsReachable= "out"
    ElseIf InStr(strTemp, "FIND HOST") Then
        IsReachable = "find"
    End If
 
    Set objShell = Nothing
    Set objExec = Nothing
End Function

Open in new window

0
 

Author Comment

by:caseassoc
ID: 24381361
thanks for the quick reply!  I'll try it when I get back to work.  Sounds good though!  I'll let you know asap.
0
 

Author Closing Comment

by:caseassoc
ID: 31581207
Great Job!  Works exactly as I asked for!
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question