在这种情况下,您必须使用带单引号的双反引号来转义括号。当您使用双引号字符串时,还可以使用四重反引号。
所以固定的代码行是:
Copy-item -Path $FSG\$SW\0.RoomView.Notes\starter\'``[RoomView``] Versions explained*.pdf' -Destination $FSG\$containerFolder\$rootFolder\'Fusion Server'\
关于文件路径和连线字符等的另一个好资源是阅读这篇文章:从字面上理解事物(例如文件路径) http://technet.microsoft.com/en-us/library/ff730956.aspx
EDIT
Thanks to @mklement0 for highlighting that the true cause of this
inconsistency is because of a bug currently in
PowerShell https://github.com/PowerShell/PowerShell/issues/79991 https://github.com/PowerShell/PowerShell/issues/7999.
This bug causes escaping of wildcard characters, as well as backticks
with the default -Path
parameter to behave differently than other
parameters e.g. the -Include
and -Filter
parameters.
To expand on @mklement0's excellent answer https://stackoverflow.com/a/57778204/2150063, and comments https://stackoverflow.com/questions/21008180/copy-file-with-square-brackets-in-the-filename-and-use-wildcard/21009701?noredirect=1#comment102143583_21009701, and other answers below:
为了更好地理解为什么我们需要单引号 and 两个回勾 in this情况; (and以突出bug https://github.com/PowerShell/PowerShell/issues/7999和不一致)让我们通过一些示例来演示发生了什么:
Get-Item
和关联的 cmdlet (Get-ChildItem
, Copy-Item
等),处理-Path
处理组合时参数不同转义通配符 and 未转义的通配符*同时***!
TLDR:我们需要单引号和双反引号组合的根本原因是底层 PowerShell 提供程序如何解析-Path
通配符的参数字符串。它似乎解析了一次转义字符,第二次解析了通配符。
让我们通过一些例子来证明这个奇怪的结果:
首先,让我们创建两个文件来测试调用File[1]a.txt
and File[1]b.txt
"MyFile" | Set-Content '.\File`[1`]a.txt'
"MyFriend" | Set-Content '.\File`[1`]b.txt'
我们将尝试不同的方式来获取该文件。我们知道方括号[ ]
are 通配符 https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_wildcards?view=powershell-6,所以我们需要用反引号字符 https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_quoting_rules?view=powershell-6#single-and-double-quoted-strings.
我们将尽力获得one明确文件。
让我们从使用单引号文字字符串开始:
PS C:\> Get-Item 'File[1]a.txt'
PS C:\> Get-Item 'File`[1`]a.txt'
Directory: C:\
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 2019-09-06 5:42 PM 8 File[1]a.txt
PS C:\> Get-Item 'File``[1``]a.txt'
Directory: C:\
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 2019-09-06 5:42 PM 8 File[1]a.txt
对于单引号字符串,检索文件只需要一个反引号,但两个反引号也可以。
使用双引号字符串我们得到:
PS C:\> Get-Item "File[1]a.txt"
PS C:\> Get-Item "File`[1`]a.txt"
PS C:\> Get-Item "File``[1``]a.txt"
Directory: C:\
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 2019-09-06 5:42 PM 8 File[1]a.txt
对于双引号字符串,正如预期的那样,我们可以看到我们需要两个反引号才能使其工作。
现在,我们想要检索both files and使用通配符。
让我们从单引号开始:
PS C:\> Get-Item 'File[1]*.txt'
PS C:\> Get-Item 'File`[1`]*.txt'
PS C:\> Get-Item 'File``[1``]*.txt'
Directory: C:\
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 2019-09-06 5:42 PM 8 File[1]a.txt
-a---- 2019-09-06 5:49 PM 10 File[1]b.txt
使用单引号,当我们有通配符时,我们需要two反引号组。一个用于转义括号,第二个反引号用于转义我们在评估通配符时用于转义括号的反引号。
同样对于双引号:
PS C:\> Get-Item "File[1]*.txt"
PS C:\> Get-Item "File`[1`]*.txt"
PS C:\> Get-Item "File``[1``]*.txt"
PS C:\> Get-Item "File```[1```]*.txt"
PS C:\> Get-Item "File````[1````]*.txt"
Directory: C:\
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 2019-09-06 5:42 PM 8 File[1]a.txt
-a---- 2019-09-06 5:49 PM 10 File[1]b.txt
使用双引号时,使用通配符进行计算会更加冗长。在这种情况下,我们需要four反刻度集。对于双引号,我们需要两个反引号来转义括号,并且在评估星号通配符时还需要另外两个反引号来转义转义字符。
EDIT
As @mklement0 提及 https://stackoverflow.com/a/54108210/2150063,这种行为与-Path
参数不一致,并且行为与-Include
参数,其中只需要一个反引号即可正确转义括号。这可能会在更高版本的 PowerShell 中“修复”。
1 https://github.com/PowerShell/PowerShell/issues/7999 As of Windows PowerShell v5.1 / PowerShell Core 6.2.0-preview.3