gpt4 book ai didi

Powershell UNC 路径不支持异常

转载 作者:行者123 更新时间:2023-12-02 18:51:13 33 4
gpt4 key购买 nike

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

这是我的脚本(非常感谢 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")

现在,由于某种原因,如果我对本地路径运行此命令,例如 c:\temp\test1 c:\temp\test2 它工作正常,但是当我使用 UNC 运行它时code> 我得到的两个服务器之间的路径

Exception calling "OpenRead" with "1" argument(s): "The given path's format is not supported."

对此的任何帮助将不胜感激。最终结果应该是文件列表,但由于某种原因它不喜欢 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 <-- 返回上述异常.

为什么?

最佳答案

这给出了您在没有 Microsoft.PowerShell.Core\FileSystem:::

的情况下所追求的路径:

(Resolve-Path $file).ProviderPath

无需使用字符串替换。

关于Powershell UNC 路径不支持异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10656846/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com