2016-07-27 52 views
0

我將開發機器升級到了Powershell 5,現在在PS2中工作的代碼不在PS5中。在PowerShell中使用Robocopy 5

我的代碼使用Robocopy將文件從svn checkout目錄複製到工作目錄。它也被用來清理工作目錄。

原來的代碼如下所示:

$what = @("""/copy:DAT""","""/E""","/XD .svn","/XD Debug","/XF .pdb") 
    $options = @("""/R:4""","""/W:5""","""/TS""","""/NS""","""/Tee""","""/NP""") 
    if ($file) { 
     $cmdArgs = @("""$src""", """$dest""", """$file""", $what, $options) 
    }else 
    { 
     $cmdArgs = @("""$src""", """$dest""", $what, $options) 
    } 
    robocopy $cmdArgs 

當命令運行使用PowerShell 5,我得到

ERROR : Invalid Parameter #3 : "/copy:DAT /E /XD .svn /XD Debug /XF .pdb"

我看到什麼正在發生,這是治療的參數作爲一個單一的命令,而比單獨的參數。 我試過單雙引號,我試過單引號,但我仍然得到相同的結果。

如果我硬編碼的選項來執行線,

robocopy $cmdArgs /COPY:DAT /E /XD .svn /XD Debug /XF .pdb /R:4 /W:5 /TS /NS /Tee /NP

則命令將工作,所以我有一個解決方法現在,但還是寧願有原來的參數結構。

我需要做些什麼才能讓PS5工作?謝謝。

編輯:在Windows 10和PS5,我收到以下錯誤: 錯誤:無效的參數#5: 「/ XD .svn文件」

$what = @("""/PURGE""","""/E""","/XD .svn","/XD Debug","/XF .pdb") 
$options = @("""/R:4""","""/W:5""","""/TS""","""/NS""","""/Tee""","""/NP""") 
$cmdArgs = @("""$srcBase""","""$targetPath"""; $what) 

對於 '/ XD .svn文件' 我有嘗試用三重引號,單引號,轉義引號等方式附上它。但是,沒有成功。

+0

',$ what,$ options' - >'; $什麼; $ options' – PetSerAl

+0

謝謝你的建議,但是這個失敗,同樣的問題。有人可以幫我一個忙,並嘗試與PowerShell 5相同的方案嗎?我意識到這是一個鋸齒狀的數組,我懷疑這是PowerShell變得困惑的地方。不過,我需要一種在PowerShell中執行robocopy的方法。 –

回答

0

哇!多麼痛苦,但我有一個解決方案。 我不得不使用字符串拆分方法並在空間上拆分。我不得不拋出數組並創建一個單一的字符串。 PS 2.0和更高版本之間

$cmdArgs = @("""$srcBase""", """$targetPath""") #still required for quoted paths 
$what = "/PURGE /E /XD .svn /XD DEBUG /XF .pdb" 
$options = "/R:4 /W:5 /TS /NS /Tee /NP" 
robocopy $cmdArgs $what.split(' ') $options.split(' ') 

東西改變了這一切打破了我的代碼,但是這似乎解決它。希望這可以幫助別人避免痛苦。 :)

編輯:經過對此的反思後,我想這是PS和Robocopy之間的一個問題,其中值的參數不能正確評估。讓我感到慚愧的是,我花了很多的嘗試來嘗試一個簡單的字符串作爲參數。

0

你正在構造一個鋸齒狀的數組。您可以通過查看$ cmdargs的結果運行包括你

"" 
"" 
"/copy:DAT" 
"/E" 
/XD .svn 
/XD Debug 
/XF .pdb 
"/R:4" 
"/W:5" 
"/TS" 
"/NS" 
"/Tee" 
"/NP" 

這看起來正確,代碼後驗證這一點,但如果你只是進入第三個元素$ cmdArgs [2]你可以看到,它涵蓋了整個的$內容是什麼陣列

"/copy:DAT" 
"/E" 
/XD .svn 
/XD Debug 
/XF .pdb 

作爲一個快速和骯髒的解決方法,請嘗試以下操作:

$what = @("""/copy:DAT""","""/E""","/XD .svn","/XD Debug","/XF .pdb") 
    $options = @("""/R:4""","""/W:5""","""/TS""","""/NS""","""/Tee""","""/NP""") 
    if ($file) { 
     $cmdArgs = @("""$src""", """$dest""", """$file""") 
     $cmdArgs += $what 
     $cmdArgs += $options 
    }else 
    { 
     $cmdArgs = @("""$src""", """$dest""") 
     $cmdArgs += $what 
     $cmdArgs += $options 
    } 
    robocopy $cmdArgs 

編輯: 輕微的修改之上, More on Jagged vs Multidimensional vs standard arrays here。朝底部看。本質上,你構建數組的方式被解釋爲@(0,1,(0,1,2,3,4),(0,1,2,3,4,5))

+0

對於應對延遲,我感到抱歉,這是緊急情況的暴政。 :)我在Windows 10 x86系統上,當然它使用PowerShell 5,我仍遇到同樣的問題。我無法正確構建可正確解析的命令行。當我回到PowerShell 2.0機器時,命令行可以完美解析。我嘗試了上面的建議,但得到了相同的結果。 –

0

太多令人難以置信的引號!除了源路徑和目標路徑外,你根本不需要它們。我發現的最佳方法是創建一個格式化的字符串表達式,然後執行它。

$what = "/copy:DAT /E /XD .svn /XD Debug /XF .pdb" 
$options = "/R:4 /W:5 /TS /NS /Tee /NP" 
$source = "C:\DataSource" 
$destination = "D:\DataDestination" 

$expression = [string]::Format('ROBOCOPY "{0}" "{1}" {2} {3}', $source,$Destination,$what,$options) 
$rc = Invoke-Expression $expression 
$rc = $LASTEXITCODE; 
    Switch ($rc) 
{ 
    0 {$msg = 'No files were copied. No failure was encountered. No files were mismatched.'} 
    1 {$msg = 'All files were copied successfully.'} 
    2 {$msg = 'There are some additional files in the destination directory that are not present in the source directory. No files were copied.'} 
    3 {$msg = 'Some files were copied. Additional files were present. No failure was encountered.'} 
    5 {$msg = 'Some files were copied. Some files were mismatched. No failure was encountered.'} 
    6 {$msg = 'Additional files and mismatched files exist. No files were copied and no failures were encountered. This means that the files already exist in the destination directory.'} 
    7 {$msg = 'Files were copied, a file mismatch was present, and additional files were present.'} 
    8 {$msg = 'Several files did not copy.'} 
    default {$msg = 'Robocopy failed with an error'} 
} 
$msg 
If($rc -ge 8) {Throw} 
相關問題