Class: Inspec::Resources::WindowsTasks

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

Instance Method Summary collapse

Constructor Details

#initialize(taskuri) ⇒ WindowsTasks

Returns a new instance of WindowsTasks.



29
30
31
32
33
34
35
# File 'lib/resources/windows_task.rb', line 29

def initialize(taskuri)
  @taskuri = taskuri
  @cache = nil

  # verify that this resource is only supported on Windows
  return skip_resource 'The `windows_task` resource is not supported on your OS.' unless inspec.os.windows?
end

Instance Method Details

#disabled?Boolean

Returns:

  • (Boolean)


48
49
50
51
# File 'lib/resources/windows_task.rb', line 48

def disabled?
  return false if info.nil? || info[:state].nil?
  info[:scheduled_task_state] == 'Disabled' || info[:state] == 'Disabled'
end

#enabled?Boolean

rubocop:disable Style/WordArray

Returns:

  • (Boolean)


43
44
45
46
# File 'lib/resources/windows_task.rb', line 43

def enabled?
  return false if info.nil? || info[:state].nil?
  ['Ready', 'Running'].include?(info[:state])
end

#exists?Boolean

Returns:

  • (Boolean)


37
38
39
40
# File 'lib/resources/windows_task.rb', line 37

def exists?
  return true unless info.nil? || info[:uri].nil?
  false
end

#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
# File 'lib/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

  @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

#last_resultObject



57
58
59
# File 'lib/resources/windows_task.rb', line 57

def last_result
  info[:last_result]
end

#logon_modeObject



53
54
55
# File 'lib/resources/windows_task.rb', line 53

def logon_mode
  info[:logon_mode]
end

#run_as_userObject



65
66
67
# File 'lib/resources/windows_task.rb', line 65

def run_as_user
  info[:run_as_user]
end

#task_to_runObject



61
62
63
# File 'lib/resources/windows_task.rb', line 61

def task_to_run
  info[:task_to_run].to_s.strip
end

#to_sObject



102
103
104
# File 'lib/resources/windows_task.rb', line 102

def to_s
  "Windows Task '#{@taskuri}'"
end

#typeObject



69
70
71
# File 'lib/resources/windows_task.rb', line 69

def type
  info[:type] unless info.nil?
end