Copy-Item不适用于路径变量中包含空格的目标

时间:2014-12-10 20:33:15

标签: powershell-v3.0

我正在尝试从PowerGUI编辑器运行以下powershell脚本

$windowsServicePath = (gwmi win32_service|?{$_.name -eq "Test Windows Service"}).pathname

$windowsServicePath = $windowsServicePath.Replace("\TestWindowService.exe","")

Write-Host $windowsServicePath

$source = "C:\\Temp\\Configs\\App.DEVINT.config"

Copy-Item -path "$source" -destination $windowsServicePath -recurse -verbose;

在使用PowerGUI运行上述powershell脚本时,我收到以下错误:

Copy-Item : Cannot find drive. A drive with the name '"C' does not exist.
At C:\Users\Test\Desktop\Powershell Details\Automation Scripts\Test-Powers.ps1:41 char:10
+ Copy-Item <<<<  -path "$source" -destination $windowsSerivcePath -recurse -verbose;
    + CategoryInfo          : ObjectNotFound: ("C:String) [Copy-Item], DriveNotFoundException
    + FullyQualifiedErrorId : DriveNotFound,Microsoft.PowerShell.Commands.CopyItemCommand

$windowsServicePath值为C:\Program Files (x86)\Default Company Name\TestWindowsService

但是,如果我使用以下代码更新Copy-Item命令,那么它适用于我

Copy-Item -path "$source" -destination "C:\Program Files (x86)\Default Company Name\TestWindowsService" -recurse -verbose;

有人可以帮我解决上述问题吗?

1 个答案:

答案 0 :(得分:1)

我认为$windowsServicePath值实际上是"C:\Program Files (x86)\Default Company Name\TestWindowsService"(带引号)。你可以删除它们并将其传递给Copy-Item