我是 PowerShell 的新手,有點迷茫,我創建了一個新的 PSobject,它存盤檔案的日期和哈希,并且資料存盤在 .csv 檔案中。我的問題在于嘗試在檔案的哈希更改時更新資訊。我不想覆寫檔案,而是添加新資訊。以下是我到目前為止所做的以及我得到的錯誤。我希望有人能澄清我遇到的問題。提前致謝
function GetFileHash {
$wc = [System.Net.WebClient]::new()
$settingsObject = Get-Content -Path $PSScriptRoot\config.json | ConvertFrom-Json
try {
Get-FileHash -InputStream($wc.OpenRead($settingsObject.pdfUrl))
}
catch {
return $null
}
}
function CheckforUpdate {
$fileHash = GetFileHash
if ($null -eq $fileHash) {
"Some error occured. The error was: " $Error[0]
return
}
$csvPath = Join-Path -Path $PSScriptRoot -ChildPath "hashInfo.csv"
$checkDate = Get-Date
if ((Test-Path $csvPath) -eq $false) {
$CSVObject = New-Object PSobject
$CSVObject | Add-Member -MemberType NoteProperty -Name "CheckDate" -Value $checkDate.DateTime
$CSVObject | Add-Member -MemberType NoteProperty -Name "LastknowHash" -Value $fileHash.Hash
$CSVObject | Export-Csv -NoTypeInformation -Path $csvPath
} else {
Write-Host "The csv file exist"
$csvData = @(import-csv $csvPath)
$onlineHash = $fileHash.Hash
$lastKnowHash = $csvData | Where-Object {$_.Value -eq $onlineHash}
if ($lastKnowHash -ne $onlineHash) {
[PSCustomObject]@{
CheckDate = $checkDate.DateTime
LastknowHash = $fileHash.Hash
} | Export-Csv -Path $csvPath -Append -NoTypeInformation
}
}
}
CheckforUpdate
這是錯誤:
未找到此物件的 CheckDate 屬性。確保該屬性存在并且可以設定。找不到此物件的 LastKnowHash 屬性。確保該屬性存在并且可以設定。
uj5u.com熱心網友回復:
假設檔案夾中已經有一個帶有給定標題的 CSV 檔案,您可以將第二個函式替??換為以下內容:
$csvPath = Join-Path -Path $PSScriptRoot -ChildPath 'hashInfo.csv'
$csvData = Import-Csv -Path $csvPath
$LastFileHash = ($csvData | Select-Object -Last 1).LastknowHash
$CurrentFileHash = (GetFileHash).Hash
if ($LastFileHash -ne $CurrentFileHash) {
[PSCustomObject]@{
CheckDate = Get-Date
LastknowHash = $CurrentFileHash
} |
Export-Csv -Path $csvPath -Append -NoTypeInformation
}
它將讀取現有的 CSV 檔案,獲取最后一個檔案哈希并將其與您使用 function 獲得的當前檔案進行比較GetFileHash。如果它們不同,則當前檔案哈希與當前時間戳一起寫入 CSV 檔案。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/481832.html
上一篇:通用PDF轉換器
