Categories
Archives
Authors
 
22

.:.

When automating tasks with Windows Script Host in most of the time we must use objects such as WScript.Shell, WScript.Network, Scripting.FileSystemObject and others because they allow us to implement the actions that we need at the moment. The problems begin because many do not remember the syntax for creating each of these objects and thus we have to refer to the help or VBS code already developed. To facilitate the use of common objects such as Shell, FileSystemObject, Network and wmi we created the ObjectManager  Class which makes available these objects without having to believe that one of them, since the class is does for us. To use only we must include and is ready to operate.  

Include "ObjectManager.class"

 

After this instruction in our VBS code automatically have available an object named ObjectManager with which we can make use of the objects mentioned above through the following properties:

  • Shell: Return a reference to an object of type "WScript.Shell".
  • Net: Return a reference to an object of type "WScript.Network".
  • Fso: Return a reference to an object of type "Scripting.FileSystemObject".
  • Wmi: Return a reference to an object of type "winmgmts:\\" in local computer.

Let us now see some examples of its use: 

'Calling to WScript.Network Property
WScript.Echo ObjectManager.Net.ComputerName
'Calling to Scripting.FileSystemObject Method.
WScript.Echo ObjectManager.Fso.GetFile("ObjectManager.class").Path
'Calling to WScript.Shell Property
WScript.Echo ObjectManager.Shell.CurrentDirectory
'Calling to WMI (Windows Management Instrumentation) Method
Dim So
For Each So in ObjectManager.Wmi.ExecQuery("Select * from Win32_OperatingSystem")
	WScript.Echo "Operating System: " & So.Caption
	WScript.Echo "Version: " & So.Version 
	WScript.Echo "Service Pack: " & So.CSDVersion
Next

The Property wmi works by default with the namespace "root\cimv2" but through the method SetWmiNameSpace we can change it, let's see an example:

'Changing WMI (Windows Management Instrumentation) NameSpace to "StdRegProv"
ObjectManager.SetWmiNameSpace("root\default:StdRegProv")
const HKEY_LOCAL_MACHINE = &H80000002
Dim arrSubKeys,strSubKey
ObjectManager.Wmi.EnumKey HKEY_LOCAL_MACHINE, "SYSTEM\CurrentControlSet\Services", arrSubKeys
For Each strsubkey In arrSubKeys
    wscript.Echo strSubkey
Next

The complete code for these examples are available via our download page for the search criteria: "MySamples: Another way to use Shell, FileSystemObject and other objects in WSH/vbcript".

 

See Also:

Object Manager Class

Using Includes in vbscript

Simulando Clases Compartidas al estilo C# en vbscript

 

We apologize for any errors in the syntax and semantics of the articles in the English version, these were done with our current knowledge of the English language and with the support of the site http://translate.google.com.


Posted in: VbScript

Comentarios

Actualmente no hay comentarios, sera el primero en publicar uno.

Digite su comentario

Name (required)

Email (required)

Website

Enter the code shown above:

 
Inspired by Nina