Powershell UNC Path不支持异常

Bob*_*man 3 powershell unc filepath

好吧,我在这个问题上已经挣扎了很长时间.我有一个项目来比较两个文件夹,两个服务器各一个.我们将源服务器上的文件与目标服务器上的文件进行比较,并在目标服务器上完成更新后创建源文件列表,这些文件需要刷新.

这是我的脚本(非常感谢原文http://quickanddirtyscripting.wordpress.com):

param ([string] $src,[string] $dst)

function get-DirHash()
{
    begin 
    {
        $ErrorActionPreference = "silentlycontinue"
    }
    process 
    {
        dir -Recurse $_ | where { $_.PsIsContainer -eq $false -and ($_.Name -like "*.js" -or $_.Name -like "*.css"} | select Name,FullName,@{Name="SHA1 Hash"; Expression={get-hash $_.FullName -algorithm "sha1" }}
    }
    end 
    {
    }
}  

function get-hash 
{
    param([string] $file = $(throw 'a filename is required'),[string] $algorithm = 'sha256')
    try
    {
        $fileStream = [system.io.file]::openread((resolve-path $file));
        $hasher = [System.Security.Cryptography.HashAlgorithm]::create($algorithm);
        $hash = $hasher.ComputeHash($fileStream);
        $fileStream.Close();
    }
    catch
    {
        write-host $_
    }
    return $hash
}

Compare-Object $($src | get-DirHash) $($dst | get-DirHash) -property @("Name", "SHA1 Hash")
Run Code Online (Sandbox Code Playgroud)

现在出于某种原因,如果我针对本地路径c:\temp\test1 c:\temp\test2运行它说它工作正常,但是当我使用UNC两台服务器之间的路径运行它时,我得到了

使用"1"参数调用"OpenRead"的异常:"不支持给定路径的格式."

任何有关这方面的帮助将不胜感激.最终结果应该是文件列表,但由于某种原因它不喜欢UNC路径.

脚本名称是compare_js_css.ps1这样调用的:

.\compare_js_css.ps1 c:\temp\test1 c:\temp\test2 < - 这很有效

.\compare_js_css.ps1 \\\\devserver1\c$\websites\site1\website \\\\devserver2\c$\websites\site1\website < - 返回上述异常.

为什么?

Gre*_*man 7

这给出了你没有的路径Microsoft.PowerShell.Core\FileSystem:::

(Resolve-Path $file).ProviderPath
Run Code Online (Sandbox Code Playgroud)

无需使用字符串替换.