将命令输出到PowerShell中的文本文件

时间:2011-04-01 08:40:31

标签: powershell

我正在尝试将以下命令输出到powershell中的文本文件,但我似乎无法使其正常工作:

ssh -v git@git.assembla.com  | Out-File C:\output.txt

2 个答案:

答案 0 :(得分:1)

正如使用原生应用的post below所述,您可以尝试使用Start-Process,例如

Start-Process ssh "-v git@git.assembla.com" -NoNewWindow -RedirectStandardOutput stdOut.log -RedirectStandardError stdErr.log; gc *.log; rm *.log

答案 1 :(得分:1)

处理同样的问题我在我的博客How to SSH from Powershell Using Putty\Plink上发了一篇详细信息,但简短的版本就是这段代码。但是一定要在安装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