Skip to content

Commit 8dc2d44

Browse files
committed
Update WMI docs / samples
1 parent e5f87e1 commit 8dc2d44

7 files changed

Lines changed: 244 additions & 76 deletions

File tree

docs/source/sample.rst

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -320,10 +320,13 @@ Ouput::
320320
I AM LOADING <C:\Windows\system32\ole32.dll>
321321

322322

323-
.. _wmi_request:
323+
.. _wmi_samples:
324+
325+
WMI
326+
"""
324327

325328
WMI requests
326-
""""""""""""
329+
''''''''''''
327330

328331

329332
.. literalinclude:: ..\..\samples\wmi\wmi_request.py
@@ -332,6 +335,15 @@ Output
332335

333336
.. literalinclude:: samples_output\wmi_wmi_request.txt
334337

338+
WMI Create Process
339+
''''''''''''''''''
340+
341+
.. literalinclude:: ..\..\samples\wmi\create_process.py
342+
343+
Output
344+
345+
.. literalinclude:: samples_output\wmi_create_process.txt
346+
335347

336348
.. _sample_com:
337349

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
(cmd) python wmi\create_process.py
2+
WMI namespace is <<WmiNamespace "root\cimv2">>
3+
Process class is <WmiObject class "Win32_Process">
4+
Method Create InParams is <<WmiObject class "__PARAMETERS">>
5+
Method Create InParams properties are <[u'CommandLine', u'CurrentDirectory', u'ProcessStartupInformation']>
6+
Creating instance of inparam
7+
InParam instance is <<WmiObject instance of "__PARAMETERS">>
8+
Setting <CommandLine>
9+
Executing method
10+
OutParams is <WmiObject instance of "__PARAMETERS">
11+
Out params values are: [u'ProcessId', u'ReturnValue']
12+
Created process is <WinProcess "notepad.exe" pid 24036 at 0x3d04390>
13+
Waiting 1s
14+
Killing the process
Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,30 @@
11
(cmd) python wmi\wmi_request.py
2-
WMI requester is <windows.winobject.wmi.WmiManager object at 0x05C70CA8>
2+
WMI requester is <windows.winobject.wmi.WmiManager object at 0x04258918>
33
Selecting * from 'Win32_Process'
4-
They are <188> processes
4+
They are <308> processes
55
Looking for ourself via pid
66
Some info about our process:
77
* Name -> python.exe
8-
* ProcessId -> 8144
9-
* OSName -> Microsoft Windows 10 Home|C:\WINDOWS|\Device\Harddisk0\Partition2
10-
* UserModeTime -> 1875000
11-
* WindowsVersion -> 10.0.16299
8+
* ProcessId -> 27144
9+
* OSName -> Microsoft Windows 10 Pro|C:\WINDOWS|\Device\Harddisk0\Partition2
10+
* UserModeTime -> 2812500
11+
* WindowsVersion -> 10.0.17134
1212
* CommandLine -> C:\Python27\python.exe wmi\wmi_request.py
1313
<Select Caption,FileSystem,FreeSpace from Win32_LogicalDisk>:
14-
* {'Caption': u'C:', 'FreeSpace': u'4704194560', 'FileSystem': u'NTFS'}
15-
* {'Caption': u'D:', 'FreeSpace': u'269887504384', 'FileSystem': u'NTFS'}
14+
* {u'Caption': u'B:', u'FreeSpace': None, u'FileSystem': None}
15+
* {u'Caption': u'C:', u'FreeSpace': u'5701517312', u'FileSystem': u'NTFS'}
16+
* {u'Caption': u'D:', u'FreeSpace': u'47657324544', u'FileSystem': u'NTFS'}
17+
* {u'Caption': u'E:', u'FreeSpace': u'89507512320', u'FileSystem': u'NTFS'}
1618

1719
==== Advanced use ====
1820
Listing some namespaces:
1921
* CIMV2
2022
* SecurityCenter2
2123
* StandardCimv2
22-
Querying non-default namespace: <WmiRequester namespace="root\SecurityCenter2">
24+
Querying non-default namespace: <WmiNamespace "root\SecurityCenter2">
2325
Listing some available classes:
24-
* FirewallProduct
25-
* AntiVirusProduct
26-
* AntiSpywareProduct
26+
* <WmiObject class "FirewallProduct">
27+
* <WmiObject class "AntiVirusProduct">
28+
* <WmiObject class "AntiSpywareProduct">
2729
Listing <AntiVirusProduct>:
2830
* Windows Defender

docs/source/wmi.rst

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ The :class:`WmiManager` is accessible via :py:attr:`windows.system.wmi
99

1010
.. note::
1111

12-
See sample :ref:`wmi_request`
12+
See sample :ref:`wmi_samples`
1313

1414

1515
WmiManager
@@ -19,7 +19,34 @@ WmiManager
1919
:no-inherited-members:
2020
:members: DEFAULT_NAMESPACE, select, query, namespaces
2121

22-
WmiRequester
22+
23+
WmiNamespace
2324
""""""""""""
2425

25-
.. autoclass:: WmiRequester
26+
.. autoclass:: WmiNamespace
27+
:members:
28+
:show-inheritance:
29+
30+
WmiObject
31+
"""""""""
32+
33+
.. autoclass:: WmiObject
34+
:members:
35+
:special-members: __call__, __getitem__, __setitem__
36+
:show-inheritance:
37+
38+
39+
WmiCallResult
40+
"""""""""""""
41+
42+
.. autoclass:: WmiCallResult
43+
:members:
44+
:show-inheritance:
45+
46+
WmiEnumeration
47+
""""""""""""""
48+
49+
.. autoclass:: WmiEnumeration
50+
:members:
51+
:special-members: __call__, __iter__
52+
:show-inheritance:

samples/wmi/create_process.py

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,34 @@
1+
import time
12
import windows
2-
import windows.com
3-
import windows.generated_def as gdef
43

5-
def bstr_variant(s):
6-
v = windows.com.Variant()
7-
v.vt = gdef.VT_BSTR
8-
v._VARIANT_NAME_3.bstrVal = s
9-
return v
104

5+
wmispace = windows.system.wmi["root\\cimv2"]
6+
print("WMI namespace is <{0}>".format(wmispace))
7+
proc_class = wmispace.get_object("Win32_process")
8+
print("Process class is {0}".format(proc_class))
119

12-
wmireq = windows.system.wmi["root\\cimv2"]
13-
proc_class = wmireq.get_object("Win32_process")
10+
inparam_cls = proc_class.get_method("Create").inparam
11+
print("Method Create InParams is <{0}>".format(inparam_cls))
12+
print("Method Create InParams properties are <{0}>".format(inparam_cls.properties))
13+
print("Creating instance of inparam")
1414

15-
# # Method 1
16-
inparam = proc_class.get_method("Create").inparam.spawn_instance()
17-
inparam["CommandLine"] = r"c:\windows\system32\notepad.exe trolol.exe"
18-
# Create a test checking return value
19-
xx = wmireq.exec_method(proc_class, "Create", inparam)
20-
print(xx)
21-
print(xx.as_dict())
15+
inparam = inparam_cls()
16+
print("InParam instance is <{0}>".format(inparam))
17+
print("Setting <CommandLine>")
18+
inparam["CommandLine"] = r"c:\windows\system32\notepad.exe"
2219

23-
## Method2
20+
print("Executing method")
21+
# This API may change for something that better wraps cls/object/Parameters handling
22+
outparam = wmispace.exec_method(proc_class, "Create", inparam)
2423

25-
# class MyResult(gdef.IWbemCallResult):
26-
# def result(self):
27-
# res = type(proc_class)()
28-
# self.GetResultObject(gdef.WBEM_INFINITE, res)
29-
# return res
24+
print("OutParams is {0}".format(outparam))
25+
print("Out params values are: {0}".format(outparam.properties))
26+
target = windows.WinProcess(pid=int(outparam["ProcessId"]))
27+
print("Created process is {0}".format(target))
28+
print("Waiting 1s")
29+
time.sleep(1)
30+
print("Killing the process")
31+
target.exit(0)
3032

3133

32-
# proc = proc_class.spawn()
33-
# cmdline = bstr_variant(r"c:\windows\system32\notepad.exe")
34-
# proc.put_variant("CommandLine", cmdline)
35-
# res = wmireq.put_instance(proc)
3634

samples/wmi/wmi_request.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,9 @@
2222
print(" * {0} -> {1}".format("CommandLine", us["CommandLine"]))
2323

2424
print("<Select Caption,FileSystem,FreeSpace from Win32_LogicalDisk>:")
25-
for vol in windows.system.wmi.select("Win32_LogicalDisk", ["Caption", "FileSystem", "FreeSpace"]):
26-
print(" * " + str(vol))
25+
for vol in windows.system.wmi.query("select Caption,FileSystem,FreeSpace from Win32_LogicalDisk"):
26+
# Filter out system-properties for the sample
27+
print(" * " + str({k:v for k,v in vol.items() if not k.startswith("_")}))
2728

2829
print("\n ==== Advanced use ====")
2930
print("Listing some namespaces:")
@@ -33,7 +34,7 @@
3334
security2 = windows.system.wmi["root\\SecurityCenter2"]
3435
print("Querying non-default namespace: {0}".format(security2))
3536
print("Listing some available classes:")
36-
for clsname in [x for x in security2.classes if x.endswith("Product")]:
37+
for clsname in [x for x in security2.classes if x["__CLASS"].endswith("Product")]:
3738
print(" * {0}".format(clsname))
3839

3940
print("Listing <AntiVirusProduct>:")

0 commit comments

Comments
 (0)