首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Powershell Copy-item在unc路径上失败,路径为空格和与号

Powershell Copy-item在unc路径上失败,路径为空格和与号
EN

Stack Overflow用户
提问于 2020-06-30 17:02:02
回答 3查看 757关注 0票数 1

我正在尝试使用powershell Copy-Item命令将文件复制到UNC路径。遗憾的是,此生产服务器上的UNC路径名称中包含空格和与号。我试过很多方法,但都还没成功。你能建议一下解决这个问题的方法吗?

代码语言:javascript
运行
复制
$InvokeExpressionPath = "\\servername\This Folder Has Spaces & Ampersand\Folder"
$TransfersSharePath = Invoke-Expression $InvokeExpressionPath
$TransfersSharePathFile = $InvokeExpressionPath + "\" + $FileName
Copy-Item -Path $CheckFileExists -Destination $TransfersSharePathFile -Force -ErrorAction SilentlyContinue

这是我得到的错误消息:

代码语言:javascript
运行
复制
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

如果我试图用双引号(""&"")把“”和“”括起来,代码运行时就会出现这个错误。

代码语言:javascript
运行
复制
\\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

提前感谢您的帮助。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2020-06-30 17:17:43

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

票数 0
EN

Stack Overflow用户

发布于 2020-07-01 12:46:48

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

代码语言:javascript
运行
复制
$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
票数 1
EN

Stack Overflow用户

发布于 2020-06-30 17:12:52

尝试:

代码语言:javascript
运行
复制
$InvokeExpressionPath = '\\servername\This Folder Has Spaces & Ampersand\Folder'

应该能胜任这项工作

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62653883

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档