EV Reach - WMI Explorer - Using the WMI Script Builder

Last modified on 2023/07/12 14:02

EV Reach's WMI Explorer includes a VisualBasic and PowerShell GUI Builder which can be triggered at any point while you use the WMIX interface. The Script builder creates complete WMI Scripts that can be executed at the command prompt. You can also select to use only parts of the generated script in your own scripts.

Exporting Scripts with the Script Builder

EV Reach's WMI Explorer Script Builder can export either PowerShell or Visual Basic Scripts. To set an export default, click the Generate Script menu option. You can set the default script export, or choose to have WMIX prompt you each time.

wmix12.png

Once you have set a default, whenever you right-click a WMI component and choose Export Query as a Script...

wmix13.png

...a window will open containing the exported script in the specfied format.

The Script Generator can be called from any of the WMIX Views: Browser View, Namespace & Class View and WMI Query View. It is accessible either via the context sensitive menu of an object, via the Information Panel or by clicking on the Generate Script button located in the Ribbon bar.

Using the Script Generator, you will be able to:

  • Create a script which reports the instances of a class or a specific class instance.
  • Create a script which modifies the values of writable class attributes.
  • Create a script which executes a class's method.
  • Create a script which executes a WQL query.
  • Create a script which executes one of the pre-configured WMIX reports.

Script Builder: Options

The Script Generator currently has only one option which controls whether or not you are prompted for a file name & path each time you generate a script. This option can be configured in the General Settings tab of WMIX Options.

  • Check Automatically show generated scripts. Do not prompt me to 'Save As' if you do not want to be prompted for a filename and path each time you generate a script.In this mode, each time you generate a script, WMIX creates a temporary file called WMIX_Script.vbs in the default TEMP folder, inserts the WMI Script in it, and displays the script by opening the file.  This mode is the best way to quickly test generated script or to copy parts of WMIX generated scripts and paste them into your own script.
  • Un-check this option if you want to be prompted for a file name and path each time you generate a script.

Script Builder: General Format

No matter what type of script you decide to create, the general format of the script is always the same. It is composed of a Main Loop which calls one or more script function for each target computer. The selected script actions are placed into script functions which are in turn called by the Main Loop. This format makes it easy for the functions to be copied from the generated script and pasted into your own script.

'*********** START OF MAIN LOOP>>
For Each strComputer IN arrComputers
execute_WMIfunction1(strComputer)
execute_WMIfunction2(strComptuer)
Next
'<< END OF MAIN LOOP**************
Function execute_WMIfunction1()
...
End Function
Function execute_WMIfunction2()
...
End Function

The list of target machines is defined by the command line parameters:

  • If you do not specify any target machine at the command line, the local machine is queried. Example: > CScript WMIX_Script.vbs
  • To execute the script on a particular machine, specify the machine's name or IP address after the script. Example: > CScript WMIX_Script.vbs 192.235.11.62 > CScript WMIX_Script.vbs macName_01
  • To execute the script on a set of machines, separate the list of machine names or IP address with commas. Example: > CScript WMIX_Script.vbs 192.235.11.62,macName_022,192.235.205.144Note: There cannot be any spaces between the machine names and commas. The following command line will not work: > CScript WMIX_Script.vbs 192.235.11.62 , macName_022 , 192.235.205.144

Script Builder: Reporting Class Information

WMIX can be used to generate scripts which queries the instances of a WMI class or the properties of a single instance of a WMI class.

WMIX automatically creates all script support functions to:

  • Translate WMI mapped property values from their numerical form to their friendly string format.
  • Translate array values and date & time values to a friendly string format.
  • Append the value unit string if the property has a unit.

To generate a Class Information Report Script

  1. Select a class object from the Browser View or the Namespace & Class View.
  2. Right click on the mouse and select Script: Query Class Instances or click on the Script: Query Class Instance link in the Information Panel or click on the button in the Ribbon bar.

    wmix14.png

Depending on your selection, a WMI Class Definition Object or a WMI Class Instance Object, you will be presented with different scripting choices.

  • If the selected class is a Definition Class (such as a root object in the Browser View or any class object in the Namespace & Class View), you can only choose to generate a script which queries the instances of that class:

    wmix15.png

    Class Instances Scripts generates the following script action type: Set wbemObjectSet = wbemServices.InstancesOf("Win32_className")
  • If the selected class object is an instance of a WMI class, you can choose to generate a script which queries the selected instance or a script which queries all of the instances of the selected class definition.

    wmix16.png

    If you select to Query This Instance's Properties, then the script will focus on the selected instance. For example, the above selection would generate the following script line:Set objWMIClass = GetObject("winmgmts:"&strComputer&""&"rootCIMV2"&":"&"Win32_Share.Name='C$'")

Reporting Class Association Information

WMIX can also be used to query the associated instances of a WMI Class with another WMI Class. To do so, browse through the associations of an object in the Browser View (see: Displaying Object Associations), select a resulting class instance or its parent class, and then generate a script to Query Class Instances.

For example, to browse through the associations of a Share instance to disclose the Directory association, expand the share directory instance to disclose theDirectory Contains File association and query its instances. You should end up with a structure similar to this one:

wmix17.png

Then right-click on the All File (CIM) class object and select Script: Query Class Instances.

If the All File (CIM) class would have been a root object, then the resulting script would query information from all files on the system. However, since this class is the result of an association with the C: directory, the resulting script only queries the instances of the files within that directory. The following code is generated:

Set wbemObjectSet = wbemServices.ExecQuery("ASSOCIATORS OF {Win32_Directory.Name='c:'} WHERE AssocClass=CIM_DirectoryContainsFile ResultClass=CIM_DataFile")

Setting Class Property Values

WMIX can generate script to set the value of writable properties of a class. To do so, open the properties window of a class instance (see: View Class Instance Properties), modify the writable properties to the desired values and click on the Export as a Script button.

wmix6-1.png

Executing a Class Method

WMIX can be used to generate scripts which execute a method of a WMI class. To do so, open the WMIX Execute Method dialog (see: Executing Management Tasks), enter the method parameter values if applicable, then click on the Export as a Script button.

wmix18.png

Executing WMI Queries

To generate a script which executes a specific WMI Query, select the Query WMI View, enter the WMI Query to execute, and then click on the button in the Ribbon bar.

wmix14.png

Generating a Script for a Pre-configured Report

WMIX can create a WMI script which queries the information as configured in a pre-configured report template (see: Report Manager).

To do so:

  1. Click on the Report Wizard button in the ribbon bar.
  2. Click on Next in the WMIX Report Wizard and select the report template to use for the script.
  3. Click on the Script button .

You can also click on the Report Selection Extended menu and select Export Report as a Script.

Tags:
Powered by XWiki © EasyVista 2022