Sha256: bcbe7805b5820d4843e4cbce97a7dba17483f7ac025060615d09374f1702a3c3

Contents?: true

Size: 1.72 KB

Versions: 67

Compression:

Stored size: 1.72 KB

Contents

module Facter::Util::WMI
  class << self

    # Impersonation Level Constants
    # https://msdn.microsoft.com/en-us/library/windows/desktop/ms693790%28v=vs.85%29.aspx
    RPC_C_IMP_LEVEL_DEFAULT       = 0
    RPC_C_IMP_LEVEL_ANONYMOUS     = 1
    RPC_C_IMP_LEVEL_IDENTIFY      = 2
    RPC_C_IMP_LEVEL_IMPERSONATE   = 3
    RPC_C_IMP_LEVEL_DELEGATE      = 4

    # returns a COM class implementing ISWbemServicesEx
    # prior to Facter 2.5.0, this defaulted to using a moniker
    # but now defaults to using COM classes directly to support Nano
    # backward compatibility is maintained in case custom facts specified a moniker
    # but note that passing in the uri parameter can never work on Nano
    def connect(uri = nil)
      require 'win32ole'
      uri.nil? ?
        connect2() :
        # NOTE: in the future it would be better to parse a given moniker uri / call connect2
        WIN32OLE.connect(uri)
    end

    # @deprecated
    def wmi_resource_uri( host = '.' )
      "winmgmts:{impersonationLevel=impersonate}!//#{host}/root/cimv2"
    end

    def execquery(query)
      connect().execquery(query)
    end

    private

    # this mimics the previous behavior of using the COM moniker
    # winmgmts:{impersonationLevel=impersonate}!//./root/cimv2
    # which is not supported on Nano Server
    def connect2( server = '.', namespace = 'root\\cimv2', impersonation_level = RPC_C_IMP_LEVEL_IMPERSONATE )
      locator = WIN32OLE.new("WbemScripting.SWbemLocator")
      # https://msdn.microsoft.com/en-us/library/aa393720%28v=vs.85%29.aspx
      # ConnectServer returns an ISWbemServicesEx
      conn = locator.ConnectServer(server, namespace)
      conn.Security_.ImpersonationLevel = impersonation_level
      conn
    end

  end
end

Version data entries

67 entries across 67 versions & 2 rubygems

Version Path
facter-2.5.7 lib/facter/util/wmi.rb
facter-2.5.7-x86-mingw32 lib/facter/util/wmi.rb
facter-2.5.7-x64-mingw32 lib/facter/util/wmi.rb
facter-2.5.7-universal-darwin lib/facter/util/wmi.rb
facter-2.5.6-x86-mingw32 lib/facter/util/wmi.rb
facter-2.5.6-x64-mingw32 lib/facter/util/wmi.rb
facter-2.5.6-universal-darwin lib/facter/util/wmi.rb
facter-2.5.6 lib/facter/util/wmi.rb
facter-2.5.5-x86-mingw32 lib/facter/util/wmi.rb
facter-2.5.5-x64-mingw32 lib/facter/util/wmi.rb
facter-2.5.5-universal-darwin lib/facter/util/wmi.rb
facter-2.5.5 lib/facter/util/wmi.rb
facter-2.5.4-x86-mingw32 lib/facter/util/wmi.rb
facter-2.5.4-x64-mingw32 lib/facter/util/wmi.rb
facter-2.5.4-universal-darwin lib/facter/util/wmi.rb
facter-2.5.4 lib/facter/util/wmi.rb
bolt-0.24.0 vendored/facter/lib/facter/util/wmi.rb
bolt-0.23.0 vendored/facter/lib/facter/util/wmi.rb
bolt-0.22.0 vendored/facter/lib/facter/util/wmi.rb
bolt-0.21.8 vendored/facter/lib/facter/util/wmi.rb