我尝试在 PowerShell 脚本中使用 robocopy,但目前失败了。当我手动运行以下命令时
robocopy \\corporatesystem00.local.com\C$\Maven\ \\corporatesystem01.local.com\C$\Maven\ * /e /purge
我能够将一个文件夹的所有内容复制到另一个文件夹,效果就像变魔术一样。但是,考虑到 PowerShell 的“神奇”,我想我遇到了一个未知的问题。当我从 PowerShell 脚本运行上述相同的命令时,我收到此错误:
PS C:\Program_Files> .\jenkins-maven-copy.ps1
Performing Jenkins Maven Sync
robocopy \\corporatesystem00.local.com\C$\Maven\ \\corporatesystem01.local.com\C$\Maven\ * /e /purge
-------------------------------------------------------------------------------
ROBOCOPY :: Robust File Copy for Windows
-------------------------------------------------------------------------------
Started : Wednesday, July 30, 2014 3:00:03 PM
Source - \\corporatesystem00.local.com\C$\Maven\
Dest - \\corporatesystem01.local.com\C$\Maven\
Files : *
Options : /DCOPY:DA /COPY:DAT /R:1000000 /W:30
------------------------------------------------------------------------------
ERROR : Invalid Parameter #4 : "/e /purge"
Simple Usage :: ROBOCOPY source destination /MIR
source :: Source Directory (drive:\path or \\server\share\path).
destination :: Destination Dir (drive:\path or \\server\share\path).
/MIR :: Mirror a complete directory tree.
For more usage information run ROBOCOPY /?
**** /MIR can DELETE files as well as copy them !
在我为脚本中的选项输入任何命令行选项时,都会发生这种情况。我正在运行的脚本如下。
Param (
[string]$srcLocation = '\\corporatesystem00.local.com\C$\Maven\',
[string]$destLocation = '\\corporatesystem01.local.com\C$\Maven\',
[string]$files = '*',
[string]$options = '/e /purge'
)
function syncMaven( $srcLocation, $destLocation, $files, $options )
{
Write-Host "Performing Jenkins Maven Sync"
Write-Host "robocopy $srcLocation $destLocation $files $options"
robocopy $srcLocation $destLocation $files $options
}
syncMaven $srcLocation $destLocation $files $options
如果您查看之前的错误/输出,它表明该命令正在通过命令选项传递,但 robocopy 无法识别它们或其他东西。
PS C:\Program_Files> .\jenkins-maven-copy.ps1
Performing Jenkins Maven Sync
robocopy \\corporatesystem00.local.com\C$\Maven\ \\corporatesystem01.local.com\C$\Maven\ * /e /purge
所以,我很茫然。
答案1
通过在 PowerShell 中尝试以下命令,您很可能可以重现您的问题:
robocopy a b * "/e /purge"
如果这是真的,那么参数扩展就是导致问题的原因。要解决这个问题,你可以采取以下方法:
iex "$srcLocation $destLocation $files $options"