# Ruby and WMI

**URL:** <https://www.ruby-forum.com/t/ruby-and-wmi/81458>\
**Category:** Ruby\
**Created:** [November 18, 2005, 7:35pm UTC](https://www.ruby-forum.com/t/ruby-and-wmi/81458 "2005-11-18T19:35:27Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![timgerrlists](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ee7513/32.png) [@timgerrlists](https://www.ruby-forum.com/u/timgerrlists)\
**Post date:** [November 18, 2005, 7:35pm UTC](https://www.ruby-forum.com/t/ruby-and-wmi/81458/1 "2005-11-18T19:35:27Z")

</div>

I have been playing with ruby for a few weeks now to do windows  
administration. I haved used ruby with WMI on harder scripts but cant  
figure out what to do here. I want to query remote computers and see  
who is in the administratiors group.

require ‘win32ole’

cpuName = ‘127.0.0.1’  
mgmt = WIN32OLE.connect(“WinNT://#{cpuName}/Administrators,group”)  
mgmt.InstancesOf(“Members”).each{|person| puts person.Name} \<---- This  
fails

The last line fails because I am not sure how to structure it. Here is  
the equivelent VBscript

Set objGroup = GetObject(“WinNT://” & strComputer &  
“/Administrators,group”)  
For Each mem In objGroup.Members  
WScript.echo vbTab & Right(mem.adsPath,Len(mem.adsPath) -  
8)

I thank any help.

---

<div class="post-metadata">

**Author:** ![timgerrlists](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ee7513/32.png) [@timgerrlists](https://www.ruby-forum.com/u/timgerrlists)\
**Post date:** [November 19, 2005, 2:54pm UTC](https://www.ruby-forum.com/t/ruby-and-wmi/81458/2 "2005-11-19T14:54:29Z")

</div>

timgerrlists wrote:

> fails
> 
> The last line fails because I am not sure how to structure it. Here is  
> the equivelent VBscript
> 
> Set objGroup = GetObject(“WinNT://” & strComputer &  
> “/Administrators,group”)  
> For Each mem In objGroup.Members  
> WScript.echo vbTab & Right(mem.adsPath,Len(mem.adsPath) -  
> 8)

The following are semantically equivalent. Choose the prettier of the  
two.

mgmt.Members.each {|person| puts person.AdsPath[8…-1] }

for person in mgmt.Members  
puts person.AdsPath[8…-1]  
end

> I thank any help.

You’re welcome.

Cheers,  
Dave

---

<div class="post-metadata">

**Author:** ![timgerrlists](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ee7513/32.png) [@timgerrlists](https://www.ruby-forum.com/u/timgerrlists)\
**Post date:** [November 21, 2005, 2:14pm UTC](https://www.ruby-forum.com/t/ruby-and-wmi/81458/3 "2005-11-21T14:14:59Z")

</div>

Dave B. wrote:

> > mgmt.InstancesOf(“Members”).each{|person| puts person.Name} \<---- This
> 
> You’re welcome.
> 
> Cheers,  
> Dave

Dave thank you for the informaiton but I am getting some errors, "  
Untitled2.rb:2: undefined local variable or method `cpuName’ for  
main:Object (NameError)" Not sure what to do here.

Thanks,  
timgerr

---

<div class="post-metadata">

**Author:** ![timgerrlists](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ee7513/32.png) [@timgerrlists](https://www.ruby-forum.com/u/timgerrlists)\
**Post date:** [November 21, 2005, 5:32pm UTC](https://www.ruby-forum.com/t/ruby-and-wmi/81458/4 "2005-11-21T17:32:45Z")

</div>

timgerrlists wrote:

> Dave thank you for the informaiton but I am getting some errors, "  
> Untitled2.rb:2: undefined local variable or method `cpuName’ for  
> main:Object (NameError)" Not sure what to do here.

You haven’t defined cpuName. If you post the script you’re using, I can  
be  
more specific. The following script should work just like the VBScript  
you  
posted. (You might notice it also looks very similar.)

require ‘win32ole’  
strComputer = “localhost”  
objGroup =  
WIN32OLE.connect(“WinNT://#{strComputer}/Administrators,group”)  
for mem in objGroup.Members  
puts “\t” + mem.adsPath[8…-1]  
end

Cheers,  
Dave

---

<div class="post-metadata">

**Author:** ![timgerrlists](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ee7513/32.png) [@timgerrlists](https://www.ruby-forum.com/u/timgerrlists)\
**Post date:** [November 21, 2005, 7:33pm UTC](https://www.ruby-forum.com/t/ruby-and-wmi/81458/5 "2005-11-21T19:33:50Z")

</div>

Dave B. wrote:

> strComputer = “localhost”  
> objGroup = WIN32OLE.connect(“WinNT://#{strComputer}/Administrators,group”)  
> for mem in objGroup.Members  
> puts “\t” + mem.adsPath[8…-1]  
> end
> 
> Cheers,  
> Dave

Thank you, it now works, can you tell me one thing, ( I am learning)  
what does [8…-1] mean in the script “puts “\t” + mem.adsPath[8…-1]”.

Thanks again.

---

<div class="post-metadata">

**Author:** ![timgerrlists](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ee7513/32.png) [@timgerrlists](https://www.ruby-forum.com/u/timgerrlists)\
**Post date:** [November 21, 2005, 8:22pm UTC](https://www.ruby-forum.com/t/ruby-and-wmi/81458/6 "2005-11-21T20:22:09Z")

</div>

On 11/21/05, timgerrlists [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Thank you, it now works, can you tell me one thing, ( I am learning)  
> what does [8…-1] mean in the script “puts “\t” + mem.adsPath[8…-1]”.

Indexing with a range [something…-1] will go to the end of the  
array/string.

irb(main):001:0\> a = [0, 1, 2, 3, 4]  
=\> [0, 1, 2, 3, 4]  
irb(main):002:0\> a[2…-1]  
=\> [2, 3, 4]  
irb(main):003:0\> b = “Hello, World!”  
=\> “Hello, World!”  
irb(main):005:0\> b[7…-1]  
=\> “World!”
