Method: Inspec::Resources::WindowsTasks#info

Defined in:
lib/inspec/resources/windows_task.rb

#infoObject



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/inspec/resources/windows_task.rb', line 73

def info
  return @cache unless @cache.nil?

  # PowerShell v5 has Get-ScheduledTask cmdlet,
  # _using something with backward support to v3_
  # script = "Get-ScheduledTask | ? { $_.URI -eq '#{@taskuri}' } | Select-Object URI,@{N='State';E={$_.State.ToString()}} | ConvertTo-Json"

  # Using schtasks as suggested by @modille but aligning property names to match cmdlet to future proof.
  script = "schtasks /query /v /fo csv /tn '#{@taskuri}' | ConvertFrom-Csv | Select @{N='URI';E={$_.TaskName}},@{N='State';E={$_.Status.ToString()}},'Logon Mode','Last Result','Task To Run','Run As User','Scheduled Task State' | ConvertTo-Json -Compress"

  cmd = inspec.powershell(script)

  begin
    params = JSON.parse(cmd.stdout)
  rescue JSON::ParserError => _e
    return nil
  end

  # If multiple triggers are defined, `schtasks` returns a list.
  # This merges that list with the latest item taking precedence.
  # This is the same behavior as `Get-ScheduledTask`.
  params = params.reduce(:merge) if params.is_a?(Array)

  @cache = {
    uri: params["URI"],
    state: params["State"],
    logon_mode: params["Logon Mode"],
    last_result: params["Last Result"],
    task_to_run: params["Task To Run"],
    run_as_user: params["Run As User"],
    scheduled_task_state: params["Scheduled Task State"],
    type: "windows-task",
  }
end