我正在尝试使用一个变量,select-string -pattern
但它什么也不返回。如果我将变量的内容复制并粘贴到语法中,它就可以工作。
我所拥有的是我之前记录的一些哈希值,并且想要再次获取同一目录的哈希值并查看它们是否与之前记录的相同,因此我尝试使用 select-string。
用于获取原始哈希:
$data2hash = "C:\users\blue\Desktop\*.txt"
$hash = Get-FileHash $data2hash -Algorithm md5
$h_hash = $hash.hash
PS C:\Users> get-filehash c:\users\bob\desktop\* -Algorithm md5 | export-csv c:\users\bob\desktop\hashes.csv
Algorithm Hash Path
--------- ---- ----
MD5 E081EAAA07EC3CBC71DBC374E85B3031 c:\users\bob\desktop\1.txt
MD5 78D85AB09077BA9BE641C5AFC1EDFEE9 c:\users\bob\desktop\2.txt
MD5 69B8789ED87248AB5B69C0421ADF6E54 c:\users\bob\desktop\3.txt
MD5 34DF279CA08B79238246787321939C60 c:\users\bob\desktop\4.txt
验证阶段
$db = 'C:\users\bob\Desktop\hashes.csv'
$db2 = Get-Content $db
$some_data = "C:\users\bob\Desktop\*"
$new_hashes = Get-FileHash $data2hash -Algorithm md5
$h_hashes = $hash.hash
foreach($h in $h_hash)
{
$format_hash = "'" + $h + "',"
$d += $format_hash
}
$d = $d.substring(0,$d.length-1)
PS C:\Users\bob\desktop> $d.GetType()
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True String System.Object
$db2 | select-string -pattern $d
期望的输出:
PS C:\Users> $db2 | select-string -pattern $d
"MD5","3C535413A18289E8CEEF69ED15479515","C:\users\blue\Desktop\computers.txt"
"MD5","60E6B4CF0E9A1E99E5861ECE1001DB3D","C:\users\blue\Desktop\filecheck.txt"
"MD5","BC36FF295E4A68EE9C8E04B1D833E836","C:\users\blue\Desktop\FilesCheck_02-08-14.txt