2012-03-21 32 views
1

我需要寫cmd命令DIR的輸出的powershell變量(數組)。可能嗎?我需要它,因爲我使用WinSCP賦予CMD腳本(SFTP客戶端),當我連接到SFTP服務器,我可以用CMD命令只,而不是PowerShell的。但我需要獲取遠程目錄中文件的名稱和大小,以檢查傳輸是否成功。這是我的腳本,它連接到SFTP和上傳一些文件:cmd以連接的powershell

C:\"Program Files"\WinSCP\winscp.com /console /command "option batch abort" "option confirm off" "open sftp://login:[email protected]" "cd import" "option transfer binary" "put C:\_ZbankyNaOdoslanie\*.gpg" "put C:\_ZbankyNaOdoslanie\*.chk" "close" "exit" 

謝謝。

回答

3

您可以在PowerShell中使用電話運營商&調用此行,並指定它的標準輸出和/或錯誤輸出到一個變量。

實施例:

$output = & "C:\Program Files\WinSCP\winscp.com" /arg1 /arg2 

可變$output將是一個字符串數組。每行輸出將是一個數組元素。

更多的例子:

  • 只是標準輸出:$output = & "C:\Program Files\WinSCP\winscp.com" /arg1 /arg2 2>$null
  • 只是標準錯誤:$output = & "C:\Program Files\WinSCP\winscp.com" /arg1 /arg2 1>$null
  • 兩種:$output = & "C:\Program Files\WinSCP\winscp.com" /arg1 /arg2 2>&1
+0

謝謝你,安迪。我將WinSCP重新安裝到c:\ WinSCP以避免文件夾名稱中出現空白,但它尖叫着:術語'C:\ WinSCP \ winscp.com/console/command'不會被識別爲cmdlet,函數的nam e,腳本文件或可操作程序。檢查拼寫Ø F中的名稱,或是否包含一個路徑,驗證路徑是否正確,然後再次嘗試 。 – culter 2012-03-21 10:46:17

+0

@culter權利,請確保您有您的交易報價和參數的WinSCP這樣一個空格:'&「C:\路徑爲\ winscp.com」 /控制檯/命令「選項一批中止」 ...' – 2012-03-21 10:49:11

+0

安迪,現在沒有空間了。我將它重新安裝到C:\ WinSCP目錄。 – culter 2012-03-21 10:55:13