"Get ChildItem : Cannot find path"显示我没有提供的路径



我继承了一个脚本,该脚本应该只是将文件从-source移动到-target。我被提示两者,在提供路径后,它告诉我,它在显示我完全没有提交的路径时找不到路径,但无法弄清楚它是如何到达那里的。

[Parameter(
Mandatory = $true,
Position = 0,
HelpMessage = "Root of the folders or share to archive"
)]
[String] $source,

[Parameter(
Mandatory = $true,
Position = 1,
HelpMessage = "Path of the folder or share of archive"
)]
[string] $target,

[Parameter(
Mandatory = $false,
Position = 3
)]
[int] $days = 30
)

# Get all the files from the source path, that are not shortcuts (*.lnk) and older than the days set
Get-ChildItem $source -Recurse |  
Where-Object {!$_.psiscontainer -and ((get-date) - $_.lastwritetime).totaldays -gt $days -and $_.extension -ne ".lnk"} |
ForEach-Object {
# For each file build the destination path 
$dest = $_.fullname -replace ([regex]::escape($source)), $target

# Move the files into the destination
Move-Item -Path $_.fullname -Destination $dest -ErrorAction silentlycontinue
}

日志上写着";找不到路径"\\appserver\abc$\Automation\Daily\Archive\appserver\abc$\Storage",因为它不存在"-看看它是如何开始重复的\\appserver\abc$\Automation\Daily\Archive\是脚本的位置,而\\appserver\abc$\Storage\是我输入的-source。所以我不知道为什么它要查看脚本的路径,然后同时附加源路径。

编辑:这就是我如何调用脚本(来自一个鲜为人知的名为APX的金融应用程序(:

SHELL PowerShell \appserverabc$AutomationDailyArchiveArchiveFiles.ps1 -source \appserverabc$Dataportdpdata -target \appserverabc$Dataportarchived -days 30

当脚本启动时,它在运行它的目录中开始,因此它已经在\appserverabc$AutomationDailyArchive中,如果您没有提供诸如\A:之类的UNC资源前缀,那么它将从该目录向下查找ChildItems。因此,当您提供文件夹路径时,它会将其附加到当前目录中,但无法找到新路径。

由于只有当您在字符串开头省略了\时才会发生这种情况,因此只有当您将appserverabc$Storage作为源提交时,我才会期望您的输出。如果您确信您确实提供了\,那么请更仔细地查看将命令传递给此脚本的任何一行脚本,看看它是否有提前剥离\的原因。

最新更新