【发布时间】:2019-10-17 23:41:48
【问题描述】:
我有一个在 VSCode 中运行良好的 Powershell 脚本,但在 Powershell 提示中,我遇到了错误。下面是输出。
→ C:\WINDOWS\system32› powershell.exe -file 'D:\Source\Repos\Powershell Scripts\SD-Report-Archive.ps1' -sourcePath 'D:\Archives\' -targetPath 'D:\Archives2\'
D:\Archives\
Get-ChildItem : Cannot find path 'D:\A' because it does not exist.
At D:\Source\Repos\Powershell Scripts\SD-Report-Archive.ps1:25 char:14
+ $files = Get-ChildItem -Recurse -File -Path $sourcePath
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (D:\A:String) [Get-ChildItem], ItemNotFoundException
+ FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetChildItemCommand
正如您在输出的第 2 行所看到的,我对要发送的参数值进行了写入输出,它是正确的。当我执行 Get-ChildItem 时,它似乎将值截断为 'D:\A',我不知道为什么。
Param(
[Parameter(Mandatory = $true)]
[string]$sourcePath,
[Parameter(Mandatory = $true)]
[string]$targetPath
)
function Copy-FilesIntoFolders {
param()
Write-Output $sourcePath;
$files = Get-ChildItem -Path $sourcePath -Recurse -File
...
}
【问题讨论】:
-
如果您从路径中删除尾部反斜杠,您是否会遇到同样的问题?
-
@TheMadTechnician 我没有。非常感谢。我从没想过这会是问题所在。
-
我刚刚发现这篇文章似乎与将来访问此文章的任何人有关。这可能是发生这种行为的原因。 stackoverflow.com/questions/5079413/…。要考虑的关键语句是“当使用 -File 参数时,powershell.exe 似乎没有完全评估脚本参数。”
-
从显示的内容来看,它对我有用。也许最好显示整个脚本。
-
@Tim,链接的帖子不相关(这是一个单独的错误);您的问题仅与 PowerShell 损坏的幕后重新引用有关。