NetCmdlets: Remote Execution

There are a variety of ways to execute commands remotely in PowerShell and NetCmdlets supports most of them. With NetCmdlets you can execute commands remotely using SSH, Rexec, RShell, Telnet, and more!

Technology Cmdlet
SSH (Sexec) Invoke-SSH
Rexec Invoke-Rexec
Rshell Invoke-Rshell
Telnet Invoke-Telnet

Below you will find details and examples using the cmdlets that implement these technologies.

Invoke-SSH

For secure shell connections, NetCmdlets provides the Invoke-SSH cmdlet.
Below is an example of connecting to a remote SSH server to execute the “ls” command.

PS> invoke-ssh -server myserver -user myuser -password mypassword -command ls
                                                         
Do you want to trust the certificate presented?
The server has presented the key below.        
Fingerprint: 59:52:C8:DB:C8:3A:FE:CF:9D:02:E3:31:3A:2C:11:E4
[Y] Yes  [N] No  [S] Suspend  [?] Help (default is "Y"): y   
                                       
Text                                                                      EOL 
----                                                                      --- 
AssemblyInfo.vb                                                          True 
Documents                                                                True 
EmptyFolder                                                              True 
Lance                                                                    True 
test.exe                                                                 True 
test.ps1                                                                 True 
test.txt                                                                 True 
v6tests                                                                  True 
v8tests                                                                  True

The cmdlet will prompt you to accept the server’s host key as above by default.
To automatically accept the server’s host key without user interaction, set the SSHAccept parameter. For instance:

PS> invoke-ssh -server myserver -user myuser -password mypass 
-command ls -sshaccept 59:52:C8:DB:C8:3A:FE:CF:9D:02:E3:31:3A:2C:11:E4    

Text                                                                      EOL 
----                                                                      --- 
AssemblyInfo.vb                                                          True 
Documents                                                                True 
EmptyFolder                                                              True 
Lance                                                                    True 
test.exe                                                                 True 
test.ps1                                                                 True 
test.txt                                                                 True 
v6tests                                                                  True 
v8tests                                                                  True

For more information on options for accepting the server’s host, key please see the help for the SSHAccept parameter.

Invoke-Rexec and Invoke-Rshell

Rexec and Rshell (rsh.exe) already exist as command line applications, but until
now they did not return useful, easily scriptable objects in PowerShell.

Below is an example of using Invoke-Rexec to issue a directory listing command to
a remote rexec host:

PS> invoke-rexec -server server -user user -password pass 
-command "C:\WINDOWS\system32\cmd exe /c dir c:\"
                                                                            
Text                                                                       EOL
----                                                                       --- 
 Volume in drive C has no label....                                       True 
 Volume Serial Number is 6C32-6256...                                     True 
...                                                                       True 
 Directory of c:\...                                                      True 
...                                                                       True 
12/13/2006  05:41 PM    <DIR>          Documents and Set...               True 
12/17/2006  12:04 PM    <DIR>          Inetpub...                         True 
01/09/2007  03:06 PM    <DIR>          Program Files...                   True 
12/17/2006  12:54 PM    <DIR>          share...                           True 
12/17/2006  12:29 PM    <DIR>          Sun...                             True 
01/21/2007  02:10 PM    <DIR>          TFTP-Root...                       True 
01/09/2007  03:08 PM    <DIR>          Virtual Machines...                True 
01/17/2007  03:02 PM    <DIR>          WINDOWS...                         True 
               7 File(s)        851,124 bytes...                          True 
              13 Dir(s)  15,440,244,736 bytes free...                     True 
              13 Dir(s)  15,440,244,736 bytes free...                     True 
                                                                       
                                                                     
PS>

Similarly, the Invoke-Rshell cmdlet gives the same output:

PS> invoke-rexec -server server -user user -password pass
-command "C:\WINDOWS\system32\cmd exe /c dir c:\"

Invoke-Telnet

The Invoke-Telnet cmdlet may be used to execute a command via a telnet session. The cmdlet will connect, authenticate, wait for the specified Shell prompt to be returned by the server, and then execute the command. For instance:

PS> invoke-telnet -user test -password test -server MyServer 
-command ls -shellprompt bash-2.05a$

It is necessary to supply the ShellPrompt parameter so that the cmdlet can send the command at the appropriate time, as this is not an interactive session.

We appreciate your feedback.  If you have any questions, comments, or suggestions about this article please contact our support team at kb@nsoftware.com.