好吧,我在这个问题上已经挣扎了很长时间.我有一个项目来比较两个文件夹,两个服务器各一个.我们将源服务器上的文件与目标服务器上的文件进行比较,并在目标服务器上完成更新后创建源文件列表,这些文件需要刷新.
这是我的脚本(非常感谢原文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
{ …Run Code Online (Sandbox Code Playgroud)