Well, I've been struggling with this for so long. I have a project to compare two folders, one on each of the two servers. We compare the files on the source server with the destination servers on the target server and create a list of files from the source that need to be updated after the update is completed on the target server.
Here is my script (thank you very much http://quickanddirtyscripting.wordpress.com for the original):
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")
Now for some reason, if I run it with respect to local paths, let's say it c:\temp\test1 c:\temp\test2works fine, but when I run it using paths UNCbetween two servers, I get
An OpenRead exception is thrown with argument "1": "This path format is not supported."
. , - UNC.
script 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 < - .
?