Class: WinRM::Shells::Elevated

Inherits:
Object
  • Object
show all
Defined in:
lib/winrm/shells/elevated.rb

Overview

Runs PowerShell commands elevated via a scheduled task

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(connection_opts, transport, logger) ⇒ Elevated

Create a new elevated shell

Parameters:

  • connection_opts (ConnectionOpts)

    The WinRM connection options

  • transport (HttpTransport)

    The WinRM SOAP transport

  • logger (Logger)

    The logger to log diagnostic messages to



29
30
31
32
33
34
35
36
# File 'lib/winrm/shells/elevated.rb', line 29

def initialize(connection_opts, transport, logger)
  @logger = logger
  @username = connection_opts[:user]
  @password = connection_opts[:password]
  @interactive_logon = false
  @shell = Powershell.new(connection_opts, transport, logger)
  @winrm_file_transporter = WinRM::FS::Core::FileTransporter.new(@shell)
end

Instance Attribute Details

#interactive_logonBool

Returns Using an interactive logon.

Returns:

  • (Bool)

    Using an interactive logon



45
46
47
# File 'lib/winrm/shells/elevated.rb', line 45

def interactive_logon
  @interactive_logon
end

#passwordString

Returns The admin user password.

Returns:

  • (String)

    The admin user password



42
43
44
# File 'lib/winrm/shells/elevated.rb', line 42

def password
  @password
end

#usernameString

Returns The admin user name to execute the scheduled task as.

Returns:

  • (String)

    The admin user name to execute the scheduled task as



39
40
41
# File 'lib/winrm/shells/elevated.rb', line 39

def username
  @username
end

Instance Method Details

#closeObject

Closes the shell if one is open



63
64
65
# File 'lib/winrm/shells/elevated.rb', line 63

def close
  @shell.close
end

#run(command, &block) ⇒ WinRM::Output

Run a command or PowerShell script elevated without any of the restrictions that WinRM puts in place.

Parameters:

  • The (String)

    command or PS script to wrap in a scheduled task

Returns:

  • (WinRM::Output)

    :stdout and :stderr



53
54
55
56
57
58
59
60
# File 'lib/winrm/shells/elevated.rb', line 53

def run(command, &block)
  # if an IO object is passed read it, otherwise assume the contents of the file were passed
  script_text = command.respond_to?(:read) ? command.read : command

  script_path = upload_elevated_shell_script(script_text)
  wrapped_script = wrap_in_scheduled_task(script_path, username, password)
  @shell.run(wrapped_script, &block)
end