Class: Inspec::Resources::WindowsGroup

Inherits:
GroupInfo
  • Object
show all
Defined in:
lib/resources/group.rb

Instance Attribute Summary

Attributes inherited from GroupInfo

#inspec

Instance Method Summary collapse

Methods inherited from GroupInfo

#initialize

Constructor Details

This class inherits a constructor from Inspec::Resources::GroupInfo

Instance Method Details

#group_info(compare_group, compare_domain = nil) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/resources/group.rb', line 108

def group_info(compare_group, compare_domain = nil)
  cmd = inspec.command('Get-WmiObject Win32_Group | Select-Object -Property Caption, Domain, Name, SID, LocalAccount | ConvertTo-Json')

  # cannot rely on exit code for now, successful command returns exit code 1
  # return nil if cmd.exit_status != 0, try to parse json
  begin
    groups = JSON.parse(cmd.stdout)
  rescue JSON::ParserError => _e
    return nil
  end

  # ensure we have an array of groups
  groups = [groups] if !groups.is_a?(Array)

  # reduce list
  groups.each_with_object([]) do |grp, grp_collection|
    # map object
    grp_info = {
      name: grp['Name'],
      domain: grp['Domain'],
      caption: grp['Caption'],
      gid: nil,
      sid: grp['SID'],
      local: grp['LocalAccount'],
    }
    return grp_collection.push(grp_info) if grp_info[:name].casecmp(compare_group) == 0 && (compare_domain.nil? || grp_info[:domain].casecmp(compare_domain) == 0)
  end
end