Previous Topic: run-remote Command--Execute a Script on Remote Systems (Funclet)Next Topic: run-SSHShell Command--Run an Interactive SSH Command Line (Funclet)


run-SSHCommand Command--Run a Remote Command Through SSH (Funclet)

The run-SSHCommand executes a command within an existing SSH session or creates a session for command execution. The session is destroyed after the specified command is executed. Although AutoShell uses SSH as its primary remote communication conduit, the remote node for this command does not require a running client AutoShell running. The run-remote command is preferable for executing commands on remote nodes running the client AutoShell because it returns more detailed information about the command execution.

Commands can be executed on any system running an SSH server. The available commands and their options depend on the configured shell on the remote node.

The command has the following syntax:

run-SSHCommand [-host hostname] [-user username] [-pass password] [-key key phrase] 
[-prompt prompt] -command cmd [-port portnumber] [-session ssh]
-host hostname

The name of the SSH server.

-user username

(Optional) Specifies the user name to log in to the remote node. A user name is required for password or public key authentication. If no user name is specified, the user name entered during AutoShell login is used. Unquoted argument tokens are automatically stringified. Prevent automatic quoting for expressions by placing expression code in parentheses.

Default: $$User

-pass password

(Optional) Specifies the password to use to log in to the remote node. If no password is specified, the password entered during AutoShell login is used. To enter a hidden password after issuing this command, specify PW_GET() with an optional prompt as argument. Unquoted argument tokens are automatically stringified. Prevent automatic quoting for expressions by placing expression code in parentheses.

Default: $$Pass

-key key

(Optional) Specifies an absolute or relative path to a file containing a private RSA key to use to log in to the remote node. If a private key and a password are specified, AutoShell attempts a public key logon first, and if that fails, a password logon. Unquoted argument tokens are automatically stringified. Prevent automatic quoting for expressions by placing expression code in parentheses. This parameter is only required when creating an SSH session on the fly using public key authentication.

Default: ""

phrase

Specifies the passphrase for a private key. If the key is not encrypted, the passphrase is not required. To enter a hidden password after issuing this command, specify PW_GET() with an optional prompt as argument. Unquoted argument tokens are automatically stringified. Prevent automatic quoting for expressions by placing expression code in parentheses.

Default: ""

-prompt prompt

(Optional) Specifies the fixed portion at the end of the prompt string displayed by the SSH server to indicate it is ready for input (for example: "::->" for AutoShell). This information is required to determine the completion of commands executed during the SSH session. Specify as many fixed characters as possible. Unquoted argument tokens are automatically stringified. Prevent automatic quoting for expressions by placing expression code in parentheses.

Default: "::->"

-port portnumber

(Optional) Port on which to connect to the target system.

Default: SSH standard port 22.

-cmd cmd

(Optional) Specifies the command to execute on the remote Windows system. Unquoted argument tokens are automatically stringified. Prevent automatic quoting for expressions by placing expression code in parentheses. This parameter is only required when creating an SSH session on the fly using public key authentication.

Default: cmd

-session ssh

A CASSHELL object as returned by new-SSHSession. When passing an existing SSH session object, run-SSHCommand does not need to create an SSH session to execute the specified command. When executing multiple commands session negotiation and logon are only performed once typically resulting in improved performance over multiple session allocations. If this parameter is specified, values passed for host, user, pass, key, phrase, and port are ignored. This information is already specified when creating the session.

Default: null

Examples

Run a directory listing on a remote node using an SSH session explicitly specifying user name and password:

s=run-SSHCommand -host srv14 -user bob -pass xyz42 -prompt :> -command ls

Run a directory listing on a remote node using an SSH session with user name and password used for AutoShell login:

s = run-SSHCommand -host srv14 -prompt ":>" -command "ls"

Execute two commands in an explicitly allocated SSH session:

ssh=new-SSHSession -host client8 -user bob -pass xyz42 -prompt ::->
? run-SSHCommand -session ssh -command "! ver"
? run-SSHCommand -session ssh -command "ls *.* -l"
ssh.disconnect();