Home > Web Front-end > JS Tutorial > Some tips of wmi scripting in jscript (1)_javascript技巧

Some tips of wmi scripting in jscript (1)_javascript技巧

WBOY
Release: 2016-05-16 19:15:45
Original
1131 people have browsed it

The concept of collection is very commonly used in Windows scripts, especially in WMI scripts, collection operations are basically encountered.
The method of traversing a collection in vbscript is very simple, the For Each loop can achieve the purpose. However, how to use collections in jscript has troubled me for a long time. I can't even find a good example in msdn, which made me pessimistically think that
jscript cannot perform collection operations.

When I was almost giving up, I dug out another vbscript wmi e-book and looked at it, and found jscript
The method of using collections is the Enumerator object, which is specially used for Enumeration collection. What do you
think about when you see this object? Does it look familiar? If you have used jscript to call FSO, how to enumerate Drives Files
and Folders? The FSO examples are clearly written. When you used them, did you move the examples into your program like a zombie? At that time, I also felt strange why it was so troublesome to write to traverse a collection, but I didn't delve into why
was written like this. At that time, because your method for arrays didn't work, traversing a collection in jscript could only do this.

At this time you may still have questions, what is the difference between sets and arrays? To quote the original words in MS Script Help: The difference between a set and a
group is that the members of the set cannot be accessed directly. Unlike using subscripts when working with arrays, this only moves the current item
pointer to the next or previous element in the collection. If you think a little deeper here, you can understand it this way. An array is equivalent to the array concept in the C
language. It is a linear storage space that can be easily accessed through subscripts, while a set is a complex
When accessing a data structure, such as a linked list, you can only access the previous or next element through the pointing relationship between nodes.

The usage of Enumerator is very simple. After passing the collection you want to traverse as a parameter to the constructor of the Enumerator object,
you can enumerate the members of the collection. The atEnd method determines whether the end has been reached. The moveFirst method can Move the pointer
to the first element, the moveNext method moves the current pointer position to the next element, and returns a single element in the set
through the item method.

Example 1: Enumerate all drives


/**//*
* cscript ListDrive.js
*/
var oFSO = new ActiveXObject("Scripting.FileSystemObject");
var enDrives = new Enumerator(oFSO.Drives);
var oDrive;
while (! enDrives.atEnd()) {
oDrive = enDrives.item();

if (oDrive .IsReady) {
               WScript.Echo(oDrive.DriveLetter    ":");                                                                                                                                                                                                                     . The process of


/**//*
* cscript ListProcess.js
*/
var sComputerName = ".";
var oLoc = new ActiveXObject("WbemScripting.SWbemLocator");
var oSvc = oLoc.ConnectServer(sComputerName, "root\cimv2");
var colItems = oSvc.ExecQuery("SELECT * FROM Win32_Process");
var enProcesses = new Enumerator(colItems);

while (! enProcesses.atEnd()) {
WScript.Echo(enProcesses.item().Name);
enProcesses.moveNext();
}

A few days ago it was still Long discussed the advantages and disadvantages between vbscript and jscript. vbscript is much worse than jscript in terms of language functions and code style, but jscript also has some shortcomings in functional implementation, such as the inability to perform byte operations. , but in general jscript is a smart language, which can be seen from Enumerator, hehe.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template