wmi-lite 1.0.0.rc.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +15 -0
- data/.gitignore +27 -0
- data/.rspec +2 -0
- data/.travis.yml +8 -0
- data/CHANGELOG.md +11 -0
- data/CONTRIBUTING.md +5 -0
- data/Gemfile +2 -0
- data/LICENSE +201 -0
- data/README.md +111 -0
- data/Rakefile +1 -0
- data/lib/wmi-lite.rb +20 -0
- data/lib/wmi-lite/version.rb +3 -0
- data/lib/wmi-lite/wmi.rb +97 -0
- data/lib/wmi-lite/wmi_exception.rb +77 -0
- data/lib/wmi-lite/wmi_instance.rb +48 -0
- data/spec/functional/wmi_spec.rb +161 -0
- data/spec/spec_helper.rb +12 -0
- data/spec/unit/wmi_spec.rb +295 -0
- data/wmi-lite.gemspec +33 -0
- metadata +140 -0
checksums.yaml
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
---
|
2
|
+
!binary "U0hBMQ==":
|
3
|
+
metadata.gz: !binary |-
|
4
|
+
ZTNmZWM2YTNkNDc3MDljMDM0YTc1ZGY1YzU2MTUwOTE4ZWIwYTA2Yw==
|
5
|
+
data.tar.gz: !binary |-
|
6
|
+
YzZiOTUyMzJlMDAwOWExZDRkZGMzOGRkOGU4YTFiNWUwYWQ0OTc0YQ==
|
7
|
+
SHA512:
|
8
|
+
metadata.gz: !binary |-
|
9
|
+
N2NiNzRkMzAyYzBiOWEwMzU5OTAyNzU1ODZjYWQxZDExMWVkYzczYjY3NjI5
|
10
|
+
ZWQ3YjFhNjUwNmVkODVmZDUwYmE5ODNjN2I5YTc1MjNmOGJhYTg0ZTZiOGNh
|
11
|
+
MjY0NjJkZjkwOGM1YTEzYmZiOWU0NWVjYjJjZWYzNjQ5ZmM5ZmI=
|
12
|
+
data.tar.gz: !binary |-
|
13
|
+
ZWQ1YTVhMzQ3OGI2Y2Q5YmQ5NThiMGY2N2M2ZTgzMDBkMzA5ZDIyZmZkYzg0
|
14
|
+
YjVmOGFlYjg4YjI4ZWQxOWRkZDY2NTY1MWY4NTVhZjQ1ZDgxOWViNjUwZWVi
|
15
|
+
ZmNmNDY3MDBlNzE0YTMwNDg3NDBiYzJlNjlhNjhlMmYxNDc2Zjg=
|
data/.gitignore
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
.autotest
|
2
|
+
coverage
|
3
|
+
.DS_Store
|
4
|
+
pkg
|
5
|
+
*/tags
|
6
|
+
*~
|
7
|
+
|
8
|
+
# you should check in your Gemfile.lock in applications, and not in gems
|
9
|
+
Gemfile.lock
|
10
|
+
Gemfile.local
|
11
|
+
|
12
|
+
# Do not check in the .bundle directory, or any of the files inside it. Those files are specific to each particular machine, and are used to persist installation options between runs of the bundle install command.
|
13
|
+
.bundle
|
14
|
+
|
15
|
+
# ignore some common Bundler 'binstubs' directory names
|
16
|
+
# http://gembundler.com/man/bundle-exec.1.html
|
17
|
+
b/
|
18
|
+
binstubs/
|
19
|
+
|
20
|
+
# RVM and RBENV ruby version files
|
21
|
+
.rbenv-version
|
22
|
+
.rvmrc
|
23
|
+
|
24
|
+
# Documentation
|
25
|
+
_site/*
|
26
|
+
.yardoc/
|
27
|
+
doc/
|
data/.rspec
ADDED
data/.travis.yml
ADDED
data/CHANGELOG.md
ADDED
data/CONTRIBUTING.md
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,201 @@
|
|
1
|
+
Apache License
|
2
|
+
Version 2.0, January 2004
|
3
|
+
http://www.apache.org/licenses/
|
4
|
+
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
6
|
+
|
7
|
+
1. Definitions.
|
8
|
+
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
11
|
+
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
13
|
+
the copyright owner that is granting the License.
|
14
|
+
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
16
|
+
other entities that control, are controlled by, or are under common
|
17
|
+
control with that entity. For the purposes of this definition,
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
19
|
+
direction or management of such entity, whether by contract or
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
22
|
+
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
24
|
+
exercising permissions granted by this License.
|
25
|
+
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
27
|
+
including but not limited to software source code, documentation
|
28
|
+
source, and configuration files.
|
29
|
+
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
31
|
+
transformation or translation of a Source form, including but
|
32
|
+
not limited to compiled object code, generated documentation,
|
33
|
+
and conversions to other media types.
|
34
|
+
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
36
|
+
Object form, made available under the License, as indicated by a
|
37
|
+
copyright notice that is included in or attached to the work
|
38
|
+
(an example is provided in the Appendix below).
|
39
|
+
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
46
|
+
the Work and Derivative Works thereof.
|
47
|
+
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
49
|
+
the original version of the Work and any modifications or additions
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
61
|
+
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
64
|
+
subsequently incorporated within the Work.
|
65
|
+
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
72
|
+
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
78
|
+
where such license applies only to those patent claims licensable
|
79
|
+
by such Contributor that are necessarily infringed by their
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
82
|
+
institute patent litigation against any entity (including a
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
85
|
+
or contributory patent infringement, then any patent licenses
|
86
|
+
granted to You under this License for that Work shall terminate
|
87
|
+
as of the date such litigation is filed.
|
88
|
+
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
91
|
+
modifications, and in Source or Object form, provided that You
|
92
|
+
meet the following conditions:
|
93
|
+
|
94
|
+
(a) You must give any other recipients of the Work or
|
95
|
+
Derivative Works a copy of this License; and
|
96
|
+
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
98
|
+
stating that You changed the files; and
|
99
|
+
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
102
|
+
attribution notices from the Source form of the Work,
|
103
|
+
excluding those notices that do not pertain to any part of
|
104
|
+
the Derivative Works; and
|
105
|
+
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
108
|
+
include a readable copy of the attribution notices contained
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
111
|
+
of the following places: within a NOTICE text file distributed
|
112
|
+
as part of the Derivative Works; within the Source form or
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
114
|
+
within a display generated by the Derivative Works, if and
|
115
|
+
wherever such third-party notices normally appear. The contents
|
116
|
+
of the NOTICE file are for informational purposes only and
|
117
|
+
do not modify the License. You may add Your own attribution
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
120
|
+
that such additional attribution notices cannot be construed
|
121
|
+
as modifying the License.
|
122
|
+
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
124
|
+
may provide additional or different license terms and conditions
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
128
|
+
the conditions stated in this License.
|
129
|
+
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
133
|
+
this License, without any additional terms or conditions.
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
135
|
+
the terms of any separate license agreement you may have executed
|
136
|
+
with Licensor regarding such Contributions.
|
137
|
+
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
140
|
+
except as required for reasonable and customary use in describing the
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
142
|
+
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
152
|
+
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
158
|
+
incidental, or consequential damages of any character arising as a
|
159
|
+
result of this License or out of the use or inability to use the
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
162
|
+
other commercial damages or losses), even if such Contributor
|
163
|
+
has been advised of the possibility of such damages.
|
164
|
+
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
168
|
+
or other liability obligations and/or rights consistent with this
|
169
|
+
License. However, in accepting such obligations, You may act only
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
174
|
+
of your accepting any such warranty or additional liability.
|
175
|
+
|
176
|
+
END OF TERMS AND CONDITIONS
|
177
|
+
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
179
|
+
|
180
|
+
To apply the Apache License to your work, attach the following
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
182
|
+
replaced with your own identifying information. (Don't include
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
184
|
+
comment syntax for the file format. We also recommend that a
|
185
|
+
file or class name and description of purpose be included on the
|
186
|
+
same "printed page" as the copyright notice for easier
|
187
|
+
identification within third-party archives.
|
188
|
+
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
190
|
+
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
192
|
+
you may not use this file except in compliance with the License.
|
193
|
+
You may obtain a copy of the License at
|
194
|
+
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
196
|
+
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
200
|
+
See the License for the specific language governing permissions and
|
201
|
+
limitations under the License.
|
data/README.md
ADDED
@@ -0,0 +1,111 @@
|
|
1
|
+
Wmi-Lite
|
2
|
+
========
|
3
|
+
|
4
|
+
`wmi-lite` is a lightweight Ruby gem utility library for accessing basic
|
5
|
+
[Windows Management Instrumentation (WMI)](http://msdn.microsoft.com/en-us/library/aa394582(v=vs.85).aspx)
|
6
|
+
functionality on Windows. It has no dependencies other than version 1.9 or greater of the Ruby interpreter
|
7
|
+
and libraries and of course the Windows operating system.
|
8
|
+
|
9
|
+
Installation
|
10
|
+
------------
|
11
|
+
|
12
|
+
To install it, run:
|
13
|
+
|
14
|
+
gem install wmi-lite
|
15
|
+
|
16
|
+
Usage
|
17
|
+
-----
|
18
|
+
To use `wmi-lite` in your Ruby source code, just `require` it:
|
19
|
+
|
20
|
+
```ruby
|
21
|
+
require 'wmi-lite'
|
22
|
+
```
|
23
|
+
|
24
|
+
You can then instantiate an object through `WmiLite::Wmi.new` that will allow you to query a WMI namespace by calling methods on
|
25
|
+
that object.
|
26
|
+
|
27
|
+
* The default namespace if no argument is specified to `new` is `root\cimv2`. To override, pass the desired WMI
|
28
|
+
namespace string as an argument to the constructor.
|
29
|
+
* To execute queries against the object's namespace, use the `instances_of`, `first_of`, and `query` methods.
|
30
|
+
* The `instances_of` method will return all instances of a given class in the namespace as an array of instances.
|
31
|
+
* The `query` method returns the results of an arbitrary WMI Query Language (WQL) query as an array of instances.
|
32
|
+
* The `first_of` method will return the first of all instances of a given class in the namespace.
|
33
|
+
* Each instance is represented by a Ruby `Hash` for which each property value of the instance is indexed by
|
34
|
+
the string name of the property as documented in the [WMI Schema](http://technet.microsoft.com/en-us/library/cc180287.aspx) or
|
35
|
+
as registered in the local system's WMI repository.
|
36
|
+
* The string name specified to the aformentioned `Hash` is case insensitive.
|
37
|
+
|
38
|
+
#### Examples
|
39
|
+
Use of the `instances_of`, `query`, and `first_of` methods of the `WmiLite::Wmi` object is demonstrated below.
|
40
|
+
|
41
|
+
##### Count cores in the system
|
42
|
+
|
43
|
+
```ruby
|
44
|
+
cores = 0
|
45
|
+
wmi = WmiLite::Wmi.new
|
46
|
+
processors = wmi.instances_of('Win32_Processor')
|
47
|
+
processors.each do | processor |
|
48
|
+
cores += processor['numberofcores']
|
49
|
+
end
|
50
|
+
puts "\nThis system has #{cores} core(s).\n"
|
51
|
+
```
|
52
|
+
|
53
|
+
##### Determine if the system is domain-joined
|
54
|
+
|
55
|
+
```ruby
|
56
|
+
wmi = WmiLite::Wmi.new
|
57
|
+
computer_system = wmi.first_of('Win32_ComputerSystem')
|
58
|
+
is_in_domain = computer_system['partofdomain']
|
59
|
+
puts "\nThis system is #{is_in_domain ? '' : 'not '}domain joined.\n"
|
60
|
+
```
|
61
|
+
|
62
|
+
##### List Group Policy Objects (GPOs) applied to the system
|
63
|
+
|
64
|
+
```ruby
|
65
|
+
wmi = WmiLite::Wmi.new('root\rsop\computer')
|
66
|
+
gpos = wmi.instances_of('RSOP_GPO')
|
67
|
+
puts "\n#{'GPO Id'.ljust(40)}\tName"
|
68
|
+
puts "#{'------'.ljust(40)}\t----\n"
|
69
|
+
gpos.each do | gpo |
|
70
|
+
gpo_id = gpo['guidname']
|
71
|
+
gpo_display_name = gpo['name']
|
72
|
+
puts "#{gpo_id.ljust(40)}\t#{gpo_display_name}"
|
73
|
+
end
|
74
|
+
puts 'No GPOs' if gpos.count == 0
|
75
|
+
puts
|
76
|
+
```
|
77
|
+
|
78
|
+
##### List ruby-related processes
|
79
|
+
```ruby
|
80
|
+
puts "Ruby processes:\n"
|
81
|
+
wmi = WmiLite::Wmi.new
|
82
|
+
processes = wmi.query('select * from Win32_Process where Name LIKE \'%ruby%\'')
|
83
|
+
puts "\n#{'Process Id'.ljust(10)} Name"
|
84
|
+
puts "#{'----------'.ljust(10)} ----\n"
|
85
|
+
processes.each do | process |
|
86
|
+
pid = process['processid']
|
87
|
+
name = process['name']
|
88
|
+
puts "#{pid.to_s.ljust(10)} #{name}"
|
89
|
+
end
|
90
|
+
puts
|
91
|
+
```
|
92
|
+
|
93
|
+
License & Authors
|
94
|
+
-----------------
|
95
|
+
|
96
|
+
Author:: Adam Edwards (<adamed@getchef.com>)
|
97
|
+
Copyright:: Copyright (c) 2014 Chef Software, Inc.
|
98
|
+
License:: Apache License, Version 2.0
|
99
|
+
|
100
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
101
|
+
you may not use this file except in compliance with the License.
|
102
|
+
You may obtain a copy of the License at
|
103
|
+
|
104
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
105
|
+
|
106
|
+
Unless required by applicable law or agreed to in writing, software
|
107
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
108
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
109
|
+
See the License for the specific language governing permissions and
|
110
|
+
limitations under the License.
|
111
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'bundler/gem_tasks'
|
data/lib/wmi-lite.rb
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
#
|
2
|
+
# Author:: Adam Edwards (<adamed@getchef.com>)
|
3
|
+
# Copyright:: Copyright 2014 Chef Software, Inc.
|
4
|
+
# License:: Apache License, Version 2.0
|
5
|
+
#
|
6
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
7
|
+
# you may not use this file except in compliance with the License.
|
8
|
+
# You may obtain a copy of the License at
|
9
|
+
#
|
10
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
11
|
+
#
|
12
|
+
# Unless required by applicable law or agreed to in writing, software
|
13
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
14
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
15
|
+
# See the License for the specific language governing permissions and
|
16
|
+
# limitations under the License.
|
17
|
+
#
|
18
|
+
|
19
|
+
require 'wmi-lite/wmi'
|
20
|
+
|
data/lib/wmi-lite/wmi.rb
ADDED
@@ -0,0 +1,97 @@
|
|
1
|
+
#
|
2
|
+
# Author:: Adam Edwards (<adamed@getchef.com>)
|
3
|
+
# Copyright:: Copyright 2014 Chef Software, Inc.
|
4
|
+
# License:: Apache License, Version 2.0
|
5
|
+
#
|
6
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
7
|
+
# you may not use this file except in compliance with the License.
|
8
|
+
# You may obtain a copy of the License at
|
9
|
+
#
|
10
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
11
|
+
#
|
12
|
+
# Unless required by applicable law or agreed to in writing, software
|
13
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
14
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
15
|
+
# See the License for the specific language governing permissions and
|
16
|
+
# limitations under the License.
|
17
|
+
#
|
18
|
+
|
19
|
+
require 'win32ole' if RUBY_PLATFORM =~ /mswin|mingw32|windows/
|
20
|
+
require 'wmi-lite/wmi_instance'
|
21
|
+
require 'wmi-lite/wmi_exception'
|
22
|
+
|
23
|
+
module WmiLite
|
24
|
+
class Wmi
|
25
|
+
def initialize(namespace = nil)
|
26
|
+
@namespace = namespace.nil? ? 'root/cimv2' : namespace
|
27
|
+
@connection = nil
|
28
|
+
end
|
29
|
+
|
30
|
+
def query(wql_query)
|
31
|
+
query_with_context(wql_query)
|
32
|
+
end
|
33
|
+
|
34
|
+
def instances_of(wmi_class)
|
35
|
+
query_with_context("select * from #{wmi_class}", wmi_class)
|
36
|
+
end
|
37
|
+
|
38
|
+
def first_of(wmi_class)
|
39
|
+
query_result = start_query("select * from #{wmi_class}", wmi_class)
|
40
|
+
first_result = nil
|
41
|
+
query_result.each do | record |
|
42
|
+
first_result = record
|
43
|
+
break
|
44
|
+
end
|
45
|
+
first_result.nil? ? nil : wmi_result_to_snapshot(first_result)
|
46
|
+
end
|
47
|
+
|
48
|
+
private
|
49
|
+
|
50
|
+
def query_with_context(wql_query, diagnostic_class_name = nil)
|
51
|
+
results = start_query(wql_query, diagnostic_class_name)
|
52
|
+
|
53
|
+
result_set = []
|
54
|
+
|
55
|
+
results.each do | result |
|
56
|
+
result_set.push(wmi_result_to_snapshot(result))
|
57
|
+
end
|
58
|
+
|
59
|
+
result_set
|
60
|
+
end
|
61
|
+
|
62
|
+
def start_query(wql_query, diagnostic_class_name = nil)
|
63
|
+
result = nil
|
64
|
+
connect_to_namespace
|
65
|
+
begin
|
66
|
+
result = @connection.ExecQuery(wql_query)
|
67
|
+
raise_if_failed(result)
|
68
|
+
rescue WIN32OLERuntimeError => native_exception
|
69
|
+
raise WmiException.new(native_exception, :ExecQuery, @namespace, wql_query, diagnostic_class_name)
|
70
|
+
end
|
71
|
+
result
|
72
|
+
end
|
73
|
+
|
74
|
+
def raise_if_failed(result)
|
75
|
+
# Attempting to access the count property of the underlying
|
76
|
+
# COM (OLE) object will trigger an exception if the query
|
77
|
+
# was unsuccessful.
|
78
|
+
result.count
|
79
|
+
end
|
80
|
+
|
81
|
+
def connect_to_namespace
|
82
|
+
if @connection.nil?
|
83
|
+
namespace = @namespace.nil? ? 'root/cimv2' : @namespace
|
84
|
+
locator = WIN32OLE.new("WbemScripting.SWbemLocator")
|
85
|
+
begin
|
86
|
+
@connection = locator.ConnectServer('.', namespace)
|
87
|
+
rescue WIN32OLERuntimeError => native_exception
|
88
|
+
raise WmiException.new(native_exception, :ConnectServer, @namespace)
|
89
|
+
end
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
def wmi_result_to_snapshot(wmi_object)
|
94
|
+
snapshot = Instance.new(wmi_object)
|
95
|
+
end
|
96
|
+
end
|
97
|
+
end
|
@@ -0,0 +1,77 @@
|
|
1
|
+
#
|
2
|
+
# Author:: Adam Edwards (<adamed@getchef.com>)
|
3
|
+
# Copyright:: Copyright 2014 Chef Software, Inc.
|
4
|
+
# License:: Apache License, Version 2.0
|
5
|
+
#
|
6
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
7
|
+
# you may not use this file except in compliance with the License.
|
8
|
+
# You may obtain a copy of the License at
|
9
|
+
#
|
10
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
11
|
+
#
|
12
|
+
# Unless required by applicable law or agreed to in writing, software
|
13
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
14
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
15
|
+
# See the License for the specific language governing permissions and
|
16
|
+
# limitations under the License.
|
17
|
+
#
|
18
|
+
|
19
|
+
module WmiLite
|
20
|
+
class WmiException < Exception
|
21
|
+
def initialize(exception, wmi_method_context, namespace, query = nil, class_name = nil)
|
22
|
+
error_message = exception.message
|
23
|
+
error_code = translate_error_code(error_message)
|
24
|
+
|
25
|
+
case wmi_method_context
|
26
|
+
when :ConnectServer
|
27
|
+
error_message = translate_wmi_connect_error_message(error_message, error_code, namespace)
|
28
|
+
when :ExecQuery
|
29
|
+
error_message = translate_query_error_message(error_message, error_code, namespace, query, class_name)
|
30
|
+
end
|
31
|
+
|
32
|
+
super(error_message)
|
33
|
+
end
|
34
|
+
|
35
|
+
private
|
36
|
+
|
37
|
+
def translate_error_code(error_message)
|
38
|
+
error_code = nil
|
39
|
+
|
40
|
+
# Parse the error to get the error status code
|
41
|
+
error_code_match = error_message.match(/[^\:]+\:\s*([0-9A-Fa-f]{1,8}).*/)
|
42
|
+
error_code = error_code_match.captures.first if error_code_match
|
43
|
+
error_code ? error_code : ''
|
44
|
+
end
|
45
|
+
|
46
|
+
def translate_wmi_connect_error_message(native_message, error_code, namespace)
|
47
|
+
error_message = "An error occurred connecting to the WMI service for namespace \'#{namespace}\'. The namespace may not be valid, access may not be allowed to the WMI service, or the WMI service may not be available.\n#{native_message}"
|
48
|
+
|
49
|
+
if error_code =~ /8004100E/i
|
50
|
+
error_message = "The specified namespace name \'#{namespace}\' is not a valid namespace name or does not exist.\n#{native_message}"
|
51
|
+
end
|
52
|
+
|
53
|
+
error_message
|
54
|
+
end
|
55
|
+
|
56
|
+
def translate_query_error_message(native_message, error_code, namespace, query, class_name)
|
57
|
+
error_message = "An error occurred when querying namespace \'#{namespace}\' with query \'#{query}\'.\n#{native_message}"
|
58
|
+
|
59
|
+
error_code = translate_error_code(error_message)
|
60
|
+
|
61
|
+
# Use the status code to generate a more friendly message
|
62
|
+
case error_code
|
63
|
+
when /80041010/i
|
64
|
+
if class_name
|
65
|
+
error_message = "The specified class \'#{class_name}\' is not valid in the namespace \'#{namespace}\'.\n#{native_message}."
|
66
|
+
else
|
67
|
+
error_message = "The specified query \'#{query}\' referenced a class that is not valid in the namespace \'#{namespace}\'\n#{native_message}."
|
68
|
+
end
|
69
|
+
when /80041017/i
|
70
|
+
error_message = "The specified query \'#{query}\' in namespace \'#{namespace}\' is not a syntactically valid query.\n#{native_message}"
|
71
|
+
end
|
72
|
+
|
73
|
+
error_message
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
@@ -0,0 +1,48 @@
|
|
1
|
+
#
|
2
|
+
# Author:: Adam Edwards (<adamed@getchef.com>)
|
3
|
+
# Copyright:: Copyright 2014 Chef Software, Inc.
|
4
|
+
# License:: Apache License, Version 2.0
|
5
|
+
#
|
6
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
7
|
+
# you may not use this file except in compliance with the License.
|
8
|
+
# You may obtain a copy of the License at
|
9
|
+
#
|
10
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
11
|
+
#
|
12
|
+
# Unless required by applicable law or agreed to in writing, software
|
13
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
14
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
15
|
+
# See the License for the specific language governing permissions and
|
16
|
+
# limitations under the License.
|
17
|
+
#
|
18
|
+
module WmiLite
|
19
|
+
class Wmi
|
20
|
+
class Instance
|
21
|
+
|
22
|
+
attr_reader :wmi_ole_object
|
23
|
+
|
24
|
+
def initialize(wmi_ole_object)
|
25
|
+
@wmi_ole_object = wmi_ole_object
|
26
|
+
@property_map = wmi_ole_object_to_hash(wmi_ole_object)
|
27
|
+
end
|
28
|
+
|
29
|
+
def [](key)
|
30
|
+
@property_map[key.downcase]
|
31
|
+
end
|
32
|
+
|
33
|
+
private
|
34
|
+
|
35
|
+
def wmi_ole_object_to_hash(wmi_object)
|
36
|
+
property_map = {}
|
37
|
+
wmi_object.properties_.each do |property|
|
38
|
+
property_map[property.name.downcase] = wmi_object.invoke(property.name)
|
39
|
+
end
|
40
|
+
|
41
|
+
@wmi_ole_object = wmi_object
|
42
|
+
|
43
|
+
property_map.freeze
|
44
|
+
end
|
45
|
+
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
@@ -0,0 +1,161 @@
|
|
1
|
+
#
|
2
|
+
# Author:: Adam Edwards (<adamed@getchef.com>)
|
3
|
+
#
|
4
|
+
# Copyright:: 2014, Chef Software, Inc.
|
5
|
+
#
|
6
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
7
|
+
# you may not use this file except in compliance with the License.
|
8
|
+
# You may obtain a copy of the License at
|
9
|
+
#
|
10
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
11
|
+
#
|
12
|
+
# Unless required by applicable law or agreed to in writing, software
|
13
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
14
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
15
|
+
# See the License for the specific language governing permissions and
|
16
|
+
# limitations under the License.
|
17
|
+
#
|
18
|
+
|
19
|
+
require 'spec_helper'
|
20
|
+
|
21
|
+
describe WmiLite::Wmi, :windows_only do
|
22
|
+
let(:wmi) { WmiLite::Wmi.new(namespace) }
|
23
|
+
|
24
|
+
def validate_wmi_results(results, class_name)
|
25
|
+
result_collection = cardinality_transform.call(results)
|
26
|
+
result_collection.each do | result |
|
27
|
+
# make sure the class name of the instance is what we asked for
|
28
|
+
expect(result['creationclassname'].downcase).to eql(class_name.downcase)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
shared_examples_for 'a valid WMI query result' do
|
33
|
+
it 'should successfully return multiple results' do
|
34
|
+
query_parameter = wmi_query.nil? ? wmi_class : wmi_query
|
35
|
+
results = wmi.send(query_method, query_parameter)
|
36
|
+
validate_wmi_results(results, wmi_class)
|
37
|
+
end
|
38
|
+
|
39
|
+
describe 'when the namespace is invalid' do
|
40
|
+
it_behaves_like 'an invalid namespace'
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
shared_examples_for 'an invalid query' do
|
45
|
+
it 'should raise an exception' do
|
46
|
+
expect { wmi.send(query_method, wmi_query) }.to raise_error(WmiLite::WmiException)
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
shared_examples_for 'an invalid namespace' do
|
51
|
+
it 'should raise an exception if an invalid namespace is specified' do
|
52
|
+
invalid_wmi = WmiLite::Wmi.new('root/notvalid')
|
53
|
+
expect { invalid_wmi.send(query_method, wmi_query) }.to raise_error(WmiLite::WmiException)
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
shared_examples_for 'a valid WMI query' do
|
58
|
+
let(:wmi_class) { 'Win32_LogicalDisk' }
|
59
|
+
it_behaves_like 'a valid WMI query result'
|
60
|
+
|
61
|
+
let(:wmi_class) { 'Win32_ComputerSystem' }
|
62
|
+
it_behaves_like 'a valid WMI query result'
|
63
|
+
|
64
|
+
let(:wmi_class) { 'Win32_Process' }
|
65
|
+
it_behaves_like 'a valid WMI query result'
|
66
|
+
|
67
|
+
context 'that return 0 results' do
|
68
|
+
let(:wmi_class) { 'Win32_TapeDrive' }
|
69
|
+
it_behaves_like 'a valid WMI query result'
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
context 'when making valid queries' do
|
74
|
+
let(:namespace) { nil }
|
75
|
+
let(:wmi_query) { nil }
|
76
|
+
let(:cardinality_transform) { lambda{|x| x} }
|
77
|
+
context 'using first_of' do
|
78
|
+
let(:cardinality_transform) { lambda{|x| x.nil? ? [] : [x] } }
|
79
|
+
let(:query_method) { :first_of }
|
80
|
+
it_behaves_like 'a valid WMI query'
|
81
|
+
end
|
82
|
+
|
83
|
+
context 'using instances_of' do
|
84
|
+
let(:query_method) { :instances_of }
|
85
|
+
it_behaves_like 'a valid WMI query'
|
86
|
+
end
|
87
|
+
|
88
|
+
context 'using query' do
|
89
|
+
let(:wmi_query) { "select * from #{wmi_class}" }
|
90
|
+
let(:query_method) { :query }
|
91
|
+
it_behaves_like 'a valid WMI query'
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
context 'when making invalid queries' do
|
96
|
+
let(:namespace) { nil }
|
97
|
+
|
98
|
+
let(:wmi_query) { 'invalidclass' }
|
99
|
+
let(:query_method) { :first_of }
|
100
|
+
it_behaves_like 'an invalid query'
|
101
|
+
|
102
|
+
let(:query_method) { :instances_of }
|
103
|
+
it_behaves_like 'an invalid query'
|
104
|
+
|
105
|
+
let(:query_method) { :query }
|
106
|
+
let(:wmi_query) { 'nosql_4_life' }
|
107
|
+
it_behaves_like 'an invalid query'
|
108
|
+
end
|
109
|
+
|
110
|
+
let(:namespace) { nil }
|
111
|
+
describe 'when querying Win32_Environment' do
|
112
|
+
it 'should have the same environment variables as the Ruby ENV environment hash' do
|
113
|
+
results = wmi.instances_of('Win32_Environment')
|
114
|
+
|
115
|
+
variables = {}
|
116
|
+
|
117
|
+
# Skip some environment variables because we can't compare them against what's in ENV.
|
118
|
+
# Path, pathext, psmodulepath are special, they ares "merged" between the user and system value.
|
119
|
+
# PROCESSOR_ARCHITECTURE is actually the real processor arch of the system, so #{ENV['processor_architecture']} will
|
120
|
+
# report X86, while WMI will (correctly) report X64.
|
121
|
+
# And username is oddly the username of the WMI service, i.e. 'SYSTEM'.
|
122
|
+
ignore = {'path' => true, 'pathext' => true, 'processor_architecture' => true, 'psmodulepath' => true, 'username' => true}
|
123
|
+
results.each do | result |
|
124
|
+
if ! variables.has_key?(result['name']) || result['username'] != '<SYSTEM>'
|
125
|
+
variables[result['name']] = result['variablevalue']
|
126
|
+
end
|
127
|
+
end
|
128
|
+
|
129
|
+
verified_count = 0
|
130
|
+
variables.each_pair do | name, value |
|
131
|
+
if ignore[name.downcase] != true
|
132
|
+
|
133
|
+
# Turn %SYSTEMROOT% into c:\windows
|
134
|
+
# so we can compare with what's in ENV
|
135
|
+
evaluated_value = `echo #{value}`.strip
|
136
|
+
|
137
|
+
expect(evaluated_value).to eql(`echo #{ENV[name]}`.strip)
|
138
|
+
verified_count += 1
|
139
|
+
end
|
140
|
+
end
|
141
|
+
|
142
|
+
# There are at least 3 variables we could verify in a default
|
143
|
+
# Windows configuration, make sure we saw some
|
144
|
+
expect(verified_count).to be >= 3
|
145
|
+
end
|
146
|
+
end
|
147
|
+
|
148
|
+
let(:namespace) { nil }
|
149
|
+
it 'should ignore case when retrieving WMI properties' do
|
150
|
+
result = wmi.first_of('Win32_ComputerSystem')
|
151
|
+
caption_mixed = result['Caption']
|
152
|
+
caption_lower = result['caption']
|
153
|
+
|
154
|
+
expect(caption_mixed.nil?).to eql(false)
|
155
|
+
expect(caption_lower.nil?).to eql(false)
|
156
|
+
|
157
|
+
expect(caption_mixed.length).to be > 0
|
158
|
+
expect(caption_lower.length).to be > 0
|
159
|
+
expect(caption_mixed).to eql(caption_lower)
|
160
|
+
end
|
161
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
# $:.unshift File.expand_path('../../lib', __FILE__)
|
2
|
+
require 'rspec'
|
3
|
+
require 'wmi-lite'
|
4
|
+
|
5
|
+
RSpec.configure do |config|
|
6
|
+
config.include(RSpec::Matchers)
|
7
|
+
config.treat_symbols_as_metadata_keys_with_true_values = true
|
8
|
+
config.filter_run :focus => true
|
9
|
+
config.filter_run_excluding :windows_only => true if ! (RUBY_PLATFORM =~ /mswin|mingw32|windows/)
|
10
|
+
config.run_all_when_everything_filtered = true
|
11
|
+
end
|
12
|
+
|
@@ -0,0 +1,295 @@
|
|
1
|
+
#
|
2
|
+
# Author:: Adam Edwards (<adamed@getchef.com>)
|
3
|
+
#
|
4
|
+
# Copyright:: 2014, Chef Software, Inc.
|
5
|
+
#
|
6
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
7
|
+
# you may not use this file except in compliance with the License.
|
8
|
+
# You may obtain a copy of the License at
|
9
|
+
#
|
10
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
11
|
+
#
|
12
|
+
# Unless required by applicable law or agreed to in writing, software
|
13
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
14
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
15
|
+
# See the License for the specific language governing permissions and
|
16
|
+
# limitations under the License.
|
17
|
+
#
|
18
|
+
|
19
|
+
require 'spec_helper'
|
20
|
+
|
21
|
+
describe WmiLite::Wmi do
|
22
|
+
|
23
|
+
let(:wbem_locator) { double 'WIN32OLE', :ConnectServer => wbem_connection }
|
24
|
+
let(:wmi_query_instance1) { double 'Wmi::Instance', :wmi_ole_object => native_query_instance1, :[] => native_properties1 }
|
25
|
+
let(:wmi_query_instance2) { double 'Wmi::Instance', :wmi_ole_object => native_query_instance2, :[] => native_properties2 }
|
26
|
+
let(:wmi_query_result1) { [ wmi_query_instance1 ].to_enum }
|
27
|
+
let(:wmi_query_result2) { [ wmi_query_instance1, wmi_query_instance2 ].to_enum }
|
28
|
+
let(:native_query_result1) { [ native_query_instance1 ].to_enum }
|
29
|
+
let(:native_query_result2) { [ native_query_instance1, native_query_instance2 ].to_enum }
|
30
|
+
let(:wmi_query_result_empty) { [].to_enum }
|
31
|
+
let(:native_properties1) { wmi_properties1.map { | property, value | double 'WIN32OLE', :name => property } }
|
32
|
+
let(:native_properties2) { wmi_properties2.map { | property, value | double 'WIN32OLE', :name => property } }
|
33
|
+
let(:native_query_instance1) { double 'WIN32OLE', :properties_ => native_properties1, :invoke => 'value1' }
|
34
|
+
let(:native_query_instance2) { double 'WIN32OLE', :properties_ => native_properties2, :invoke => 'value2' }
|
35
|
+
let(:wbem_connection) { double 'WIN32OLE', :ExecQuery => native_query_result }
|
36
|
+
|
37
|
+
def validate_query_result(actual, expected)
|
38
|
+
expect(actual.count).to eql(expected.count)
|
39
|
+
|
40
|
+
index = 0
|
41
|
+
|
42
|
+
expected.each do | expected_value |
|
43
|
+
actual_value = actual[index]
|
44
|
+
expected_value.wmi_ole_object.invoke == actual_value.wmi_ole_object.invoke
|
45
|
+
expected_value.wmi_ole_object.properties_.each do | expected_property |
|
46
|
+
|
47
|
+
expect(actual_value[expected_property.name]).not_to eql(nil)
|
48
|
+
|
49
|
+
names = actual_value.wmi_ole_object.properties_.map { | property | property.name }
|
50
|
+
|
51
|
+
expect(names.include?(expected_property.name)).to eql(true)
|
52
|
+
|
53
|
+
end
|
54
|
+
index += 1
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
before(:each) do
|
59
|
+
stub_const('WIN32OLE', Class.new)
|
60
|
+
WIN32OLE.stub(:new).with("WbemScripting.SWbemLocator").and_return(wbem_locator)
|
61
|
+
stub_const('WIN32OLERuntimeError', Class.new(Exception))
|
62
|
+
end
|
63
|
+
|
64
|
+
let(:wmi) { WmiLite::Wmi.new }
|
65
|
+
let(:wmi_query_result) { wmi_query_result_empty }
|
66
|
+
let(:native_query_result) { [].to_enum }
|
67
|
+
|
68
|
+
it "should not fail with empty query results" do
|
69
|
+
results = wmi.query('')
|
70
|
+
result_count = 0
|
71
|
+
results.each { | result | result_count += 1 }
|
72
|
+
|
73
|
+
expect( result_count ).to eq(0)
|
74
|
+
end
|
75
|
+
|
76
|
+
shared_examples_for "the first_of method" do
|
77
|
+
|
78
|
+
let(:wmi_properties1) { { 'cores' => 4, 'name' => 'mycomputer1', 'diskspace' => 400, 'os' => 'windows' } }
|
79
|
+
let(:wmi_properties2) { { 'cores' => 2, 'name' => 'mycomputer2', 'bios' => 'ami', 'os' => 'windows' } }
|
80
|
+
let(:native_query_result) { [].to_enum }
|
81
|
+
|
82
|
+
it "should not fail with empty query results" do
|
83
|
+
results = wmi.first_of('vm')
|
84
|
+
expect( results ).to eq(nil)
|
85
|
+
end
|
86
|
+
|
87
|
+
context "when returning one instance in the query" do
|
88
|
+
let(:wmi_query_result) { wmi_query_result1 }
|
89
|
+
let(:native_query_result) { native_query_result1 }
|
90
|
+
|
91
|
+
it "should get one instance" do
|
92
|
+
results = wmi.first_of('vm')
|
93
|
+
expected_result = WmiLite::Wmi::Instance.new(native_query_result.first)
|
94
|
+
validate_query_result([results], [expected_result])
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
context "when returning more than one instance in the query" do
|
99
|
+
let(:wmi_query_result) { wmi_query_result2 }
|
100
|
+
let(:native_query_result) { native_query_result2 }
|
101
|
+
|
102
|
+
it "should get one instance" do
|
103
|
+
results = wmi.first_of('vm')
|
104
|
+
expected_result = WmiLite::Wmi::Instance.new(native_query_result.first)
|
105
|
+
validate_query_result([results], [expected_result])
|
106
|
+
end
|
107
|
+
end
|
108
|
+
|
109
|
+
end
|
110
|
+
|
111
|
+
shared_examples_for "the instances_of method" do
|
112
|
+
|
113
|
+
let(:wmi_properties1) { { 'cores' => 4, 'name' => 'mycomputer1', 'diskspace' => 400, 'os' => 'windows' } }
|
114
|
+
let(:wmi_properties2) { { 'cores' => 2, 'name' => 'mycomputer2', 'bios' => 'ami', 'os' => 'windows' } }
|
115
|
+
let(:native_query_result) { [].to_enum }
|
116
|
+
|
117
|
+
it "should not fail with empty query results" do
|
118
|
+
results = wmi.instances_of('vm')
|
119
|
+
expect( results ).to eq([])
|
120
|
+
end
|
121
|
+
|
122
|
+
context "when returning one instance in the query" do
|
123
|
+
let(:wmi_query_result) { wmi_query_result1 }
|
124
|
+
let(:native_query_result) { native_query_result1 }
|
125
|
+
|
126
|
+
it "should get one instance" do
|
127
|
+
results = wmi.instances_of('vm')
|
128
|
+
index = 0
|
129
|
+
expected_result = results.map do | result |
|
130
|
+
WmiLite::Wmi::Instance.new(result.wmi_ole_object)
|
131
|
+
end
|
132
|
+
validate_query_result(results, expected_result)
|
133
|
+
end
|
134
|
+
end
|
135
|
+
|
136
|
+
context "when returning one instance in the query" do
|
137
|
+
let(:wmi_query_result) { wmi_query_result2 }
|
138
|
+
let(:native_query_result) { native_query_result2 }
|
139
|
+
|
140
|
+
it "should get one instance" do
|
141
|
+
results = wmi.instances_of('vm')
|
142
|
+
index = 0
|
143
|
+
expected_result = results.map do | result |
|
144
|
+
WmiLite::Wmi::Instance.new(result.wmi_ole_object)
|
145
|
+
end
|
146
|
+
validate_query_result(results, expected_result)
|
147
|
+
end
|
148
|
+
end
|
149
|
+
|
150
|
+
end
|
151
|
+
|
152
|
+
shared_examples_for 'an invalid query' do
|
153
|
+
let(:unparseable_error) { 'unparseableerror' }
|
154
|
+
it 'should raise an exception' do
|
155
|
+
wbem_connection.should_receive(:ExecQuery).and_raise(WIN32OLERuntimeError)
|
156
|
+
wmi_service = WmiLite::Wmi.new
|
157
|
+
expect { wmi_service.send(query_method, wmi_query) }.to raise_error(WmiLite::WmiException)
|
158
|
+
end
|
159
|
+
|
160
|
+
it 'should raise an exception that ends with the original exception message' do
|
161
|
+
wbem_connection.should_receive(:ExecQuery).and_raise(WIN32OLERuntimeError.new(unparseable_error))
|
162
|
+
wmi_service = WmiLite::Wmi.new
|
163
|
+
error_message = nil
|
164
|
+
begin
|
165
|
+
wmi_service.send(query_method, wmi_query)
|
166
|
+
rescue WmiLite::WmiException => e
|
167
|
+
error_message = e.message
|
168
|
+
end
|
169
|
+
|
170
|
+
# Exception messages look a like a customized error string followed by
|
171
|
+
# the original, less friendly message. A change here affects only
|
172
|
+
# aestethics of human diagnostics, this may be changed with no effect
|
173
|
+
# on libraries or applications.
|
174
|
+
expect(error_message).not_to eql(nil)
|
175
|
+
expect(e.message.start_with?(unparseable_error)).to eql(false)
|
176
|
+
expect(e.message.end_with?(unparseable_error)).to eql(true)
|
177
|
+
end
|
178
|
+
end
|
179
|
+
|
180
|
+
shared_examples_for 'an invalid namespace' do
|
181
|
+
let(:unparseable_error) { 'unparseableerror' }
|
182
|
+
it 'should raise an exception' do
|
183
|
+
wbem_locator.should_receive(:ConnectServer).and_raise(WIN32OLERuntimeError)
|
184
|
+
wmi_service = WmiLite::Wmi.new('notavalidnamespace')
|
185
|
+
expect { wmi_service.send(query_method, wmi_query) }.to raise_error(WmiLite::WmiException)
|
186
|
+
end
|
187
|
+
|
188
|
+
it 'should raise an exception that starts with the original exception message' do
|
189
|
+
wbem_locator.should_receive(:ConnectServer).and_raise(WIN32OLERuntimeError.new(unparseable_error))
|
190
|
+
wmi_service = WmiLite::Wmi.new
|
191
|
+
error_message = nil
|
192
|
+
begin
|
193
|
+
wmi_service.send(query_method, wmi_query)
|
194
|
+
rescue WmiLite::WmiException => e
|
195
|
+
error_message = e.message
|
196
|
+
end
|
197
|
+
|
198
|
+
# See previous comment on this validation of human readability -- a change
|
199
|
+
# to the format / content of these messages will not break applications.
|
200
|
+
expect(error_message).not_to eql(nil)
|
201
|
+
expect(error_message.start_with?(unparseable_error)).to eql(false)
|
202
|
+
expect(error_message.end_with?(unparseable_error)).to eql(true)
|
203
|
+
end
|
204
|
+
end
|
205
|
+
|
206
|
+
shared_examples_for "the query method" do
|
207
|
+
|
208
|
+
let(:wmi_properties1) { { 'cores' => 4, 'name' => 'mycomputer1', 'diskspace' => 400, 'os' => 'windows' } }
|
209
|
+
let(:wmi_properties2) { { 'cores' => 2, 'name' => 'mycomputer2', 'bios' => 'ami', 'os' => 'windows' } }
|
210
|
+
let(:native_query_result) { [].to_enum }
|
211
|
+
|
212
|
+
it "should not fail with empty query results" do
|
213
|
+
results = wmi.query('vm')
|
214
|
+
expect( results ).to eq([])
|
215
|
+
end
|
216
|
+
|
217
|
+
context "when returning one instance in the query" do
|
218
|
+
let(:wmi_query_result) { wmi_query_result1 }
|
219
|
+
let(:native_query_result) { native_query_result1 }
|
220
|
+
|
221
|
+
it "should get one instance" do
|
222
|
+
results = wmi.query('vm')
|
223
|
+
index = 0
|
224
|
+
expected_result = results.map do | result |
|
225
|
+
WmiLite::Wmi::Instance.new(result.wmi_ole_object)
|
226
|
+
end
|
227
|
+
validate_query_result(results, expected_result)
|
228
|
+
end
|
229
|
+
end
|
230
|
+
|
231
|
+
context "when returning one instance in the query" do
|
232
|
+
let(:wmi_query_result) { wmi_query_result2 }
|
233
|
+
let(:native_query_result) { native_query_result2 }
|
234
|
+
|
235
|
+
it "should get one instance" do
|
236
|
+
results = wmi.query('vm')
|
237
|
+
index = 0
|
238
|
+
expected_result = results.map do | result |
|
239
|
+
WmiLite::Wmi::Instance.new(result.wmi_ole_object)
|
240
|
+
end
|
241
|
+
validate_query_result(results, expected_result)
|
242
|
+
end
|
243
|
+
end
|
244
|
+
|
245
|
+
end
|
246
|
+
|
247
|
+
context "when constructing a Ruby class instance" do
|
248
|
+
it "should not connect to WMI in the constructor" do
|
249
|
+
WmiLite::Wmi.any_instance.should_not_receive(:connect_to_namespace)
|
250
|
+
wmi_service_nil_namespace = WmiLite::Wmi.new
|
251
|
+
wmi_service_explicit_namespace = WmiLite::Wmi.new('root/cimv2')
|
252
|
+
end
|
253
|
+
end
|
254
|
+
|
255
|
+
context "when calling query methods" do
|
256
|
+
it "should only connect to WMI on the first query execution" do
|
257
|
+
WIN32OLE.should_receive(:new).with("WbemScripting.SWbemLocator").exactly(1).times.and_return(wbem_locator)
|
258
|
+
wmi_service = WmiLite::Wmi.new
|
259
|
+
|
260
|
+
# Make a lot of queries to be sure the connection is only created once
|
261
|
+
wmi_service.query('select * from Win32_Process')
|
262
|
+
wmi_service.query('select * from Win32_Process')
|
263
|
+
wmi_service.instances_of('Win32_Processor')
|
264
|
+
wmi_service.instances_of('Win32_Processor')
|
265
|
+
wmi_service.first_of('Win32_Group')
|
266
|
+
wmi_service.first_of('Win32_Group')
|
267
|
+
end
|
268
|
+
end
|
269
|
+
|
270
|
+
context 'when making invalid queries' do
|
271
|
+
let(:namespace) { nil }
|
272
|
+
|
273
|
+
let(:wmi_query) { 'invalidclass' }
|
274
|
+
let(:query_method) { :first_of }
|
275
|
+
|
276
|
+
it_behaves_like 'an invalid query'
|
277
|
+
it_behaves_like 'an invalid namespace'
|
278
|
+
|
279
|
+
let(:query_method) { :instances_of }
|
280
|
+
it_behaves_like 'an invalid query'
|
281
|
+
it_behaves_like 'an invalid namespace'
|
282
|
+
|
283
|
+
let(:query_method) { :query }
|
284
|
+
let(:wmi_query) { 'nosql_4_life' }
|
285
|
+
it_behaves_like 'an invalid query'
|
286
|
+
it_behaves_like 'an invalid namespace'
|
287
|
+
end
|
288
|
+
|
289
|
+
it_should_behave_like "the first_of method"
|
290
|
+
|
291
|
+
it_should_behave_like "the instances_of method"
|
292
|
+
|
293
|
+
it_should_behave_like "the query method"
|
294
|
+
|
295
|
+
end
|
data/wmi-lite.gemspec
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'wmi-lite/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = 'wmi-lite'
|
8
|
+
spec.version = WmiLite::VERSION
|
9
|
+
spec.authors = ['Adam Edwards']
|
10
|
+
spec.email = ['dev@getchef.com']
|
11
|
+
spec.description = 'A lightweight utility over win32ole for accessing basic ' \
|
12
|
+
'WMI (Windows Management Instrumentation) functionality ' \
|
13
|
+
'in the Microsoft Windows operating system. It has no ' \
|
14
|
+
'runtime dependencies other than Ruby, so it can be used ' \
|
15
|
+
'without concerns around dependency issues.'
|
16
|
+
spec.summary = 'A lightweight utility library for accessing basic WMI ' \
|
17
|
+
'(Windows Management Instrumentation) functionality on Windows'
|
18
|
+
spec.homepage = 'https://github.com/opscode/wmi-lite'
|
19
|
+
spec.license = 'Apache 2.0'
|
20
|
+
|
21
|
+
spec.required_ruby_version = '>= 1.9'
|
22
|
+
|
23
|
+
spec.files = `git ls-files`.split($/)
|
24
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
25
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
26
|
+
spec.require_paths = ['lib']
|
27
|
+
|
28
|
+
spec.add_development_dependency 'bundler', '~> 1.3'
|
29
|
+
spec.add_development_dependency 'rspec'
|
30
|
+
spec.add_development_dependency 'rake'
|
31
|
+
spec.add_development_dependency 'pry'
|
32
|
+
spec.add_development_dependency 'pry-debugger'
|
33
|
+
end
|
metadata
ADDED
@@ -0,0 +1,140 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: wmi-lite
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0.rc.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Adam Edwards
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-05-19 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ~>
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.3'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.3'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rspec
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ! '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ! '>='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ! '>='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ! '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: pry
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ! '>='
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: pry-debugger
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ! '>='
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ! '>='
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
description: A lightweight utility over win32ole for accessing basic WMI (Windows
|
84
|
+
Management Instrumentation) functionality in the Microsoft Windows operating system.
|
85
|
+
It has no runtime dependencies other than Ruby, so it can be used without concerns
|
86
|
+
around dependency issues.
|
87
|
+
email:
|
88
|
+
- dev@getchef.com
|
89
|
+
executables: []
|
90
|
+
extensions: []
|
91
|
+
extra_rdoc_files: []
|
92
|
+
files:
|
93
|
+
- .gitignore
|
94
|
+
- .rspec
|
95
|
+
- .travis.yml
|
96
|
+
- CHANGELOG.md
|
97
|
+
- CONTRIBUTING.md
|
98
|
+
- Gemfile
|
99
|
+
- LICENSE
|
100
|
+
- README.md
|
101
|
+
- Rakefile
|
102
|
+
- lib/wmi-lite.rb
|
103
|
+
- lib/wmi-lite/version.rb
|
104
|
+
- lib/wmi-lite/wmi.rb
|
105
|
+
- lib/wmi-lite/wmi_exception.rb
|
106
|
+
- lib/wmi-lite/wmi_instance.rb
|
107
|
+
- spec/functional/wmi_spec.rb
|
108
|
+
- spec/spec_helper.rb
|
109
|
+
- spec/unit/wmi_spec.rb
|
110
|
+
- wmi-lite.gemspec
|
111
|
+
homepage: https://github.com/opscode/wmi-lite
|
112
|
+
licenses:
|
113
|
+
- Apache 2.0
|
114
|
+
metadata: {}
|
115
|
+
post_install_message:
|
116
|
+
rdoc_options: []
|
117
|
+
require_paths:
|
118
|
+
- lib
|
119
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
120
|
+
requirements:
|
121
|
+
- - ! '>='
|
122
|
+
- !ruby/object:Gem::Version
|
123
|
+
version: '1.9'
|
124
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
125
|
+
requirements:
|
126
|
+
- - ! '>'
|
127
|
+
- !ruby/object:Gem::Version
|
128
|
+
version: 1.3.1
|
129
|
+
requirements: []
|
130
|
+
rubyforge_project:
|
131
|
+
rubygems_version: 2.1.11
|
132
|
+
signing_key:
|
133
|
+
specification_version: 4
|
134
|
+
summary: A lightweight utility library for accessing basic WMI (Windows Management
|
135
|
+
Instrumentation) functionality on Windows
|
136
|
+
test_files:
|
137
|
+
- spec/functional/wmi_spec.rb
|
138
|
+
- spec/spec_helper.rb
|
139
|
+
- spec/unit/wmi_spec.rb
|
140
|
+
has_rdoc:
|