2011-04-01 87 views

回答

1

與使用原生的post below規定應用程序,你可以嘗試使用Start-Process,例如

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

1

在我在我的博客How to SSH from Powershell Using Putty\Plink但短期的版本做了一個詳細的崗位同樣的問題,工作是這段代碼。但是,確保在安裝膩子後嘗試使用它。

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