Previous Page
Next Page

Using an Operator

One of the good things you can do is use greater than and less than operators in your evaluation clause. You might wonder what is so good about greater than. It makes working with alphabetic characters and numeric characters easy. If you work on a server that hosts home directories for users (which are often named after their user names), you can easily produce a list of all home directories from the letters T through Z by using the > S expression. This is illustrated in the ListSpecificGreaterThanShares.vbs script.

ListSpecificGreaterThanShares.vbs

Option Explicit
'On Error Resume Next
Dim strComputer
Dim wmiNS
Dim wmiQuery
Dim objWMIService
Dim colItems
Dim objItem

strComputer = "."
wmiNS = "\root\cimv2"
wmiQuery = "Select Name, path, allowMaximum from win32_Share where name > 's'"
Set objWMIService = GetObject("winmgmts:\\" & strComputer & wmiNS)
Set colItems = objWMIService.ExecQuery(wmiQuery)

For Each objItem in colItems
  WScript.Echo "Name: " & objItem.Name
  WScript.Echo "Path: " & objItem.path
  WScript.Echo "AllowMaximum: " & objItem.AllowMaximum
  WScript.Echo VbCrLf
Next

There are many other available operators in Microsoft Visual Basic, Scripting Edition (VBScript) as well. These operators are listed in Table 10-2.

Table 10-2. VBScript Operators

Operator

Description

=

Equal to

<

Less than

>

Greater than

<=

Less than or equal to

>=

Greater than or equal to

!=

Not equal to

<>

Not equal to (both != and <> mean not equal to)


Identifying service accounts

1.
Open the \My Documents\Microsoft Press\VBScriptSBS\Templates\wmiTemplate.vbs script and save it as YourNameServiceAccount.vbs.

2.
Modify the WMI query to select the start name and the started status from the WIN32_service WMI class. But only do this if the name used to start the service is not equal to localSystem. This query is seen below.

wmiQuery = "Select StartName, started from win32_service" &_
   " where startName <> 'localSystem'"

3.
Inside the For Each...Next loop, delete all the WScript.Echo commands except for one.

4.
Modify the output to print out the name of the service, the name used to start the service, and the status of the service. Use the intrinsic constant VbCrLf to make a new line. Use intrinsic constant vbTab to tab between properties. This code is seen below:

WScript.Echo objItem.name, VbCrLf & vbTab &_
  objItem.StartName & vbTab & "Running: " & objItem.Started

5.
Save and run the script by using CScript. The output will look similar to the following printout.

Alerter
   NT AUTHORITY\LocalService   Running: False
ALG
   NT AUTHORITY\LocalService   Running: False
aspnet_state
   NT AUTHORITY\NetworkService Running: False

6.
If your script does not run as expected, compare it to the \My Documents\Microsoft Press\VBScriptSBS\ch10\ServiceAccount.vbs script.

Logging the service accounts

1.
Open the \My Documents\Microsoft Press\VBScriptSBS\ch10\ServiceAccount.vbs script and save it as YourNameServiceAccountLogged.vbs.

2.
Open the \My Documents\Microsoft Press\VBScriptSBS\ch10\LoggedRunning Processes.vbs script and copy the subText subroutine, as well as the funfix function.

Paste both of these to the bottom of your script. The code you will copy looks like the following:

' **** subs below ****
Sub subText(strIN)
Dim objFSO           'the filesystemobject
Dim objFile          'file object
Dim objShell         'wshshell object
Dim strPath          'path to desktop
Dim strFile          'log file name

Const ForAppending = 8
Const CreateFile = True
strFile = funfix("logProps.csv") 'adds \ to file name

Set objShell = CreateObject("WScript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")
strPath = objShell.SpecialFolders("desktop")
strFile = strPath & strFile
Set objFile = objfso.OpenTextFile(strFile,ForAppending,createFile)
objFile.WriteLine (strIN)
End Sub

Function funfix (strIN)
funfix = "\" & strin
End Function

3.
In the subroutine, locate the line that assigns the file name to the strFile variable. Change the name of the file to LogService.csv. This is seen below:

strFile = funfix("logService.csv")

4.
Inside the For Each...Next loop of the main script, we need to remove the WScript.Echo statement and the VbCrLf statements, and intersperse the properties with commas instead. We need to declare a new variable called strValues and then assign our Worker section to this variable. The code to be placed in the For Each...Next loop is seen here:

strValues = objItem.name & "," & objItem.StartName & _
   "," & objItem.started

5.
On the line below the new line to be placed inside the For Each...Next loop, call the subroutine and pass the strValues variable as an input parameter to the sub. This is seen here:

subText(strValues)

6.
Save and run the script. A .csv file will appear on the desktop. The results will look like the spreadsheet in Figure 10-3. If they do not, then compare your script with the \My Documents\Microsoft Press\VBScriptSBS\ch10\ServiceAccountLogged.vbs script.

Figure 10-3. To display data that is opened in Excel, use .csv files



Previous Page
Next Page