module Puppet::Util::Windows::ADSI

Constants

BACKUP_DOMAIN_CONTROLLER
DOMAIN_ROLES
MAX_COMPUTERNAME_LENGTH

taken from winbase.h

MEMBER_SERVER
MEMBER_WORKSTATION
PRIMARY_DOMAIN_CONTROLLER
STANDALONE_SERVER
STANDALONE_WORKSTATION

docs.microsoft.com/en-us/windows/win32/api/dsrole/ne-dsrole-dsrole_machine_role

Public Class Methods

computer_name() click to toggle source
   # File lib/puppet/util/windows/adsi.rb
51 def computer_name
52   unless @computer_name
53     max_length = MAX_COMPUTERNAME_LENGTH + 1 # NULL terminated
54     FFI::MemoryPointer.new(max_length * 2) do |buffer| # wide string
55       FFI::MemoryPointer.new(:dword, 1) do |buffer_size|
56         buffer_size.write_dword(max_length) # length in TCHARs
57 
58         if GetComputerNameW(buffer, buffer_size) == FFI::WIN32_FALSE
59           raise Puppet::Util::Windows::Error.new(_("Failed to get computer name"))
60         end
61         @computer_name = buffer.read_wide_string(buffer_size.read_dword)
62       end
63     end
64   end
65   @computer_name
66 end
computer_uri(host = '.') click to toggle source
   # File lib/puppet/util/windows/adsi.rb
68 def computer_uri(host = '.')
69   "WinNT://#{host}"
70 end
connect(uri) click to toggle source
   # File lib/puppet/util/windows/adsi.rb
32 def connect(uri)
33   begin
34     WIN32OLE.connect(uri)
35   rescue WIN32OLERuntimeError => e
36     raise Puppet::Error.new( _("ADSI connection error: %{e}") % { e: e }, e )
37   end
38 end
connectable?(uri) click to toggle source
   # File lib/puppet/util/windows/adsi.rb
24 def connectable?(uri)
25   begin
26     !! connect(uri)
27   rescue
28     false
29   end
30 end
create(name, resource_type) click to toggle source
   # File lib/puppet/util/windows/adsi.rb
40 def create(name, resource_type)
41   Puppet::Util::Windows::ADSI.connect(computer_uri).Create(resource_type, name)
42 end
delete(name, resource_type) click to toggle source
   # File lib/puppet/util/windows/adsi.rb
44 def delete(name, resource_type)
45   Puppet::Util::Windows::ADSI.connect(computer_uri).Delete(resource_type, name)
46 end
domain_role() click to toggle source
    # File lib/puppet/util/windows/adsi.rb
114 def domain_role
115   unless @domain_role
116     query_result = Puppet::Util::Windows::ADSI.execquery('select DomainRole from Win32_ComputerSystem').to_enum.first
117     @domain_role = DOMAIN_ROLES[query_result.DomainRole] if query_result
118   end
119   @domain_role
120 end
execquery(query) click to toggle source
    # File lib/puppet/util/windows/adsi.rb
110 def execquery(query)
111   wmi_connection.execquery(query)
112 end
sid_uri(sid) click to toggle source

This method should only be used to generate WinNT://<SID> style monikers used for IAdsGroup::Add / IAdsGroup::Remove. These URIs are not useable to resolve an account with WIN32OLE.connect

    # File lib/puppet/util/windows/adsi.rb
 96 def sid_uri(sid)
 97   raise Puppet::Error.new( _("Must use a valid SID::Principal") ) if !sid.kind_of?(Puppet::Util::Windows::SID::Principal)
 98 
 99   "WinNT://#{sid.sid}"
100 end
sid_uri_safe(sid) click to toggle source

This method should only be used to generate WinNT://<SID> style monikers used for IAdsGroup::Add / IAdsGroup::Remove. These URIs are not usable to resolve an account with WIN32OLE.connect Valid input is a SID::Principal, S-X-X style SID string or any valid account name with or without domain prefix @api private

   # File lib/puppet/util/windows/adsi.rb
82 def sid_uri_safe(sid)
83   return sid_uri(sid) if sid.kind_of?(Puppet::Util::Windows::SID::Principal)
84 
85   begin
86     sid = Puppet::Util::Windows::SID.name_to_principal(sid)
87     sid_uri(sid)
88   rescue Puppet::Util::Windows::Error, Puppet::Error
89     nil
90   end
91 end
uri(resource_name, resource_type, host = '.') click to toggle source
    # File lib/puppet/util/windows/adsi.rb
102 def uri(resource_name, resource_type, host = '.')
103   "#{computer_uri(host)}/#{resource_name},#{resource_type}"
104 end
wmi_connection() click to toggle source
    # File lib/puppet/util/windows/adsi.rb
106 def wmi_connection
107   connect(wmi_resource_uri)
108 end
wmi_resource_uri( host = '.' ) click to toggle source
   # File lib/puppet/util/windows/adsi.rb
72 def wmi_resource_uri( host = '.' )
73   "winmgmts:{impersonationLevel=impersonate}!//#{host}/root/cimv2"
74 end