Invoke Remote SSH Command From PowerShell

I find that running scripts against both Windows and Linux systems together can be more efficient than running separate scripts.  This can be accomplished quite easily with the plink.exe binary from the PuTTY installation package.

The following script is designed to work with the default installation path of plink.exe (“C:\Program Files (x86)\PuTTY\plink.exe”), but you can specify an alternate path for the executable if you wish.  You need to specify a “ComputerName” as well as the remote SSH “Command” you wish to run.  If you are going to bulk run against many systems, you can pass the “UserName”, “SecurePassword”, and even “AutoAcceptKeys” to ignore the SSH key warning.

Here I’m grabbing some uname information from a remote Linux system


Here I’m getting the RedHat release info. This time I’m passing the “UserName” and “SecurePassword” so I don’t get prompted


Function Code:


function Invoke-SSHCommand {
Invoke SSH Command on a Remote Computer
Invoke-SSHCommand uses PuTTY's plink.exe to execute a SSH command on a remote system
Invoke-SSHCommand -Command "uname -a" -ComputerName "myComputer"

Run uname against the remote computer

$user = "testuser"
$userpw = Read-Host -Prompt "Enter Password" -AsSecureString
Invoke-SSHCommand -Command "touch /tmp/PowerShellHasBeenHere" -ComputerName "myComputer" -UserName $user -SecurePassword $userpw

Creates a blank file in /tmp on the remote computer.  This time we are passing the user name and password
as parameters

    [CmdletBinding()] param (
        [Parameter(Mandatory=$true)] [String]$ComputerName,
        [Parameter(Mandatory=$true)] [String]$Command,
        [Parameter(Mandatory=$false)] [String]$PathToPlink="C:\Program Files (x86)\PuTTY\plink.exe",
        [Parameter(Mandatory=$false)] [String]$UserName,
        [Parameter(Mandatory=$false)] [Security.SecureString]$SecurePassword,
        [Parameter(Mandatory=$false)] [Switch]$AutoAcceptKeys

    process {
        #check for plink.exe file
        Write-Host ""
        if(! (Test-Path $PathToPlink) ) {
            Write-Host "ERROR: $PathToPlink not found" -ForegroundColor Magenta
        #get username if not passed
        if(!$UserName) {
            $UserName = Read-Host -Prompt "Enter User Name"
        #get password if not passed
        if(!$SecurePassword) {
            $SecurePassword = Read-Host -Prompt "Enter Password" -AsSecureString
        Write-Host ""
        #convert from secure pw for command execution
        $pw = [Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR($SecurePassword))
        #formulate plink command
        if($AutoAcceptKeys) {
            $plinkCommand = "`"echo yes | `"$PathToPlink`" -ssh -l $UserName -pw $pw $ComputerName `"$Command`"`""
        else {
            $plinkCommand = "`"`"$PathToPlink`" -ssh -l $UserName -pw $pw $ComputerName `"$Command`"`""
        #run plink command
        $result = cmd /c $plinkCommand
        return $result

Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>