Replace Get-FileHash with MD5 code that works on PowerShell 3.

This commit is contained in:
Chris Church 2014-08-28 01:42:22 -04:00
parent 984d551257
commit 0d1197a995
2 changed files with 21 additions and 15 deletions

View file

@ -89,7 +89,10 @@ class ShellModule(object):
script = '''
If (Test-Path -PathType Leaf "%(path)s")
{
(Get-FileHash -Path "%(path)s" -Algorithm MD5).Hash.ToLower();
$sp = new-object -TypeName System.Security.Cryptography.MD5CryptoServiceProvider;
$fp = [System.IO.File]::Open("%(path)s", [System.IO.Filemode]::Open, [System.IO.FileAccess]::Read);
[System.BitConverter]::ToString($sp.ComputeHash($fp)).Replace("-", "").ToLower();
$fp.Dispose();
}
ElseIf (Test-Path -PathType Container "%(path)s")
{

View file

@ -34,27 +34,30 @@ $result = New-Object psobject @{
If (Test-Path $path)
{
Set-Attr $result.stat "exists" $TRUE;
$info = Get-Item $path;
If ($info.Directory) # Only files have the .Directory attribute.
{
Set-Attr $result.stat "isdir" $FALSE;
Set-Attr $result.stat "size" $info.Length;
}
Else
{
Set-Attr $result.stat "isdir" $TRUE;
}
Set-Attr $result.stat "exists" $TRUE;
$info = Get-Item $path;
If ($info.Directory) # Only files have the .Directory attribute.
{
Set-Attr $result.stat "isdir" $FALSE;
Set-Attr $result.stat "size" $info.Length;
}
Else
{
Set-Attr $result.stat "isdir" $TRUE;
}
}
Else
{
Set-Attr $result.stat "exists" $FALSE;
Set-Attr $result.stat "exists" $FALSE;
}
If ($get_md5 -and $result.stat.exists -and -not $result.stat.isdir)
{
$path_md5 = (Get-FileHash -Path $path -Algorithm MD5).Hash.ToLower();
Set-Attr $result.stat "md5" $path_md5;
$sp = new-object -TypeName System.Security.Cryptography.MD5CryptoServiceProvider;
$fp = [System.IO.File]::Open($path, [System.IO.Filemode]::Open, [System.IO.FileAccess]::Read);
$hash = [System.BitConverter]::ToString($sp.ComputeHash($fp)).Replace("-", "").ToLower();
$fp.Dispose();
Set-Attr $result.stat "md5" $hash;
}
Exit-Json $result;