• Aucun résultat trouvé

Modifying a Script

Mechanics of Two-Dimensional Arrays

Lab 9 Modifying a Script

You are the network administrator of a medium-sized company, and you have been studying scripting. You recently learned how to write a pretty cool script that will tell you which services are running in a process. You have been noticing some strangeness in the processor utilization on some of your servers, and you think you have traced it down to the way services run inside processes. To further investigate the issue, you want to modify a script you recently wrote (Lab 7) so that it can be fully automated.

Lab Instructions

1. Open the Lab9Starter.vbs file, and save it as lab9.vbs.

2. Edit the list of variables. Remove strComputer and colComputers because they won’t be used in the new script.

3. Since you’re going to feed a text file, you won’t need the code that references the Arguments collection. Therefore, remove the following lines of code:

If WScript.Arguments.count = 0 Then

WScript.Echo(“You must enter a computer name”) Else

strComputer = WScript.Arguments(0) colComputers = Split(strComputer, “,”)

Make sure you leave the line that is used to create the dictionary object. In addi­

tion, do not forget to get rid of the End If line at the bottom of the script. See lab9pt1.vbs to make sure you removed the correct lines of code.

4. Add code to accept a command-line text file. You’ll need to create a variable named txtFile for the text file and then point the variable to a valid text file on your computer. Inside the text file, you need a list of only those computer names reach-able on your network, separated by a comma. (Refer to my servers.txt file for a sample, or simply edit it to your needs.)

Next you create a constant called ForReading and set it equal to 1. This is a good way to simplify accessing the file. Now create the filesystem object by using the CreateObject(“Scripting.FileSystemObject”) command, which you set equal to the objFSO variable.

After you do that, open the text file by setting the objTextFile variable to be equal to objFSO.OpenTextFile—we feed this the variable for our text file and also the constant ForReading. Code for accomplishing all this follows. You place this code right below the Dim commands. You will need to add Dim commands for the fol­

lowing variables as well: TxtFile, objFSO, and objTextFile. This code is saved as lab9pt2.vbs.

TxtFile = “c:\scriptingBook\bookScripts_vbscript\ch4\lab9\Servers.txt"

Const ForReading = 1

Set objFSO = CreateObject(“Scripting.FileSystemObject”) Set objTextFile = objFSO.OpenTextFile _

(TxtFile, ForReading)

5. Go into the text file and parse out each line so that you know where to look for services and processes. To do this, use a Do Until loop. The interesting thing about this section of the code is that the loop is rather large, because you want to work with one computer at a time and query its services and processes prior to making another round of the loop. Therefore, placement of the outside Loop command is vital. In addition, you need to change the variable used in the For Each computer line, which follows the outside loop. Change Colcomputers to be arrServerList.

Also, add a variable for strNextLine and arrServerList to the Header information section of your script.

Do Until objTextFile.AtEndofStream strNextLine = objTextFile.Readline arrServerList = Split(strNextLine , “,”)

6. Save your file. You can compare your file with lab9pt3.vbs. This file now runs.

7. To keep track of how the script runs, add the following line just above the wmiRoot = “WinMgmts:\\ line:

WScript.Echo” Processes and services on “ & (computer)

8. To control the creation of the dictionary, move the line set objIdDictionary = CreateObject(“Scripting.Dictionary”) inside the For Each computer In arrServerList line. Save your file and compare it with lab9pt4.vbs, if you want to.

9. Add a new variable called j.

10. Change i to j in the following line: For i = 0 To objIdDictionary.Count – 1. This gives us a new counter the second time the script is run. In addition, edit two other lines and change colProcesses(i) to j as well.

11. To make sure you don’t reuse dictionary items the second time the script runs, remove all items from the dictionary by employing the objIdDictionary.RemoveAll command. You need to do this outside the For j loop but inside the For Each com­

puter loop. The completed section looks like the following:

For j = 0 To objIdDictionary.Count - 1

wmiQuery = “Select * from Win32_Service Where ProcessID = ’” & _ colProcessIDs(j) & “‘"

Set colServices = objWMIService.ExecQuery _ (wmiQuery)

WScript.Echo “Process ID: “ & colProcessIDs(j) For Each objService In colServices

WScript.Echo VbTab & objService.DisplayName Next

objIdDictionary.RemoveAll Next

Next Loop

WScript.Echo “all done”

This completes the lab. This section is saved in lab9pt5.vbs.

5 The Power of Many More

In this chapter, you’ll look at two very important concepts: dynamically creating arrays and creating dictionaries. Both of these techniques will enable you to create enter prise-class scripts that will quickly instantiate themselves into your day-to-day net-work operations.

Before You Begin

In order to work through the material presented in this chapter, you need to be familiar with the following concepts from earlier chapters:

Creating single dimension arrays

Creating two-dimensional arrays

Implementing the For Next construction

Implementing Select Case construction

After completing this chapter you will be familiar with the following:

Converting text files into arrays

Converting delimited strings into arrays

Working with dictionaries