Access WMI functions

Hi,

Is it possible to access (read/execute) Windows WMI functions from Scripter?

I don't much about WMI. In theory Scripter can execute anything, it's just Dephi. Either you could some import DLL functions directly, if the parameter and result types are supported, or you can simply add Delphi wrappers to the API/DLL and then register those Delphi wrappers in scripter to be called from it.

Now if you mean something more specific in terms of integration, then I'd like to hear what you are looking for.
Hi Wagner,

I would like to run the following script.

Dim objService, objVolumes, objVolume, nError, bDefragRecommended, objDA
Set objService = GetObject("winmgmts:root\cimv2")
Set objVolumes = objService.ExecQuery("Select * from Win32_Volume Where Name = 'C:\\'")
For Each objVolume In objVolumes
    nError = objVolume.DefragAnalysis(bDefragRecommended, objDA)
    If Not nError Then
        Wscript.Echo objDA.FilePercentFragmentation & "% of files are fragmented."
    If bDefragRecommended Then
        Wscript.Echo "This volume should be defragged."
    Else
        Wscript.Echo "This volume does not need to be defragged."
    End If
End If
Next

I'm not sure what is Wscript? Is this a COM object?

TMS Scripter supports creating COM objects, probably you can learn and create COM objects for it and use the properties. For example:



  Locator :=   CreateOleObject('WbemScripting.SWbemLocator');        
  WMIService   := Locator.ConnectServer('', 'root\CIMV2', '', '');
  WMIService.ExecQuery ...


and so on
The code is VBS script.

What kind of Basic script is supported?

Scripter supports Basic syntax. That would be something like this:




DIM Locator
DIM WMIService


Locator = CreateOleObject("WbemScripting.SWbemLocator")        
WMIService = Locator.ConnectServer("", "root\CIMV2", "", "")
 WMIService.ExecQuery ...

DIM Locator        
DIM WMIService
DIM Volume
DIM DeviceID        

Locator = CreateOleObject("WbemScripting.SWbemLocator")         
WMIService = Locator.ConnectServer("", "root\CIMV2", "", "")
Volume = WMIService.ExecQuery("SELECT * FROM Win32_Volume WHERE name='C'")   

DeviceID=Volume.DeviceID                                                                                     

The last line causes a runtime error. Is it possible to access values from Volume?

                         

I can't test it as I don't know how to use WMI nor how to test it. But if ExecQuery returns a set, you should probably iterate through it:


Volumes = WMIService.ExecQuery("SELECT * FROM Win32_Volume WHERE name='C'")   
For I = 0 to Volumes.Count
  Volume = Volumes.Item(I)
Next
I get the following error.

RUNTIME ERROR
Status code: -2147352567
Source: SWbemObjectSet
Description: Invalid query when evaluating instruction CallProc (SFFFFFFFF,SO,S1904C854,S3E237D70,'Count'). Stack content is: [(unknown),(unknown),(unknown)].
Source position: 11,27 Position: 11, 27.

Is it possible to check the value of Volumes via debugger?

Unfortunately not. You will have to find the documentation of SWbemObjectSet:

https://docs.microsoft.com/en-us/windows/win32/wmisdk/swbemobjectset
Maybe Length is available
The following code works.

DIM Locator        
DIM WMIService
DIM Volume    
DIM nError

Locator = CreateOleObject("WbemScripting.SWbemLocator")         
WMIService = Locator.ConnectServer("", "root\CIMV2", "", "")      
Volume = WMIService.ExecQuery("Select * from Win32_Volume WHERE Name='C:\\'")
showmessage("Caption: " & Volume.ItemIndex(0).Caption)
showmessage("DeviceID: " & Volume.ItemIndex(0).DeviceID)   

Now I need to set DefragAnalysis parameters.



Hello Michael,

I don't know what is DefragAnalysis.
Hi Wagner,

DefragAnalysis is a method to see if a volume needs to be defragmented.

https://docs.microsoft.com/en-us/previous-versions/windows/desktop/vdswmi/defraganalysis-method-in-class-win32-volume

Wrap it in a Delphi function and register the Delphi function in the scripter.

Thank you. Will try that.