# Solved: VB Script - use text file instead of array



## snufse (Nov 9, 2010)

Have following script and I would like to retrieve "computer names" from a text file instead of using array. Can someone help me with that? Thank you.


```
' This script makes a report of all services that are not started and that have start mode = Auto
Option Explicit
dim arrComputers, strComputer, objWMIService, colItems, objItem 
On Error Resume Next 
public objFso, objTextFile
Const ForReading = 1
Const ForWriting = 2
Const ForAppending = 8
Set objFSO = CreateObject("Scripting.FileSystemObject")
'Open text file for writing and clear text file prior to writing new events
Set objTextFile = objFSO.OpenTextFile("C:\Documents and Settings\vgwprja\My Documents\VB Script\Services Log File.txt", ForWriting, True)
objTextFile.Write ""
objTextFile.Close
Set objTextFile = objFSO.OpenTextFile("C:\Documents and Settings\vgwprja\My Documents\VB Script\Services Log File.txt", ForAppending, True)
[COLOR=red]arrComputers = Array("vgiwpw03-sql3","vgibesql")[/COLOR]
For Each strComputer In arrComputers
    Set objWMIService = GetObject("winmgmts:\\" _
        & strComputer& "\root\CIMV2") 
    Set colItems = objWMIService.ExecQuery( _
        "SELECT * FROM Win32_Service WHERE State = 'Stopped' and StartMode = 'Auto'")
    dim myVariable  
    For Each objItem in colItems 
     myVariable =  "Server: " & strComputer & "," & _
                  "Name: " & objItem.Name & "," & _
                  "Status: " & objItem.State & "," & _
                  "Start Mode: " & objItem.StartMode 
    objTextFile.WriteLine(myVariable)
    Next
   objTextFile.WriteLine(" ")
Next
```


----------



## Squashman (Apr 4, 2003)

Read the text file into an array
http://www.activexperts.com/activmo...cripts/other/textfiles/#ReadTextFileArray.htm

Or this
http://www.ehow.com/how_4901031_of-text-file-using-vbscript.html


----------



## snufse (Nov 9, 2010)

Modified the script to load text file into array. Now when I run the script following happens:

1. In the text file at the beginning it writes 10 blank lines when I just want 1 blank line

2. At the end of report it writes lines with no Server / PC name like the array contains blank entries (see below)

3. How can I detect if not able to connect to a Server / PC and write an error


```
Server: VGWPAVA,Name: clr_optimization_v4.0.30319_32,Status: Stopped,Start Mode: Auto
Server: VGWPAVA,Name: SysmonLog,Status: Stopped,Start Mode: Auto
 
[COLOR=red]Server: ,Name: clr_optimization_v4.0.30319_32,Status: Stopped,Start Mode: Auto[/COLOR]
[COLOR=red]Server: ,Name: SysmonLog,Status: Stopped,Start Mode: Auto[/COLOR]
 
[COLOR=red]Server: ,Name: clr_optimization_v4.0.30319_32,Status: Stopped,Start Mode: Auto[/COLOR]
[COLOR=red]Server: ,Name: SysmonLog,Status: Stopped,Start Mode: Auto[/COLOR]
 
[COLOR=red]Server: ,Name: clr_optimization_v4.0.30319_32,Status: Stopped,Start Mode: Auto[/COLOR]
[COLOR=red]Server: ,Name: SysmonLog,Status: Stopped,Start Mode: Auto[/COLOR]
 
[COLOR=red]Server: ,Name: clr_optimization_v4.0.30319_32,Status: Stopped,Start Mode: Auto[/COLOR]
[COLOR=red]Server: ,Name: SysmonLog,Status: Stopped,Start Mode: Auto[/COLOR]
 
 
 
          *** End of Report ***
```
Script:


```
' This script makes a report of all services that are not started and that have start mode = Auto
Option Explicit
dim strComputer, objWMIService, colItems, objItem, i 
On Error Resume Next 
public objFso, objTextFile
Const ForReading = 1
Const ForWriting = 2
Const ForAppending = 8
Set objFSO = CreateObject("Scripting.FileSystemObject")
'Open text file for readin PC names into array
Set objTextFile = objFSO.OpenTextFile("C:\Documents and Settings\vgwprja\My Documents\VB Script\PCNames.txt", ForReading, True)
dim arrComputers()
i = 0 
Do Until objTextFile.AtEndOfStream 
Redim Preserve arrComputers(i) 
arrComputers(i) = objTextFile.ReadLine 
i = i + 1 
Loop 
objTextFile.Close 
'Open text file for writing and clear text file prior to writing new events
Set objTextFile = objFSO.OpenTextFile("C:\Documents and Settings\vgwprja\My Documents\VB Script\Services Log File.txt", ForWriting, True)
objTextFile.Write ""
Set objTextFile = objFSO.OpenTextFile("C:\Documents and Settings\vgwprja\My Documents\VB Script\Services Log File.txt", ForAppending, True)
For Each strComputer In arrComputers
Set objWMIService = GetObject("winmgmts:\\" _
& strComputer& "\root\CIMV2") 
Set colItems = objWMIService.ExecQuery( _
"SELECT * FROM Win32_Service WHERE State = 'Stopped' and StartMode = 'Auto'")
dim myVariable 
For Each objItem in colItems 
myVariable = "Server: " & strComputer & ", " & _
"Name: " & objItem.Name & ", " & _
"Status: " & objItem.State & ", " & _
"Start Mode: " & objItem.StartMode 
if myVariable Is Not Nothing then 
objTextFile.WriteLine(myVariable)
end if
Next
objTextFile.WriteLine(" ")
Next
objTextFile.WriteLine(" ")
objTextFile.WriteLine(" ")
objTextFile.WriteLine(" *** End of Report *** ")
objTextFile.Close
```


----------



## snufse (Nov 9, 2010)

Solved this way:


```
' This script makes a report of all services that are not started and that have start mode = Auto
Option Explicit
dim strComputer, objWMIService, colItems, objItem, i 
On Error Resume Next 
public objFso, objTextFile
Const ForReading = 1
Const ForWriting = 2
Const ForAppending = 8
Set objFSO = CreateObject("Scripting.FileSystemObject")
'Open text file for readin PC names into array
Set objTextFile = objFSO.OpenTextFile("C:\Documents and Settings\vgwprja\My Documents\VB Script\PCNames.txt", ForReading, True)
dim arrComputers()
dim datetime
datetime = now
i = 0 
Do Until objTextFile.AtEndOfStream 
Redim Preserve arrComputers(i) 
arrComputers(i) = objTextFile.ReadLine 
i = i + 1 
Loop 
objTextFile.Close 
'Open text file for writing and clear text file prior to writing new events
Set objTextFile = objFSO.OpenTextFile("C:\Documents and Settings\vgwprja\My Documents\VB Script\Services Not Started Error Report.txt", ForWriting, True)
objTextFile.Write ""
Set objTextFile = objFSO.OpenTextFile("C:\Documents and Settings\vgwprja\My Documents\VB Script\Services Not Started Error Report.txt", ForAppending, True)
For Each strComputer In arrComputers
    Set objWMIService = GetObject("winmgmts:\\" _
        & strComputer& "\root\CIMV2") 

    Set colItems = objWMIService.ExecQuery( _
        "SELECT * FROM Win32_Service WHERE State = 'Stopped' and StartMode = 'Auto'")
    
    if  Err.Number = 424 then 
        myVariable =  "Date Time: " & datetime & ", " &  "Server: " & strComputer & " not able to connect"  
        objTextFile.WriteLine(myVariable)
        objTextFile.WriteLine(" ")
    else    
     dim myVariable  
     For Each objItem in colItems 
      myVariable =   "Date Time: " & datetime & ", " & _ 
                     "Server: " & strComputer & ", " & _
                     "Name: " & objItem.Name & ", " & _
                     "Status: " & objItem.State & ", " & _
                     "Start Mode: " & objItem.StartMode 
       if strComputer <> "" Then
             'WScript.Echo myVariable
             objTextFile.WriteLine(myVariable)
             clear myVariable
       end if
      
       Next
    objTextFile.WriteLine(" ")
    end if
 
Next
objTextFile.WriteLine(" ")
objTextFile.WriteLine(" ")
objTextFile.WriteLine("          *** End of Report ***          ")
objTextFile.Close
```


----------

