1

I am trying to output the following command to a text file in powershell, but I cannot seem to get it working:

ssh -v [email protected]  | Out-File C:\output.txt
2
  • Is your command generating any output? Commented Apr 1, 2011 at 15:52
  • Yes, we need some more information to help you with this issue. Commented Apr 3, 2011 at 3:42

2 Answers 2

3

As stated in the post below with using native apps, you could try using Start-Process, e.g.

Start-Process ssh "-v [email protected]" -NoNewWindow -RedirectStandardOutput stdOut.log -RedirectStandardError stdErr.log; gc *.log; rm *.log

Sign up to request clarification or add additional context in comments.

Comments

2

Working on the same problem I made a detail post on my blog How to SSH from Powershell Using Putty\Plink but the short version is this bit of code. But sure you try it after installing putty.

   Function Invoke-SSHCommands {

       Param($Hostname,$Username,$Password, $CommandArray, $PlinkAndPath, $ConnectOnceToAcceptHostKey = $true)

       $Target = $Username + '@' + $Hostname
       $plinkoptions = "-ssh $Target -pw $Password"

       #Build ssh Commands
       $remoteCommand = ""
       $CommandArray | % {$remoteCommand += [string]::Format('{0}; ', $_) }

       #plist prompts to accept client host key. This section will login and accept the host key then logout.
       if($ConnectOnceToAcceptHostKey)
       {
            $PlinkCommand  = [string]::Format('echo y | & "{0}" {1} exit', $PlinkAndPath, $plinkoptions ) 
            #Write-Host $PlinkCommand
            $msg = Invoke-Expression $PlinkCommand
       }

       #format plist command
       $PlinkCommand = [string]::Format('& "{0}" {1} "{2}"', $PlinkAndPath, $plinkoptions , $remoteCommand)


       #ready to run the following command
       #Write-Host $PlinkCommand
       $msg = Invoke-Expression $PlinkCommand
       $msg
    }

    $PlinkAndPath = "C:\Program Files (x86)\PuTTY\plink.exe"
    $Username = "remoteshell"
    $Password = "pa$$w0rd"
    $Hostname = "Linuxhost"

    $Commands = @()
    $Commands += "ls"
    $Commands += "whoami"

    Invoke-SSHCommands -User $Username -Hostname $Hostname -Password $Password -PlinkAndPath $PlinkAndPath -CommandArray $Commands

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.