一个计算MD5的脚本

博客分享了在monad的team blog上看到的一个可获取每个文件MD5 hash的脚本,还提到MSH是dot net脚本,直接调用FCL里的方法增强了脚本威力,比VBscript时代的COM粘贴剂更强大。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

今天在monad的team blog上看到一个脚本,可以获取每个文件的MD5 hash,引用到这里分享一下。

# # Calculates the hash of a file and returns it as a string.

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 ;

}

用法:

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确实是一个dot net脚本,直接调用FCL里的方法确实增强了脚本的威力。比起VBscript时代的COM粘贴剂又不可同日而语了。15.gif

转载于:https://www.cnblogs.com/hayate/archive/2005/10/08/250532.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值