Web forum is in read-only mode. Login as active registered customer for write access
  Forum Search   New Posts New Posts

Access WMI functions

 Post Reply Post Reply
Author
B Michael View Drop Down
New Member
New Member
Avatar

Joined: 23 Apr 2018
Posts: 2
Post Options Post Options   Quote B Michael Quote  Post ReplyReply Direct Link To This Post Topic: Access WMI functions
    Posted: 29 Mar 2020 at 5:28pm
Hi,

Is it possible to access (read/execute) Windows WMI functions from Scripter?
Back to Top
Wagner R. Landgraf View Drop Down
TMS Support
TMS Support
Avatar

Joined: 18 May 2010
Posts: 2888
Post Options Post Options   Quote Wagner R. Landgraf Quote  Post ReplyReply Direct Link To This Post Posted: 30 Mar 2020 at 4:03pm
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.
Back to Top
B Michael View Drop Down
New Member
New Member
Avatar

Joined: 23 Apr 2018
Posts: 2
Post Options Post Options   Quote B Michael Quote  Post ReplyReply Direct Link To This Post Posted: 30 Mar 2020 at 4:09pm
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

Back to Top
Wagner R. Landgraf View Drop Down
TMS Support
TMS Support
Avatar

Joined: 18 May 2010
Posts: 2888
Post Options Post Options   Quote Wagner R. Landgraf Quote  Post ReplyReply Direct Link To This Post Posted: 01 Apr 2020 at 5:04pm
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
Back to Top
Bleijerveld Michael View Drop Down
New Member
New Member
Avatar

Joined: 30 Mar 2020
Posts: 6
Post Options Post Options   Quote Bleijerveld Michael Quote  Post ReplyReply Direct Link To This Post Posted: 01 Apr 2020 at 8:58pm
The code is VBS script.

What kind of Basic script is supported?

Back to Top
Wagner R. Landgraf View Drop Down
TMS Support
TMS Support
Avatar

Joined: 18 May 2010
Posts: 2888
Post Options Post Options   Quote Wagner R. Landgraf Quote  Post ReplyReply Direct Link To This Post Posted: 02 Apr 2020 at 2:49pm
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 ...
Back to Top
Bleijerveld Michael View Drop Down
New Member
New Member
Avatar

Joined: 30 Mar 2020
Posts: 6
Post Options Post Options   Quote Bleijerveld Michael Quote  Post ReplyReply Direct Link To This Post Posted: 02 Apr 2020 at 4:14pm
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?

                         
Back to Top
Wagner R. Landgraf View Drop Down
TMS Support
TMS Support
Avatar

Joined: 18 May 2010
Posts: 2888
Post Options Post Options   Quote Wagner R. Landgraf Quote  Post ReplyReply Direct Link To This Post Posted: 02 Apr 2020 at 11:21pm
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
Back to Top
Bleijerveld Michael View Drop Down
New Member
New Member
Avatar

Joined: 30 Mar 2020
Posts: 6
Post Options Post Options   Quote Bleijerveld Michael Quote  Post ReplyReply Direct Link To This Post Posted: 03 Apr 2020 at 1:34pm
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?

Back to Top
Wagner R. Landgraf View Drop Down
TMS Support
TMS Support
Avatar

Joined: 18 May 2010
Posts: 2888
Post Options Post Options   Quote Wagner R. Landgraf Quote  Post ReplyReply Direct Link To This Post Posted: 03 Apr 2020 at 3:30pm
Unfortunately not. You will have to find the documentation of SWbemObjectSet:
Maybe Length is available
Back to Top
Bleijerveld Michael View Drop Down
New Member
New Member
Avatar

Joined: 30 Mar 2020
Posts: 6
Post Options Post Options   Quote Bleijerveld Michael Quote  Post ReplyReply Direct Link To This Post Posted: 03 Apr 2020 at 5:13pm
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.



Back to Top
Wagner R. Landgraf View Drop Down
TMS Support
TMS Support
Avatar

Joined: 18 May 2010
Posts: 2888
Post Options Post Options   Quote Wagner R. Landgraf Quote  Post ReplyReply Direct Link To This Post Posted: 05 Apr 2020 at 1:57am
Hello Michael,
I don't know what is DefragAnalysis.
Back to Top
Bleijerveld Michael View Drop Down
New Member
New Member
Avatar

Joined: 30 Mar 2020
Posts: 6
Post Options Post Options   Quote Bleijerveld Michael Quote  Post ReplyReply Direct Link To This Post Posted: 05 Apr 2020 at 12:16pm
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

Back to Top
Wagner R. Landgraf View Drop Down
TMS Support
TMS Support
Avatar

Joined: 18 May 2010
Posts: 2888
Post Options Post Options   Quote Wagner R. Landgraf Quote  Post ReplyReply Direct Link To This Post Posted: 06 Apr 2020 at 3:10am
Wrap it in a Delphi function and register the Delphi function in the scripter.
Back to Top
Bleijerveld Michael View Drop Down
New Member
New Member
Avatar

Joined: 30 Mar 2020
Posts: 6
Post Options Post Options   Quote Bleijerveld Michael Quote  Post ReplyReply Direct Link To This Post Posted: 07 Apr 2020 at 4:36pm
Thank you. Will try that.
Back to Top
 Post Reply Post Reply

Forum Jump Forum Permissions View Drop Down