Some tips of wmi scripting in jscript (1)

Source: Internet
Author: User
Tags odrive

The concept of set is often used in windows scripts, especially in wmi scripts.
The method For traversing a set in vbscript is simple, and the For Each loop can achieve the goal. But in jscript
How to use collections has plagued me for a long time. I was pessimistic that I could not find any good examples in msdn.
Jscript cannot perform set operations.

When I almost gave up, I turned out another vbscript wmi e-book and found jscript.
The method of using the set, that is, the Enumerator object, which is specially used as an enumeration set. You will see this object
What do you think? Do you think you are familiar with it? How to enumerate Drives Files if you have called FSO using jscript?
And Folders? The FSO example is clearly written, and whether to move the example to the dead
In your program? At that time, I would also find it strange why it is so troublesome to write data for traversing a set, but I did not go into the details.
Why do you want to write it like this? At that time, because you didn't work on the array set, this is the only way to traverse the set in jscript.

At this time, you may still be wondering, what is the difference between a set and an array? Reference the original statement in MS script help: Set and number
The difference between a group is that the members of the set cannot access the group directly. Unlike the subscript used to process arrays
The pointer moves to the next or previous element of the set. Here you want to go deeper and understand that arrays are equivalent
The array concept in the language is a linear storage space that can be easily accessed by subscript, while the set is a complex
Data structure, such as a linked list, you can only access the previous or
The latter element.

The usage of Enumerator is very simple. After you pass the traversal set to the constructor of the Enumerator object as a parameter,
You can enumerate the members of the set. The atEnd method is used to determine whether it has reached the end. 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 the set through the item method.
A single element.

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 + ":");
}

EnDrives. moveNext ();
}
Example 2: Enumerate all processes through wmi


/**//*
* 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 ();
}

I also discussed the advantages and disadvantages between vbscript and jscript with Long a few days ago.
Lattice is much worse than jscript, but jscript also has some functional implementation defects. For example, there is no way to perform byte
Operation, but in general, jscript is a smart language, which can be seen from Enumerator.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.