【问题标题】:Powershell Copy-item fails on unc path with space and ampersandPowershell Copy-item 在带有空格和 & 符号的 unc 路径上失败
【发布时间】:2020-10-20 13:06:51
【问题描述】:

我正在尝试使用 powershell Copy-Item 命令将文件复制到 UNC 路径。 不幸的是,此生产服务器上的 UNC 路径名称中同时包含空格和 & 符号。 我已经尝试了很多东西,但还没有任何运气。 您能否提出解决此问题的方法。

$InvokeExpressionPath = "\\servername\This Folder Has Spaces & Ampersand\Folder"
$TransfersSharePath = Invoke-Expression $InvokeExpressionPath
$TransfersSharePathFile = $InvokeExpressionPath + "\" + $FileName
Copy-Item -Path $CheckFileExists -Destination $TransfersSharePathFile -Force -ErrorAction SilentlyContinue

这是我收到的错误消息:

Invoke-Expression : At line:1 char:41
+ \\servername\This Folder Has Spaces & Ampersand\Folder
+                                     ~
The ampersand (&) character is not allowed. The & operator is reserved for future use; wrap an ampersand in double quotation marks ("&") to pass it as part of a string.
At E:\Scripts\CopyFile_Test.ps1:33 char:27
+     $TransfersSharePath = Invoke-Expression $InvokeExpressionPath
+                           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ParserError: (:) [Invoke-Expression], ParseException
    + FullyQualifiedErrorId : AmpersandNotAllowed,Microsoft.PowerShell.Commands.InvokeExpressionCommand

如果我尝试用双双引号 (""&"") 将 & 号括起来,我会在代码运行时收到此错误。

\\servername\This : The term '\\servername\This' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was 
included, verify that the path is correct and try again.
At line:1 char:1
+ \\servername\This Folder Has Spaces "&" Ampersand\Folder
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (\\servername\This:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException
 
DEBUG: This is the value of TransfersSharePathFile - \\servername\This Folder Has Spaces & Ampersand\Folder\file.zip
Copy-Item : Illegal characters in path.
At E:\Scripts\CopyFile_Test.ps1:36 char:5
+     Copy-Item -Path $CheckFileExists -Destination $TransfersSharePathFile -Force ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Copy-Item], ArgumentException
    + FullyQualifiedErrorId : System.ArgumentException,Microsoft.PowerShell.Commands.CopyItemCommand

提前感谢您的帮助。

【问题讨论】:

    标签: powershell unc copy-item


    【解决方案1】:

    非常感谢 AdamL 和 dread1 的帮助。我使用了这两个建议。 作为参考,这就是工作代码现在的样子。

    $InvokeExpressionPath = "\\servername\This Folder Has Spaces & Ampersand\Folder"
    $TransfersSharePathFile = Join-Path -Path $InvokeExpressionPath -ChildPath $FileName
    $TransfersSharePathFile = $InvokeExpressionPath + "\" + $FileName
    Copy-Item -Path $CheckFileExists -Destination $TransfersSharePathFile -Force -ErrorAction SilentlyContinue
    

    【讨论】:

      【解决方案2】:

      路径中的和号或空格不是问题。您正在尝试使用 Invoke-Expression 提供路径作为参数,这不是一个有效的命令。摆脱$TransfersSharePath 行并直接使用$InvokeExpressionPath 中的内容。只需将双引号更改为单引号以防万一。另外我建议您使用Join-Path 创建路径,而不是连接字符串。

      【讨论】:

        【解决方案3】:

        试试:

        $InvokeExpressionPath = '\\servername\This Folder Has Spaces & Ampersand\Folder'
        

        应该做的工作

        【讨论】:

        • 或者,如果您需要将 & 号传递给 Invoke-Expression,您可以尝试使用 $InvokeExpressionPath = """\\servername\This Folder Has Spaces & Ampersand\Folder""" 这将通过调用完成工作......但稍后您将不得不删除额外的 qoute.. .$TransfersSharePathFile = $($InvokeExpressionPath -replace """") + "\" +$FileName
        猜你喜欢
        • 1970-01-01
        • 2015-02-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多