gpt4 book ai didi

powershell - 比较文件哈希并输出文件

转载 作者:行者123 更新时间:2023-12-03 01:17:40 24 4
gpt4 key购买 nike

我是PowerShell的新手,正在编写脚本以获取目录的哈希并将其存储在.txt文件中。

然后,我想将其与早期版本进行比较并检查更改。如果有更改,我想要一个新的.txt或.html文件,其中包含更改了哪些订单项以及最近一次修改的日期。

到目前为止,我已经进行了比较,并且基于通过/失败的结果步骤正常进行。

我需要帮助的是将结果输出到一个.txt文件中,该文件仅列出已更改的文件,并带有算法,哈希,文件名,上次编辑时间等字段。我知道我可以用

(Get-Item $source).LastWriteTime

为了获取写入时间,但是我需要对目录中的每个文件执行此操作,而不仅仅是包含哈希的.txt文件。
# Variables
$Hashstore = "d:\baseline.txt"
$HashCompare = "d:\hashcompare.txt"
$HashTemp = "d:\hashtemp.txt"
$FileDir = "d:\New2"
$DateTime = Get-Date -format M.d.yyyy.hh.mm.ss

# Email Variables
$smtp_server = '<yourSMTPServer>'
$to_email = '<email>'
$from_email = '<email>'
$dns_server = "<yourExternalDNSServer>"
$domain = "<yourDomain>"

# Check if Baseline.txt Exists
If (Test-Path $Hashstore)
# // File exists
{}
Else {
# // File does not exist - Should never happen!
$RefreshHash = dir $FileDir | Get-FileHash -Algorithm MD5
$RefreshHash | Out-File $Hashstore
}

# Generate new Compare Hash.txt
$HashNew = dir $FileDir -Recurse | Get-FileHash -Algorithm MD5
$HashNew | Out-File $HashCompare

# Get Hash of baseline.txt
$HashBaseline = Get-FileHash -Path d:\baseline.txt -Algorithm MD5

#Get Hash of hashcompare.txt
$HashDiff = Get-FileHash -Path d:\hashcompare.txt -Algorithm MD5

#If changed, output hash to storage, and flag changes
If ($HashBaseline.hash -eq $HashDiff.hash)
{
Add-Content -Path d:\success.$DateTime.txt -Value " Source Files ARE EQUAL </p>"
}
else
{
Add-Content -Path d:\failure.$DateTime.html -Value "Source Files NOT EQUAL </p>"
$HashNew | Out-File $HashTemp
}

# Compare two logs, send email if there is a change

If ($diff_results)
{
#$evt_message = Get-Content .\domain.new.txt | Out-String
#Write-EventLog -LogName Application -EventId 9000 -EntryType Error -Source "Maximo Validation Script" -Message $evt_message
#Send-MailMessage -To $to_email -From $from_email -SmtpServer $smtp_server -Attachments .\domain.new.txt -Subject "ALERT! Change in Records" -Body "A change has been detected in the Maximo system files.`n`n`tACTION REQUIRED!`n`nVerify that this change was authorized."
}

If ($HashNew.HashString -eq $Hashstore.HashString)
{
}
else
{
$HashTemp | Out-File $HashStore
}

我知道添加项可能不是写入正在创建的日志的最佳方法。将最后写入时间添加到每个读取文件的最佳方法是什么?

最佳答案

这是一种干净的方法来输出已更改的每个文件所需的信息(算法,哈希,文件名,最后编辑时间):

$Hashstore = "d:\baseline.txt"
$HashCompare = "d:\hashcompare.txt"
$HashTemp = "d:\hashtemp.txt"
$FileDir = "d:\New2"
$DateTime = Get-Date -format M.d.yyyy.hh.mm.ss

# Check if Baseline.txt Exists
If (Test-Path $Hashstore)
# // File exists
{
}
Else {
# // File does not exist - Should never happen!
$RefreshHash = dir $FileDir -Recurse | Get-FileHash -Algorithm MD5
$RefreshHash | Export-Csv -Path $Hashstore -NoTypeInformation -Force
}

# Generate new Compare Hash.txt
$HashNew = dir $FileDir -Recurse | Get-FileHash -Algorithm MD5
$HashNew | Export-Csv -Path $HashCompare -NoTypeInformation -Force

# Get Hash of baseline.txt
$HashBaseline = Get-FileHash -Path $Hashstore -Algorithm MD5

#Get Hash of hashcompare.txt
$HashDiff = Get-FileHash -Path $HashCompare -Algorithm MD5

#If changed, output hash to storage, and flag changes
If ($HashBaseline.hash -eq $HashDiff.hash) {
Add-Content -Path D:\success.$DateTime.txt -Value " Source Files ARE EQUAL </p>"
}
Else {
Add-Content -Path D:\failure.$DateTime.txt -Value "Source Files NOT EQUAL </p>"
$HashNew | Export-Csv -Path $HashTemp -NoTypeInformation -Force

# Storing a collection of differences in $Diffs
$Diffs = Compare-Object -ReferenceObject (Import-Csv $Hashstore) -DifferenceObject (Import-Csv $HashCompare)

Foreach ($Diff in $Diffs) {
$DiffHashInfo = $Diff | Select-Object -ExpandProperty InputObject
$DiffFileInfo = Get-ChildItem -Path $DiffHashInfo.Path

# Creating a list of properties for the information you need
$DiffObjProperties = [ordered]@{'Algorithm'=$DiffHashInfo.Algorithm
'Hash'=$DiffHashInfo.Hash
'Filename'=$DiffFileInfo.Name
'Last edit time'=$DiffFileInfo.LastWriteTime
}

# Building a custom object from the list of properties in $DiffObjProperties
$DiffObj = New-Object -TypeName psobject -Property $DiffObjProperties
$DiffObj
}
}

在创建文件$ Hashstore和$ HashCompare之前,我将它们包含的信息转换为CSV格式,而不是纯文本。

使用Import-CSV,使它们的内容更容易在以后处理。
这使具有属性的适当对象可以使用。
这也使它们更易于比较,并且此比较的结果($ Diffs)是这些适当对象的集合。

因此$ Diffs包含所有已更改的文件,我在一个Foreach语句中循环遍历每个文件。

这使您可以创建一个自定义对象($ DiffObj),其中包含每个已更改文件的确切信息($ DiffObjProperties)。

关于powershell - 比较文件哈希并输出文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25315329/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com