PowerShell Ezine, Logon Scripts

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".

ˆ

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 http://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.

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 http://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 http://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.

●  FSO Part 2 - Move Files    ● See more on FSO - Whole Section

Computer Training Software - Recommended Training VideosGuy Thomas recommends Computer Training Software

Their topics and material are ideal for getting you started with VBScript.  The videos are easy to follow and you can control the pace.  Try their free demo material and then see if you want to buy the full package. See more about VB Script Training CD.


 *


Google

Webcomputerperformance.co.uk

GFi Events Manager

Guy Recommends: GFi EventsManager

Here is a solution to monitor, manage and archive thousands of events that are generated by devices across your entire network.  Get your free evaluation copy of GFI EventsManager.

 

Home Copyright © 1999-2008 Computer Performance LTD All rights reserved

Please report a broken link, or an error.