0

I am trying to use a variable in select-string -pattern but it returns nothing. If I copy and paste the contents of the variable into the syntax, it works.

What I have is a few hashes that I previously recorded and want to get the hashes of the same directory again and see if they are the same ones previously recorded, hence why I am trying to use select-string.

Used to get the original hashes:

$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         

Verification phase

    $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

Desired output:

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
on_looker
  • 3
  • 2

2 Answers2

1

Either pass the hashes as a string array directly to -Pattern, don't attempt to concatenate them:

$db2 | select-string -pattern $h_hashed

or, if you want to provide a single regex pattern, construct it with | (logical or in regex):

$pattern = '(?:{0})' -f ($h_hashed -join '|')
$db2 |Select-String -Pattern $pattern

I would also recommend pointing Select-String directly to the file, don't use Get-Content:

Select-String -Path 'C:\users\bob\Desktop\hashes.csv' -Pattern $pattern
Mathias R. Jessen
  • 157,619
  • 12
  • 148
  • 206
0

if you want compare hash and file into same dir, you can compare your csv files like this

#Get-FileHash "c:\temp\*.txt" -Algorithm md5 | export-csv "c:\temp\LastAnalyse.csv" -NoTypeInformation
Get-FileHash "c:\temp\*.txt" -Algorithm md5 | export-csv "c:\temp\NewAnalyse.csv" -NoTypeInformation

$LastAnalyse=import-csv "c:\temp\LastAnalyse.csv"
$NewAnalyse=import-csv "c:\temp\NewAnalyse.csv"

Compare-Object $LastAnalyse $NewAnalyse -Property Hash, Path

if you want look all différences:

Get-FileHash "c:\temp\*.txt" -Algorithm md5 | export-csv "c:\temp\NewAnalyse.csv" -NoTypeInformation

$LastAnalyse=import-csv "c:\temp\LastAnalyse.csv"
$NewAnalyse=import-csv "c:\temp\NewAnalyse.csv"

$lst=Compare-Object $LastAnalyse $NewAnalyse -Property Hash, Path

$lst | %{
$element=$_
$ListSameElement=$lst | where { $_.Path -eq $element.Path -and $_.Hash -ne $element.Hash} | select -First 1

if ($_.SideIndicator -eq '=>')
{
    if ($ListSameElement.Count -eq 0)
    {
    $Explain="New File Created"
    $OldHash=""
    }
    else
    {
    $Explain="Hash Modified"
    $OldHash=$ListSameElement.Hash
    }

[pscustomobject]@{Hash=$element.hash;File=$element.Path;OldHAsh=$OldHash; Explain=$Explain}

}

elseif ($_.SideIndicator -eq '<=' -and $ListSameElement.Count -eq 0)
{
     [pscustomobject]@{Hash="";File=$element.Path;OldHAsh=$element.hash; Explain="File Deleted"}
}

}
Esperento57
  • 16,521
  • 3
  • 39
  • 45
  • I was trying to stay away from compare-object but the way you did it makes it look good. Thanks, this does what I need it to do. – on_looker Jan 15 '17 at 04:45