From: Jano Svitok Date: 2008-06-20T21:34:38+09:00 Subject: Re: Two Ruby equivalents to Powershell commands... On Fri, Jun 20, 2008 at 14:21, Nicholas Calvert wrote: > Cheers Gordon, you are a life saver. Whilst im on the same subject, i am > also having trouble creating a new GUID. With Powershell i can do: > > $NEWGUID = [GUID]::NewGUID().ToString() > > Do you think its possible to do this with Ruby and ruby-wmi? This is how I create GUIDs (requires win32utils gem, might not be tested - I'm reinstalling ruby right now): require 'windows/com' require 'windows/unicode' class String # Return the portion of the string up to the first NULL character. This # was added for both speed and convenience. def nstrip self[ /^[^\0]*/ ] end end class Guid BUFFER_SIZE = 100 attr_reader :data def initialize(data = nil) @data = data create if data.nil? end def create @data = 0.chr * 16 raise 'GUID Error' unless CoCreateGuid(@data) end def to_s ret = 0.chr * BUFFER_SIZE i = StringFromGUID2(@data, ret, BUFFER_SIZE) wide_to_multi(ret[0..i*2]).nstrip end private include Windows::COM include Windows::Unicode end if File.expand_path(__FILE__) == File.expand_path($0) puts Guid.new.to_s end