Solved

VBScript to write to csv

Posted on 2009-05-13
3
827 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

This article will show, step by step, how to integrate R code into a R Sweave document
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
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…

911 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now