Vbscript fundamentals for windows scripting - the basics




















It is the schema that defines not only what type of information may be assigned to the First Name attribute, but also what the relationship is between the First Name attribute and the User object. It is important to note here that while objects in the Active Directory are static for the most part, objects referenced in WMI are not. The state of the computer is dynamic and as such, a true repository, or snapshot, of the system is not possible.

It is this design that can cause WMI scripts to perform slowly as they must retrieve new values each time they are run by sending requests to the CIM Repository. Querying or changing large amounts of information from several machines can take a while to complete using WMI due to the need to constantly obtain refreshed information.

The easiest way to learn about the classes, properties, and methods in the CIM repository and their relationships is to browse the repository itself. There are two tools that allow you to open the repository and visualize the interaction of these items. Both are beneficial in examining the repository, but the CIM Studio utility offers a little more information and is a little easier to read. This is the highest layer in the Infrastructure model and the easiest to understand.

In a nutshell, the Consumer is the script you write or the application you run that launches WMI code in the background. This is the WMI schema that is used to store the class definitions that are used in modeling managed resources. It is the CIM definitions that allow us to make minor modifications to scripts and obtain completely different output as shown in the following two scripts:. Name Wscript. BuildNumber Wscript. Manufacturer Next. The above script returns information regarding the Operating System currently installed on the local machine.

Making some slight modifications to the script as shown below will return information about shares on the local machine. Description Wscript. Path Next. The CIM allows us to obtain completely different results from a script without re-writing the entire script.

This tool enumerates every property for all available classes on the machines by simply selecting the appropriate class. Namespaces are used to logically contain similar classes within the CIM. These are similar to folder path structures on a logical drive and follow the same rules in that while you cannot have two files with the same name in the same folder path, it is not possible to have two classes with the same name in a shared namespace. For a detailed view of the WMI Schema namespace, click here.

This can be changed, but unless necessary for advanced scripting, it will usually be found within that context. You can find out what the current default namespace is on a machine by running the following script:. Name Next End Sub. The CIM Repository contains several class categories. While the details of the categories are beyond the scope of this article, understanding the differences in the classes and the class types can be useful in determining classes to use in your scripting.

More information on class categories and class types can be found here. The WMI classes represent the templates mentioned earlier that define the manageability of the resource instances within WMI. All processes in this class which are the processes on the target machine share the same properties which can be queried or managed using WMI. Properties are used to further describe the managed resources.

For example, a NIC card has a description name, an OS installation has a build number, a monitor has a refresh rate, etc. ExecutablePath Wscript. Name Next. Methods are actions that can be taken directly upon a managed resource. Services present the best examples to demonstrate methods because of the actions that can be performed on them including stop, start, pause, install, delete, etc.

In the following example we will SWbemObject in conjunction with a method to stop the Alerter service:. Qualifiers offer additional information about properties, classes, and methods. They define the operations of the called method, class or property. Using the example in the Properties section earlier, it is the qualifier that defines the properties used in the fact that the information retrieved is to be stored in an array within the control loop.

To better understand SWbemObject, it helps to understand what an object path is. The object path can be represented by the following:. The output would resemble the following:.

WMI Object Model. Again, Microsoft has a nice flowchart of the library object model for WMI which can be found here.

This will greatly aid in visualizing the process flow between the objects and instances mentioned so far. Take a few minutes to view the diagram and understand the processes occurring between objects in the library object model when different methods are called.

This article will not go into details surrounding the object model; however, the following script will demonstrate the object calls made within the model. In the first line, we define the variable strComputer, next we create a reference to the SWbemLocator which resides at the top of the object model library use the diagram for reference using the CreateObject function. This is only necessary if you are running the script in the context of a user account without administrative privileges on the remote computer.

For general WMI scripts used for tasks such as finding all the services on a machine, it is not completely necessary to understand every aspect of the WMI object model. When using WMI to obtain information, it is not always desired to obtain all information regarding every instance of the object class being queried.

Take the Event Logs for example. If you were to run the following script:. EventCode Wscript. Operators and Control Flow Statements — Operators are used to compare expressions.

An operator would compare the two and return a response or do something else based on the results such as:. Obviously 1 is not greater than 2 so the statement returns as True even though 2 is greater than 1 because of the way the logic operator was used. As all things in VBScript, this can be as complex or as simple as you make it depending on what your needs are.

Do 'outer loop 5. Do While count. The lines have been numbered for ease of explanation. Comments have been added to the right of each line to explain functionality. Space inserted to separate the variable declaration statements from the script function. Simply makes the script easier to read and is not required.

This type of loop flow control will continue until a condition becomes either True or False depending on what is specified. Defining the conditional constraints of the loop statement. Incrementing the count variable by 1 each time the statement loops. This displays the current count number in a pop-up window on your screen.

The conditional statement. Until the COUNT variable meets this condition, the statement will continue to loop and increment itself. Assigning a new value to variable X. Since the condition has now been met, we exit the second, or inner, loop. This statement is required to show the cessation of the IF loop in line 8. This is required for the script to know where and when to stop the flow control loop statement. This is the closing statement for the Do loop in line 5.

Also required to tell the script when to stop the flow control loop statement from the inner Do loop. This statement closes the outer, or first, Do loop once the condition is met. By this time, the COUNT variable has been incremented to a value of 10 so the variable X has been changed to FALSE meeting the criteria of this condition, therefore the outer loop is closed and the script is complete.

Arrays — When you need to carry out administrative tasks multiple times, arrays offer a method to store multiple values. The Array function is used to assign a set of values to a variable. The following example assigns three colors to the array variable named Colors:.

It is also possible to create an array variable without using the actual Array function statement by declaring the variable at the beginning of your script using the Dim statement.

Declaring a variable simply tells the script that the name you are declaring will be used as a variable within the script. For example:. This statement tells the script that we are declaring a variable named Colors as an array and that it will contain three values. Yes, I said three values. The first item in every array is actually designated as item 0 which must be taken into consideration when calculating the total amount of objects in the array.

In this case, we have three colors so starting at zero we count 0, 1, 2 or simply subtract one from the total values to be used.

This next script will read all of the computers in the domain and write the results to a textfile:. Dim Container 2. Dim ContainerName 3.

Dim Computer 4. Dim fso 5. Dim outfile 6. FileSystemObject" 9. For Each Computer In Container Name Declare the Container variable. Declare the ContainerName variable. Declare the Computer variable. Declare the fso variable. Declare the outfile variable. Space for separation of declarations from the script function. Use the CreateObject method to bind the Scripting. FileSystemObject object as a value for the fso variable. This means that fsois now an object for use within the script. Regarding the parameters at the end of the statement; the 2 means the file may be overwritten, and the True designates a Unicode file false would create an Ascii file.

Here is where we declare the Dynamic Array.



0コメント

  • 1000 / 1000