# Ruby & WMI (win32ole)

**URL:** <https://rubytalk.org/t/ruby-wmi-win32ole/47185>\
**Category:** ruby-talk\
**Created:** [17 June 2008 12:31 UTC](https://rubytalk.org/t/ruby-wmi-win32ole/47185 "2008-06-17T12:31:16Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nicholas\_Calvert](https://avatars.discourse-cdn.com/v4/letter/n/848f3c/32.png) [@Nicholas\_Calvert](https://rubytalk.org/u/Nicholas_Calvert)\
**Post date:** [17 June 2008 12:31 UTC](https://rubytalk.org/t/ruby-wmi-win32ole/47185/1 "2008-06-17T12:31:16Z")

</div>

Hi guys,

First off, hello 🙂 New to the community, looks very helpful and active  
indeed...

As i say, I am new to Ruby and am trying to port some WMI Powershell  
code over – I am not having much luck... My goal is to automate Hyper-V  
on Windows 2008, as an example at a very simple level in powershell I  
can do:

## Define variable for getting VSMS class  
$VSMS = get-wmiobject MSVM\_VirtualSystemManagementService -namespace  
"root\virtualization"  
## Then call a method on that class like this. Definevirtualsystem is  
the method in question, I call it with no parameters.  
$VSMS.definevirtualsystem()

I have tried two libarys in Ruby, win32ole and ruby-wmi. The synatx for  
ruby-wmi confused me the most, so can only post a (kind of) working  
example with win32ole:

require 'win32ole'  
wmi =  
WIN32OLE.connect("winmgmts:\\\\localhost\\root\\virtualization:Msvm\_VirtualSystemManagementService")  
wmi.ExecMethod(DefineVirtualSystem())

... this gives me the following error:  
test.rb:4: undefined method `DefineVirtualSystem' for main:Object  
(NoMethodError)

Also one of the tricks with WMI is to get the XML output of a class,  
change the settings and squirt it back.

Using the previous example, i could do:

$VSMS.psbase.gettext(1)

...to give myself the raw XML. Is there an easy way to do this in Ruby?

Any pointers would be greatly appreciated.

Cheers,

Nick

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Masaki\_Suketa](https://avatars.discourse-cdn.com/v4/letter/m/ecccb3/32.png) [@Masaki\_Suketa](https://rubytalk.org/u/Masaki_Suketa)\
**Post date:** [18 June 2008 10:49 UTC](https://rubytalk.org/t/ruby-wmi-win32ole/47185/2 "2008-06-18T10:49:07Z")

</div>

Hello,  
Nicholas Calvert wrote:

> I have tried two libarys in Ruby, win32ole and ruby-wmi. The synatx for  
> ruby-wmi confused me the most, so can only post a (kind of) working  
> example with win32ole:
> 
> require 'win32ole'  
> wmi =  
> WIN32OLE.connect("winmgmts:\\\\localhost\\root\\virtualization:Msvm\_VirtualSystemManagementService")  
> wmi.ExecMethod(DefineVirtualSystem())

I have not used Powershell, so I'm not sure, but  
How about the following script?  
&nbsp;&nbsp;&nbsp;wmi.ExecMethod('DefineVirtualSystem')

&nbsp;&nbsp;&nbsp;Regards,  
&nbsp;&nbsp;&nbsp;Masaki Suketa
