2011-01-12 64 views
3

我正在嘗試使一個簡單的PowerShell函數具有Linux風格的ssh命令。如:Powershell中的流水線字符串

ssh [email protected]

我使用砰砰要做到這一點,這是我寫的函數:

function ssh { 
    param($usernameAndServer) 

    $myArray = $usernameAndServer.Split("@") 

    $myArray[0] | C:\plink.exe -ssh $myArray[1] 
} 

如果用戶輸入正確,$ myarray的[0]用戶名和$ myArray [1]是URL。因此,它連接到URL,當系統提示您輸入用戶名時,用戶名將使用流水線進行流式傳輸。一切工作都很完美,除了管道持續提供用戶名($ myArray [0])並且它一次又一次地作爲密碼輸入。示例:

PS C:\Users\Mike> ssh [email protected] 
login as: [email protected]'s password: 
Access denied 
[email protected]'s password: 
Access denied 
[email protected]'s password: 
Access denied 
[email protected]'s password: 
Access denied 
[email protected]'s password: 
Access denied 
[email protected]'s password: 
FATAL ERROR: Server sent disconnect message 
type 2 (protocol error): 
"Too many authentication failures for xxxxx" 

其中用戶名已被xxxxx替換且URL已被替換爲yyyyy。

基本上,我需要找出如何在用戶名($ myArray [0])中輸入一次之後,從管道中停止腳本。

任何想法?我已經瀏覽了整個互聯網尋找解決方案,並沒有發現任何東西。

回答

2

不允許plink允許你在一個參數中指定用戶和主機?那就是:

砰砰-ssh用戶@主機

,如果讓你的SSH功能可以削減到:

function ssh { 
    param($usernameAndServer) 

    C:\plink.exe -ssh $usernameAndServer 
} 
+0

衛生署,工作完美,謝謝! – 2011-01-12 21:56:05