2016-04-14 29 views
0

我想生成一個HTML表格,它顯示目錄中一堆文件的filehash(sha1);我想要的文件名是相對於我的當前目錄 - 不是絕對的。如何生成Filehashes的HTML表 - 僅使用相對路徑?

我知道如何分開做所有不同的位,但我無法弄清楚如何鏈接起來。

這裏是我到目前爲止有:

dir|get-filehash -Algorithm sha1 

,給了我這樣的:

Algorithm  Hash                 Path 
---------  ----                 ---- 
SHA1   DA39A3EE5E6B4B0D3255BFEF95601890AFD80709        C:\temp\test\empty.txt 
SHA1   88A5B867C3D110207786E66523CD1E4A484DA697        C:\temp\test\hello.txt 

現在我只希望哈希和文件名,這樣我就可以做到這一點:

dir|get-filehash -Algorithm sha1|select-object hash, path 

哪給了我:

Hash              Path 
----              ---- 
DA39A3EE5E6B4B0D3255BFEF95601890AFD80709     C:\temp\test\empty.txt 
88A5B867C3D110207786E66523CD1E4A484DA697     C:\temp\test\hello.txt 

所以,我可以將此輸出到HTML文件是這樣的:

(dir|get-filehash -Algorithm sha1|select-object hash, path)|ConvertTo-html|add-content output.htm 

[忽略了一個事實,這只是正常工作,如果輸出文件不存在,現在。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<title>HTML TABLE</title> 
</head><body> 
<table> 
<colgroup><col/><col/></colgroup> 
<tr><th>Hash</th><th>Path</th></tr> 
<tr><td>DA39A3EE5E6B4B0D3255BFEF95601890AFD80709</td><td>C:\temp\test\empty.txt</td></tr> 
<tr><td>88A5B867C3D110207786E66523CD1E4A484DA697</td><td>C:\temp\test\hello.txt</td></tr> 
</table> 

所以這給了我一個HTML表格;但PATH值是絕對的。

我知道獲得使用「解決路徑」 cmdlet的相對路徑的簡單方法:

dir | Resolve-Path -Relative 

.\empty.txt 
.\hello.txt 

但我無法得到它在我的腳本的其餘部分「適應」;我想他們可能是一個.NET函數以不同的方式做到這一點?還是有一些花哨的忍者使用的括號,讓我把這個調用擠到'選擇對象'列表的cmdlet insde?

我嘗試這樣做:但它不工作:

# NOTE: this code does not work ! 
PS > dir|get-filehash|select-object hash, (path|Resolve-Path -relative) 

回答

0

就這一一些進展 - 但解決方法不是很滿意 - 因爲我還需要在頭控制(而不僅僅是'name','HashTable提供的'值')。

我可以在'format-table'中獲得這些頭文件,但仍然不在'convertto-html'中。

這裏是到目前爲止的代碼:

function get-relative($infile) { 
begin { [email protected]{} } 
process { 
    $relative_path=($_|resolve-path -Relative) 
    $filehash=($_| Get-FileHash -Algorithm sha1).hash 
    $return_hash.add($relative_path, $filehash) 
} 
end { return $return_hash } 

} 

並把它與一個格式化撥打:

$table_format = @{Expression={$_.Name} ; Label="Filename"}, @{Expression={$_.Value} ; Label="CHECKSUM(SHA1)"} 
dir|get-relative|format-table $table_format 

這給:

Filename                CHECKSUM(SHA1)               
--------                --------------               
.\goodbye.txt               DA39A3EE5E6B4B0D3255BFEF95601890AFD80709        
.\hello.txt                88A5B867C3D110207786E66523CD1E4A484DA697 

但如果我推,通過了「 convertto-html',怪異隨之而來。 (我猜這是因爲'format-table'的輸出現在只是一個字符串....)

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<title>HTML TABLE</title> 
</head><body> 
<table> 
<colgroup><col/><col/><col/><col/><col/><col/></colgroup> 
<tr><th>ClassId2e4f51ef21dd47e99d3c952918aff9cd</th><th>pageHeaderEntry</th><th>pageFooterEntry</th><th>autosizeInfo</th><th>shapeInfo</th><th> 
groupingEntry</th></tr> 
<tr><td>033ecb2bc07a4d43b5ef94ed5a35d280</td><td></td><td></td><td></td><td>Microsoft.PowerShell.Commands.Internal.Format.TableHeaderInfo</td>< 
td></td></tr> 
<tr><td>9e210fe47d09416682b841769c78b8a3</td><td></td><td></td><td></td><td></td><td></td></tr> 
<tr><td>27c87ef9bbda4f709f6b4002fa4af63c</td><td></td><td></td><td></td><td></td><td></td></tr> 
<tr><td>27c87ef9bbda4f709f6b4002fa4af63c</td><td></td><td></td><td></td><td></td><td></td></tr> 
<tr><td>4ec4f0187cb04f4cb6973460dfe252df</td><td></td><td></td><td></td><td></td><td></td></tr> 
<tr><td>cf522b78d86c486691226b40aa69e95c</td><td></td><td></td><td></td><td></td><td></td></tr> 
</table> 
</body></html>