Method: Jamf::Script#run

Defined in:
lib/jamf/api/classic/api_objects/script.rb

#run(**opts) ⇒ Array<(Integer,String)>

Run this script on the current machine.

If the script code is available in the #script_contents attribute, then that code is saved to a tmp file, and executed. The tmp file is deleted immediately after running

After the script runs, this method returns a two-item Array.

  • the first item is an Integer, the exit status of the script itself (0 means success)

  • the second item is a String, the output (stdout + stderr) of the script.

The exit status of the jamf binary process will be available as a Process::Status object in $? immediately after running.

Parameters:

  • opts (Hash)

    the options for running the script

Options Hash (**opts):

  • :target (String, Pathname)

    the ‘target drive’, passed to the script as the first commandline option. Defaults to ‘/’

  • :computer_name (String)

    the name of the computer, passed to the script as the second commandline option. Defaults to the name of the current machine

  • :username (String)

    the username to be passed to the script as the third commandline option. Defaults to the current console user.

  • :p4..:p11 (String)

    the values to be passed as the 4th - 11th commandline params Script params 1, 2, & 3 are the target:, computer_name: and username: params

  • :show_output (Boolean)

    should the output (stdout + stderr) be copied to stdout in realtime, as well as returned?

Returns:

  • (Array<(Integer,String)>)

    the exit status and stdout+stderr of the script

Raises:



375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
# File 'lib/jamf/api/classic/api_objects/script.rb', line 375

def run(**opts)
  raise Jamf::MissingDataError, 'script_contents does not start with #!' unless @script_contents.to_s.start_with? '#!'

  opts[:target] ||= '/'
  opts[:computer_name] ||= Jamf::Client.run_jamf('getComputerName')[/>(.*?)</, 1]
  opts[:username] ||= Jamf::Client.console_user

  params = [opts[:target], opts[:computer_name], opts[:username]]
  params << opts[:p4]
  params << opts[:p5]
  params << opts[:p6]
  params << opts[:p7]
  params << opts[:p8]
  params << opts[:p9]
  params << opts[:p10]
  params << opts[:p11]

  # everything must be a string
  params.map!(&:to_s)

  # remove nils
  params.compact!

  # remove empty strings
  params.delete_if(&:empty?)

  return_value = []

  # Save and run the script from a private temp dir
  # which will be deleted when finished
  require 'tmpdir'
  Dir.mktmpdir do |dir|
    executable = Pathname.new "#{dir}/#{@name}"
    executable.jss_touch
    executable.chmod 0o700
    executable.jss_save @script_contents

    cmd = [executable.to_s]
    cmd += params

    stdout_and_stderr_str, status = Open3.capture2e(*cmd)

    return_value << status.exitstatus
    return_value << stdout_and_stderr_str
  end # Dir.mktmpdirs

  return_value
end