使用Get-ChildItem访问IP共享文件夹时路径不存在的问题求助
问题描述
我编写了一段PowerShell脚本,用于将网络计算机中的文件及子项复制到共享驱动器,原脚本运行完全正常:
#(ComputerName) 为安全起见,非真实名称。 #(ParentFolderName) 为安全起见,非真实名称。 #(IPAddress) 为安全起见,非真实名称。 $Source = "\\(ComputerName)\measurements\\" $Destination = "G:\Shared drives\(ParentFolderName)\TSTATION-BCKUP\K4\KNA01788\\" Get-ChildItem $Source -Recurse | ForEach { $ModifiedDestination = $($_.FullName).Replace("$Source","$Destination") If ((Test-Path $ModifiedDestination) -eq $False) { Copy-Item $_.FullName $ModifiedDestination } }
但将$Source替换为IP地址路径后:
$Source = "\\(IPAddress)\data\measurements\\"
出现以下错误:
Get-ChildItem : Cannot find path '\\10.1.x.x\data\m' because it does not exist. At C:\Users\us1adavila\Documents\Scripts\Batchs\TStation Backup\TStationBackup - US250216.ps1:4 char:1 + Get-ChildItem "\\10.1.82.112\data\measurements\\" -Recurse | ForEach { + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : ObjectNotFound: (\\10.1.82.112\data\m:String) [Get-ChildItem], ItemNotFoundException + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetChildItemCommand
我能正常ping通该IP地址,尝试添加-LiteralPath参数也无法解决问题,求解决建议。
解决建议
- 修正路径字符串解析问题:PowerShell中双引号包裹的字符串会尝试解析转义字符,
\m被错误解析导致路径截断为\\10.1.x.x\data\m。解决方式二选一:# 方式1:用单引号包裹路径(单引号不解析转义字符) $Source = '\\(IPAddress)\data\measurements\\' # 方式2:将路径中的反斜杠替换为双反斜杠 $Source = "\\(IPAddress)\\data\\measurements\\" - 核实共享权限与访问权限:能ping通IP不代表拥有共享目录的访问权限。手动在文件资源管理器中输入
\\(IPAddress)\data,确认该共享存在且当前账号有读取权限。 - 确认共享名称准确性:检查目标机器的共享配置,确认共享名称确实是
data,而非带隐藏标记(名称末尾加$)或其他拼写错误的名称。 - 尝试映射网络驱动器操作:先将目标共享映射为本地驱动器号,再基于映射路径操作,避免UNC路径解析问题:
# 映射共享为Z盘 New-PSDrive -Name Z -PSProvider FileSystem -Root "\\(IPAddress)\data" $Source = "Z:\measurements\" # 后续复制逻辑不变,完成后可移除映射 # Remove-PSDrive Z
内容的提问来源于stack exchange,提问作者AaronD
相关产品推荐
相关产品推荐

