Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

VBScript to write to csv

Posted on 2009-05-13
3
Medium Priority
?
833 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 10

Accepted Solution

by:
Christian de Bellefeuille earned 2000 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
We live in a world of interfaces like the one in the title picture. VBA also allows to use interfaces which offers a lot of possibilities. This article describes how to use interfaces in VBA and how to work around their bugs.
Simple Linear Regression
Progress

722 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