Guy’s Scripting Ezine 35 – FSO (Files)

Contents for Guy’s Scripting Ezine 35 – FSO (File System Object)

This Week’s Secret

To me, a well-designed script is an object of beauty.  If VBScript represents the whole canvas, then FSO (File System Object) plays a delightful cameo.  FSO opens up scripting in so many ways.  Firstly, it provides an alternative to the message box for outputting information.  Secondly, the FSO methods give you perspective on what VBScript and WSH can achieve.  What I mean is that as a result of reading previous ezines, you may have begun to take, CreateObject("Wscript.Network") for granted, well now we have a new object: CreateObject("Scripting.FileSystemObject").

Guy Recommends: The Free IP Address Tracker (IPAT) IP Tracker

Calculating IP Address ranges is a black art, which many network managers solve by creating custom Excel spreadsheets.  IPAT cracks this problem of allocating IP addresses in networks in two ways:

For Mr Organized there is a nifty subnet calculator, you enter the network address and the subnet mask, then IPAT works out the usable addresses and their ranges. 

For Mr Lazy IPAT discovers and then displays the IP addresses of existing computers. Download the Free IP Address Tracker

FileSystemObject

When designing scripts it’s often convenient to be able to output information to files, instead of echoing the properties of services to screen.  This week’s example is a case in point, where we creating files and we can analyse the data at our leisure.  So the key command is strPath.WriteLine, instead of WScript.Echo.  (Where strPath is a folder on your machine.)

Such is the power of FSO, that in other circumstances, you may wish to manipulate files on the disk.  In which case experiment with create, move, or delete files in Ezine 36.  There may also be times when you want to interrogate or manipulate drives attached to your server.  (These FSO techniques also works for Active Server Pages.)

Example 1 – VBScript to interrogate services.

The idea behind this script is to check which services your operating system is running and then write the information to a text file.  If you open up Administrative Tools, Services, then you can see the items that my script will export to a text file.  Hopefully, this example will generate ideas for other objects that you could script.  However, first things first, let us get this straightforward method working.

Instructions

  1. Alter this line to a real path on your machine:
    strPath = "E:\ezine\scripts\ezine35\Services.txt"
  2. Copy and paste the script below into notepad.  Alternatively, use a script editor like VBsEdit.
  3. Save the file with .vbs extension e.g. Services.vbs.
  4. Double click and navigate to the file in your strPath.  This week there is no WScript.echo message, but I am sure that you could add : wscript.echo "Path" & strPath if you wanted a confirmation message.

‘ Services.vbs – Writes services to a file.
‘ Author Guy Thomas https://computerperformance.co.uk/
‘ Version 3.2 – June 27th 2004
‘ ——————————————————————–‘
Option Explicit
Dim objfso, objWMIService, objItem, colItems
Dim strPath, strFile, strComputer

strPath = "E:\ezine\scripts\ezine35\Services.txt"
strComputer = "."
Set objfso = CreateObject("Scripting.FileSystemObject")
Set strFile = objfso.CreateTextFile(strPath, True)
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_Service",,48)
For Each objItem in colItems
strFile.WriteLine("DisplayName: " & objItem.DisplayName)
strFile.WriteLine("Name: " & objItem.Name)
strFile.WriteLine("PathName: " & objItem.PathName)
strFile.WriteLine("ServiceType: " & objItem.ServiceType)
strFile.WriteLine("Started: " & objItem.Started)
strFile.WriteLine("StartMode: " & objItem.StartMode)
strFile.WriteLine("State: " & objItem.State)
strFile.WriteLine("Status: " & objItem.Status)
Next
strFile.Close

Wscript.Quit

‘ End of example VBScript

Learning Points

Note 1: Set objfso = CreateObject("Scripting.FileSystemObject")
Here is where we tell VBScript to connect to the file system.

Note 2:Set strFile = objfso.CreateTextFile(strPath, True)
creates the actual text file in the strPath.  True means overwrite the file.

Note 3: The reason that it returns services is:  Query Win32_Service on line 14.  I have a whole section here dealing with the WMI section of the script.

Note 4: .WriteLine method which adds each property or objItem on a new line.

Note 5: Spot the loop:  For Each…. Next.

Guy’s Challenges for extending this script.

If you like this script, then extend the principles by adjusting the line:
Set colItems = objWMIService.ExecQuery("Select * from Win32_Service",,48).  See here for other Win32 objects that you could substitute.

If you accept this challenge, then remember to replace Wscript.echo with strFile.WriteLine.  Also, do not be afraid to rip out unwanted properties.

Guy Recommends:  A Free Trial of the Network Performance Monitor (NPM)Review of Orion NPM v11.5 v11.5

SolarWinds’ Orion performance monitor will help you discover what’s happening on your network.  This utility will also guide you through troubleshooting; the dashboard will indicate whether the root cause is a broken link, faulty equipment or resource overload.

What I like best is the way NPM suggests solutions to network problems.  Its also has the ability to monitor the health of individual VMware virtual machines.  If you are interested in troubleshooting, and creating network maps, then I recommend that you try NPM now.

Download a free trial of Solarwinds’ Network Performance Monitor

Example 2 – VBScript to return only ‘Manual’ Service

The key amendment in this second script is to filter the services.  As a result we only write to file services where the StartMode is set to ‘Manual’.  (As opposed to those set to Automatic or Disabled.)

Do remember to amend the strPath, especially if you get:
Code Error 800A004C – Path not found

‘ ServicesManual.vbs – Writes services to a file.
‘ Author Guy Thomas https://computerperformance.co.uk/
‘ Version 3.7 – June 27th 2004
‘ ————————————————————-‘
Option Explicit
Dim objfso, objWMIService, objItem, colItems
Dim strPath, strFile, strComputer

strPath = "E:\ezine\scripts\ezine35\ServicesManual.txt"
strComputer = "."
Set objfso = CreateObject("Scripting.FileSystemObject")
Set strFile = objfso.CreateTextFile(strPath, True)
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_Service",,48)
For Each objItem in colItems

If objItem.StartMode = "Manual" Then

strFile.WriteLine("DisplayName: " & objItem.DisplayName)
strFile.WriteLine("Name: " & objItem.Name)
strFile.WriteLine("PathName: " & objItem.PathName)
strFile.WriteLine("ServiceType: " & objItem.ServiceType)
strFile.WriteLine("Started: " & objItem.Started)
strFile.WriteLine("StartMode: " & objItem.StartMode)
strFile.WriteLine("State: " & objItem.State)
strFile.WriteLine("Status: " & objItem.Status)

strFile.WriteLine("")
End if

Next
strFile.Close

Wscript.Quit

‘ End of example VBScript
 

Learning Points

Note 1:  Here I have selected service set to "Manual" by using the:  If…Then, End If construction.

Note 2:  To make the file more readable, I have added a blank line with strFile.WriteLine("")

Do remember to amend the strPath (Beware Error: 800A004C – Path not found)

Ideas for extending this script.

Want some more fun? Try other filters, for example objItem.Started = "True"

Guy’s Challenge – try and solve this ‘Out take’

It’s a while since I have had an out take, a deliberate mistake.  See if you can solve this problem.

‘ ServicesManual.vbs – Writes services to a file.
‘ Author Guy Thomas https://computerperformance.co.uk/
‘ Version 2.5 – June 26th 2004
‘ ————————————————————-‘
Option Explicit
Dim objfso, objWMIService, objItem, colItems
Dim strPath, strFile

On Error Resume Next

strPath = "E:\ezine\scripts\ezine35\ServicesOut.txt"
strComputer = "."
Set objfso = CreateObject("Scripting.FileSystemObject")
Set strFile = objfso.CreateTextFile(strPath, True)
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_Service",,48)
For Each objItem in colItems

If StartMode = "Manual" Then

strFile.WriteLine("DisplayName: " & objItem.DisplayName)
strFile.WriteLine("Name: " & objItem.Name)
strFile.WriteLine("PathName: " & objItem.PathName)
strFile.WriteLine("ServiceType: " & objItem.ServiceType)
strFile.WriteLine("Started: " & objItem.Started)
strFile.WriteLine("StartMode: " & objItem.StartMode)
strFile.WriteLine("State: " & objItem.State)
strFile.WriteLine("Status: " & objItem.Status)

strFile.WriteLine("")
End if

Next
strFile.Close

Wscript.Quit

‘ End of example VBScript

Answer

The Line: ‘On Error Resume Next’ suppresses an error.  Remove that line and you should be able to solve the problemS easily.

If objItem.StartMode = "Manual" Then
Not:If StartMode = "Manual" Then

Dim strPath, strFile, strComputer
Not:Dim strPath, strFile

Summary

FileSystemObject is a versatile command which writes VBScript output to files.  FSO opens up new avenues for scripts to handle file content.  For example, you could create a script which writes all your computers services to a text file.  Other applications for FSO include copying files between drives.

See more about scripting files with VBScript

VBScripts  • Ezines  • WMI  • Create files  • File Open  • Ezine 12 FSO  • Permissions Analyzer

• Ezine 35 FSO  • Ezine 36 FSO II  • Ezine 43 File Create  • PowerShell Create Files

Ezine 78 Files  •Ezine 79 Files  • Ezine 104 FSO  •Ezine 139 Delete Temporary