Powershell 下计算MD5 之二[转]
2009-02-02 13:29
225 查看
转载自http://blogs.msdn.com/powershell/archive/2006/04/25/583225.aspx
function Get-MD5([System.IO.FileInfo] $file = $(throw 'Usage: Get-MD5 [System.IO.FileInfo]'))
{
$stream = $null;
$cryptoServiceProvider = [System.Security.Cryptography.MD5CryptoServiceProvider];
$hashAlgorithm = new-object $cryptoServiceProvider
$stream = $file.OpenRead();
$hashByteArray = $hashAlgorithm.ComputeHash($stream);
$stream.Close();
## We have to be sure that we close the file stream if any exceptions are thrown.
trap
{
if ($stream -ne $null)
{
$stream.Close();
}
break;
}
return [string]$hashByteArray;
}
I think about the only new thing here is the trap statement. It’ll get called if any exception is thrown, otherwise its just ignored. Hopefully nothing will go wrong with the function but if anything does I want to be sure to close any open streams. Anyway, keep this function around, we’ll use it along with AddNotes and group-object to write a simple script that can search directories and tell us all the files that are duplicates. Now… an example of this function in use:
MSH>"foo" > foo.txt
MSH>"bar" > bar.txt
MSH>"foo" > AlternateFoo.txt
MSH>dir *.txt | foreach { get-md5 $_ }
33 69 151 28 248 32 88 177 8 34 154 58 46 59 255 53
54 122 136 147 125 209 249 229 12 105 236 19 140 5 107 169
33 69 151 28 248 32 88 177 8 34 154 58 46 59 255 53
MSH>
function Get-MD5([System.IO.FileInfo] $file = $(throw 'Usage: Get-MD5 [System.IO.FileInfo]'))
{
$stream = $null;
$cryptoServiceProvider = [System.Security.Cryptography.MD5CryptoServiceProvider];
$hashAlgorithm = new-object $cryptoServiceProvider
$stream = $file.OpenRead();
$hashByteArray = $hashAlgorithm.ComputeHash($stream);
$stream.Close();
## We have to be sure that we close the file stream if any exceptions are thrown.
trap
{
if ($stream -ne $null)
{
$stream.Close();
}
break;
}
return [string]$hashByteArray;
}
I think about the only new thing here is the trap statement. It’ll get called if any exception is thrown, otherwise its just ignored. Hopefully nothing will go wrong with the function but if anything does I want to be sure to close any open streams. Anyway, keep this function around, we’ll use it along with AddNotes and group-object to write a simple script that can search directories and tell us all the files that are duplicates. Now… an example of this function in use:
MSH>"foo" > foo.txt
MSH>"bar" > bar.txt
MSH>"foo" > AlternateFoo.txt
MSH>dir *.txt | foreach { get-md5 $_ }
33 69 151 28 248 32 88 177 8 34 154 58 46 59 255 53
54 122 136 147 125 209 249 229 12 105 236 19 140 5 107 169
33 69 151 28 248 32 88 177 8 34 154 58 46 59 255 53
MSH>
相关文章推荐
- Powershell 下计算SHA1 以及MD5 摘要[转]
- hdfs file md5 计算,实现本地与hdfs同步文件
- Windows Phone 7(WP7)开发 MD5计算
- iphoneMD5计算方法
- 计算 md5
- Powershell 日期计算
- 使用wxPython建立一个计算文件md5的GUI工具
- [MD5]_[Windows-macOS]_[计算文件的MD5]
- 计算 md5 值
- 使用asp.net提供的MD5实现加密计算
- Botan的编译及MD5计算
- 利用HTML5/JavaScript 计算Image的MD5
- JS JAVA MD5 计算
- 性能测试学习之二 ——性能测试模型(PV计算模型)
- Hash MD5计算
- 批量计算MD5的值工具
- 【Python】计算文件MD5 和 SHA1
- 适用于ATI卡的GPU计算MD5的小程序源码,基于AMD APP SDK开发
- C#实现MD5计算
- MPI并行计算环境建立之二--挂载nfs文件系统