Some Tips of WMI scripting in JScript (1) _javascript tips

Source: Internet
Author: User
Tags odrive
The concept of collections is common in Windows scripts, especially in WMI-related scripts, where collection operations are generally encountered.
The way to iterate through the collection in VBScript is simple, for Each loop can achieve the purpose. However, in JScript
How to use the collection, but it bothered me for a long time, even the MSDN can not find good examples, so I was once pessimistic that
JScript cannot perform collection operations.

When I almost gave up, I turned out a VBScript WMI ebook and looked at it and found JScript
The method that uses the collection is the enumerator object, which is used specifically as an enumeration collection. See this object you will
What do you think? Do you think it looks familiar? How to enumerate drives Files if you have called the FSO with JScript
and the folders? FSO in the example written plainly, at that time when the use of the zombie-like to move the example to
In your program? It would have been strange to know why it went through a collection to write such trouble, but did not delve into
Why do you write this, because that's not going to work for your array, which is the only way to traverse a collection in JScript.

At this point you may still have questions, what is the difference between sets and arrays? Quotes from Ms Script help: Collections and Numbers
The different points of a group are that members of the collection cannot be accessed directly. Unlike using subscripts when working with arrays, this only works with the current project
The pointer moves to the next or previous element of the collection. Here you want to go a little deeper, so you can understand that the array is equivalent to C
The array concept in language is a linear storage space that can be easily accessed by subscript, while a collection is a complex
Data structure, such as a linked list, you can access only through the node point to the relationship between the previous or
One element after the other.

The use of enumerator is very simple, after you pass the collection as a parameter to the constructor of the enumerator object,
You can enumerate the members of the set, and the AtEnd method to determine whether it has ended, the MoveFirst method can move the pointer
To the first element, the MoveNext method moves the current pointer position to the next element, returning the set by the Item method
A single element in a combination.

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: Enumerating 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 ();
}

A few days ago, I talked to long about the pros and cons between VBScript and JScript, VBScript in language features and code wind
Gerry is a lot worse than JScript, but JScript also has some drawbacks with functionality implementations, such as the lack of a way to byte
operation, but overall JScript is a clever language that can be seen from the enumerator, illustrious.

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.