none
PowerShell Compare Not Working Correctly RRS feed

  • General discussion

  • I am using PowerShell get-filehash and compare to audit a set of folders/files. Sometimes it works, sometimes not. When the compare does not work correctly I can open the two file with WinMerge and see that the compare files are indeed different. I can provide copies of the .csv files if someone wants to look at them. Below is my script. File path and email information has been modified for security.

    Does anyone have any idea what I'm doing wrong?

    $Date=(Get-Date).ToString('MM-dd-yyyy')
    ### Gets A Hash For The bin Folder:
    Set-Location "C:\inetpub\wwwroot\Web Site\bin"
    Get-ChildItem -Recurse | Foreach {Get-FileHash -Path $_.FullName | Sort Path | Export-Csv C:\PSTemp\binHash-$Date.csv -Append -NoTypeInformation}
    $Baseline = Import-Csv C:\PSTemp\Baseline_bin.csv
    $Diff = Import-Csv C:\PSTemp\binHash-$Date.csv
    $Compare1 = Compare-Object -ReferenceObject $Baseline -DifferenceObject $Diff
    $Compare1 | Out-File C:\PSTemp\binCompare-$Date.txt
    If (!$Compare1) {$Body1 = "There are no compare differences for the bin Folder."}
    Else {$Body101 = "There are issues in the bin folder. See the compare file."}
    ### Gets A Hash For The Scripts Folder:
    Set-Location "C:\inetpub\wwwroot\Web Site\Scripts"
    Get-ChildItem -recurse | Foreach {Get-FileHash -Path $_.FullName | Sort Path | Export-Csv C:\PSTemp\ScriptsHash-$Date.csv -Append -NoTypeInformation}
    $Baseline = Import-Csv C:\PSTemp\Baseline_Scripts.csv
    $Diff = Import-Csv C:\PSTemp\ScriptsHash-$Date.csv
    $Compare2 = Compare-Object -ReferenceObject $Baseline -DifferenceObject $Diff
    $Compare2 | Out-File C:\PSTemp\ScriptsCompare-$Date.txt
    If (!$Compare2) {$Body2 = "There are no compare differences for the Scripts Folder."}
    Else {$Body102 = "There are issues in the Scripts folder. See the compare file."}
    ### Gets a Hash for the Views Folder:
    Set-Location "C:\inetpub\wwwroot\Web Site\Views"
    Get-ChildItem -recurse | Foreach {Get-FileHash -Path $_.FullName | Sort Path | Export-Csv C:\PSTemp\ViewsHash-$Date.csv -Append -NoTypeInformation}
    $Baseline = Import-Csv C:\PSTemp\Baseline_Views.csv
    $Diff = Import-Csv C:\PSTemp\ViewsHash-$Date.csv
    $Compare3 = Compare-Object -ReferenceObject $Baseline -DifferenceObject $Diff
    $Compare3 | Out-File C:\PSTemp\ViewsCompare-$Date.txt
    If (!$Compare3) {$Body3 = "There are no compare differences for the Views Folder."}
    Else {$Body103 = "There are issues in the Views folder. See the compare file."}
    ###
    Set-Location "C:\inetpub\wwwroot\Web Site\"
    Get-FileHash -Path .\Web.config | Export-Csv C:\PSTemp\WebConfigHash-$Date.csv -Append -NoTypeInformation
    $Baseline = Import-Csv C:\PSTemp\Baseline_WebConfig.csv
    $Diff = Import-Csv C:\PSTemp\WebConfigHash-$Date.csv
    $Compare4 = Compare-Object -ReferenceObject $Baseline -DifferenceObject $Diff
    $Compare4 | Out-File C:\PSTemp\WebConfigCompare-$Date.txt
    If (!$Compare4) {$Body4 = "There are no compare differences for Web.Config."}
    Else {$Body104 = "There is an issue with the Web.Config file. See the compare file."}
    ### Send email
    If ($Body1) {}
    Else {$Subject1 = "bin"}
    If ($Body2) {}
    Else {$Subject2 = "Scripts"}
    If ($Body3) {}
    Else {$Subject3 = "Views"}
    If ($Body4) {}
    Else {$Subject4 = "Web.Config"}
    $Failure = "$Subject1" + "$Subject2" + "$Subject3" + "$Subject4"

    If (!$Failure) {
                   Send-MailMessage -to "EmailAddresHere" -Subject "Web Server Folder Audit Success $Date" -BodyAsHtml -Body "$Body1<br />$Body2<br />$Body3<br />$Body4"  -SmtpServer "SMTPServerHere" -From "EmailAddressHere" -Attachments "C:\PSTemp\ViewsHash-$Date.csv", "C:\PSTemp\Baseline_Views.csv", "C:\PSTemp\ScriptsHash-$Date.csv", "C:\PSTemp\Baseline_Scripts.csv", `
                   "C:\PSTemp\binHash-$Date.csv", "C:\PSTemp\Baseline_bin.csv", "C:\PSTemp\ViewsCompare-$Date.txt", "C:\PSTemp\ScriptsCompare-$Date.txt", "C:\PSTemp\binCompare-$Date.txt", "C:\PSTemp\Baseline_WebConfig.csv", "C:\PSTemp\WebConfigHash-$Date.csv", "C:\PSTemp\WebConfigCompare-$Date.txt"
                   }
    Else {
         Send-MailMessage -to "EmailAddresHere" -Subject "Web Server Folder Audit Failure $Date" -BodyAsHtml -Body "$Body101<br />$Body102<br />$Body103<br />$Body104"  -SmtpServer "SMTPServerHere" -From "EmailAddresHere" -Attachments "C:\PSTemp\ViewsHash-$Date.csv", "C:\PSTemp\Baseline_Views.csv", "C:\PSTemp\ScriptsHash-$Date.csv", "C:\PSTemp\Baseline_Scripts.csv", `
         "C:\PSTemp\binHash-$Date.csv", "C:\PSTemp\Baseline_bin.csv", "C:\PSTemp\ViewsCompare-$Date.txt", "C:\PSTemp\ScriptsCompare-$Date.txt", "C:\PSTemp\binCompare-$Date.txt", "C:\PSTemp\Baseline_WebConfig.csv", "C:\PSTemp\WebConfigHash-$Date.csv", "C:\PSTemp\WebConfigCompare-$Date.txt"
         }

    ### Clean Up
    Remove-Item C:\PSTemp\binHash-$Date.csv
    Remove-Item C:\PSTemp\ScriptsHash-$Date.csv
    Remove-Item C:\PSTemp\ViewsHash-$Date.csv
    Remove-Item C:\PSTemp\WebConfigHash-$Date.csv
    Remove-Item C:\PSTemp\binCompare-$Date.txt
    Remove-Item C:\PSTemp\ScriptsCompare-$Date.txt
    Remove-Item C:\PSTemp\ViewsCompare-$Date.txt
    Remove-Item C:\PSTemp\WebConfigCompare-$Date.txt

    • Changed type Bill_Stewart Thursday, September 5, 2019 9:51 PM
    • Moved by Bill_Stewart Thursday, September 5, 2019 9:51 PM This is not "fix/debug/rewrite script for me" forum
    Tuesday, August 6, 2019 5:44 PM

All replies