416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
|
# File 'lib/resources/user.rb', line 416
def identity(username)
account, domain = parse_windows_account(username)
if !domain.nil?
filter = "Name = '#{account}' and Domain = '#{domain}'"
else
filter = "Name = '#{account}' and LocalAccount = true"
end
script = " # find user\n $user = Get-WmiObject Win32_UserAccount -filter \"\#{filter}\"\n # get related groups\n $groups = $user.GetRelated('Win32_Group') | Select-Object -Property Caption, Domain, Name, LocalAccount, SID, SIDType, Status\n # filter user information\n $user = $user | Select-Object -Property Caption, Description, Domain, Name, LocalAccount, Lockout, PasswordChangeable, PasswordExpires, PasswordRequired, SID, SIDType, Status\n # build response object\n New-Object -Type PSObject | `\n Add-Member -MemberType NoteProperty -Name User -Value ($user) -PassThru | `\n Add-Member -MemberType NoteProperty -Name Groups -Value ($groups) -PassThru | `\n ConvertTo-Json\n EOH\n\n cmd = inspec.script(script)\n\n # cannot rely on exit code for now, successful command returns exit code 1\n # return nil if cmd.exit_status != 0, try to parse json\n begin\n params = JSON.parse(cmd.stdout)\n rescue JSON::ParserError => _e\n return nil\n end\n\n user = params['User']['Caption'] unless params['User'].nil?\n groups = params['Groups']\n # if groups is no array, generate one\n groups = [groups] if !groups.is_a?(Array)\n groups = groups.map { |grp| grp['Caption'] } unless params['Groups'].nil?\n\n {\n uid: nil,\n user: user,\n gid: nil,\n group: nil,\n groups: groups,\n }\nend\n"
|