在PowerShell中使用数组或变量来构建Robocopy的参数(包括 /XF)

编程语言 2026-07-09

我正在编写一个PowerShell脚本,最终要通过RoboCopy传入各种参数。我想能够把要传给RoboCopy的参数拆分开来,以便更容易管理和更新这些参数。相关的脚本部分是:

基本的RoboCopy参数数组

$robocopyBaseArguments = @(
    '/S',                   # Copy subdirectories, exclude empty subdirectories
    '/ZB',                  # Copies files in restartable mode, switches to backup mode if access denied
    '/COPY:DT',             # Copies Data and Time stamp file properties
    '/XA:SHT',              # Excludes System, Hidden, and Temp files
    '/W:1',                 # Wait 1 second between retries
    '/R:2'                  # Retry 2 times
)

把基本参数与源地址和目标地址组装在一起

$robocopyArguments = @($directorySource, $directoryTarget, $robocopyBaseArguments) + "/LOG+:$LogPath"

RoboCopy实际执行的那一行

Robocopy.exe @robocopyArguments

这一切都能正常工作,但我想能够添加 /XF *.lnk,以便更容易添加更多被排除的文件类型。我已经尝试了下面的各种方法,但都导致错误。

失败的方法1:字符串变量

$robocopyExcludedExtensions = '/XF "*.lnk"'

添加到参数数组中:

$robocopyArguments = @($directorySource, $directoryTarget, $robocopyBaseArguments, $robocopyExcludedExtensions) + "/LOG+:$LogPath"

脚本的错误信息为:ERROR : Invalid Parameter #9 : "/XF "*.lnk""

失败的方法2:数组

$robocopyExcludedExtensions = @(
    '*.lnk'
)

添加到参数数组中:

$robocopyArguments = @($directorySource, $directoryTarget, $robocopyBaseArguments) + "/XF $robocopyExcludedExtensions" + "/LOG+:$LogPath"

得到的错误是 ERROR : Invalid Parameter #9 : "/xf *.lnk"

不使用变量/数组也能工作
不通过这些变量和数组来构建参数,直接运行RoboCopy也能正常工作:Robocopy.exe $directorySource $directoryTarget /E /ZB /COPY:DT /XA:SHT /XF *.lnk /W:1 /R:2 /LOG+:$LogPath

问题
我想能够对 $robocopyExcludedExtensions 使用一个数组(参见失败方法2),因为我相信这对这个脚本的长期使用和灵活性最有帮助,但如果做不到我也能理解。如何在PowerShell中使用变量和数组来排除RoboCopy中的目标文件扩展名?

更新1:把 , 改为 ;

基于Santiago的建议,将每个 , 都改成一个 ;

我按指示更新了 $robocopyBaseArguments 数组,以及如下的“失败方法2”组装行:

$robocopyArguments = @($directorySource; $directoryTarget; $robocopyBaseArguments) + "/XF $robocopyExcludedExtensions" + "/LOG+:$LogPath"

然后我又修改了 $robocopyBaseArguments 数组,按如下将 /XF 项加入其中:

$robocopyBaseArguments = @(
    '/S';                   # Copy subdirectories, exclude empty subdirectories
    '/ZB';                  # Copies files in restartable mode, switches to backup mode if access denied
    '/COPY:DT';             # Copies Data and Time stamp file properties
    '/XA:SHT';              # Excludes System, Hidden, and Temp files
    '/W:1';                 # Wait 1 second between retries
    '/R:2';                  # Retry 2 times
    '/XF *.lnk'
)

并且完全从 $robocopyArguments行中移除了 /XF... 这段。

在两种情况下我都得到相同的错误:ERROR : Invalid Parameter #9 : "/XF *.lnk"

解决方案

主要问题是 '/XF "*.lnk"';那需要是两个独立的参数:@('/XF', '*.lnk')

你几乎已经掌握了 [splatting] 的用法;但没有必要把数组合并在一起。
你可以使用多个展开数组,并且可以把它们与常规命令行选项混合使用,例如如下所示:

$directorySource = 'C:\Files'
$directoryTarget = 'C:\Temp\Files'
$LogPath = 'C:\Temp\robocopy.log'

$robocopyBaseArguments = @(
    '/S',                   # Copy subdirectories, exclude empty subdirectories
    '/ZB',                  # Copies files in restartable mode, switches to backup mode if access denied
    '/COPY:DT',             # Copies Data and Time stamp file properties
    '/XA:SHT',              # Excludes System, Hidden, and Temp files
    '/W:1',                 # Wait 1 second between retries
    '/R:2'                  # Retry 2 times
    '/NP'                   # No Progress; doesn't play well with the log file
)

$robocopyExcludedExtensions = @(
    '/XF'
    '*.lnk'
)

# /L: Log only (test mode) instead of actually copying while still testing
# /TEE: Log and write to console while testing
& robocopy.exe $directorySource $directoryTarget /L /TEE @robocopyBaseArguments @robocopyExcludedExtensions /LOG+:$LogPath
站内所有文章版权归属LeftHeroAI导航站,无授权禁止任何主体转载、抄袭、复制内容,亦不得私自架设镜像站点。一经侵权,本站将通过法律途径追责。

相关文章