我正在编写一个批处理文件,该文件执行一个PowerShell脚本,在其中使用UNC路径作为属性循环项,并在这些路径上使用Get-ChildItem
。 在我的脚本中,以下是正在发生的最小版本:
Master.bat
powershell -ExecutionPolicy ByPass -File "Slave.ps1"
Slave.ps1
$foo = @{Name = "Foo"}
$foo.Path = "\\remote-server\foothing"
$bar = @{Name = "Bar"}
$bar.Path = "\\remote-server\barthing"
@( $foo, $bar ) | ForEach-Object {
$item = Get-ChildItem $_.Path
# Do things with item
}
我遇到的问题是,当我运行Master.bat时,在
Get-ChildItem
处失败,并显示类似以下的错误信息。get-childitem : Cannot find path '\\remote-server\foothing' because it does not exist.
然而,如果我直接在PowerShell中运行Slave.ps1文件,则似乎完美地工作。为什么只有在运行Master.bat文件时才会出现这种情况?
我尝试过的方法
- 在UNC路径前面添加
FileSystem::
提供程序http://powershell.org/wp/2014/02/20/powershell-gotcha-unc-paths-and-providers/ - 确保实际路径中没有奇怪的字符
- 对于
Get-ChildItem
,使用-literalPath
参数而不是普通的-path
参数 - 在PowerShell中运行
Get-ChildItem \\remote-server\foothing
并成功验证连接到远程服务器
powershell -version 2 -ExecutionPolicy ByPass -File "c:\temp\Slave.ps1"
。 - Loïc MICHEL