0

I am new in PowerShell, and I am a bit lost, I have created a new PSobject that stores Dates and the Hash of a file and that data is stored in a .csv file. My problem lies in trying to update the information when the Hash of the file changes. I would not want to overwrite the file, but add the new information. Below is what I have done so far and the error I get. I hope someone can clarify the problem that I have. Thanks in advance

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

Here is the error:

The CheckDate property was not found for this object. Make sure that the property exists and can be set. The LastKnowHash property was not found for this object. Make sure that the property exists and can be set.

7
  • 1
    You're using quite a lot of undefined variables.. Commented May 27, 2022 at 15:41
  • 1
    Did you mean $CSVObject instead of $csvData? Because $csvData isn't assigned anywhere in the code sample. Commented May 27, 2022 at 15:43
  • $csvData is where I import the csv file again. Commented May 27, 2022 at 16:03
  • 1
    The code sample is still incomplete. How are $fileHash and $onlineHash set? Commented May 27, 2022 at 16:32
  • If I got it right you create an object with two properties, export it to CSV and re-import it to compare it against the same values you used to create the object. What do you actually want to do? Commented May 27, 2022 at 16:33

1 Answer 1

2

Assumed there already is a CSV file in the folder with the given headers you could replace your second function with something like this:

$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
}

It will read the existing CSV file, take the last file hash and compare it against the current one you get with your function GetFileHash. If they're different the current file hash is written to the CSV file along with the current time stamp.

Sign up to request clarification or add additional context in comments.

3 Comments

The above error is no longer displayed, but every time I run the script it writes the same Hash (it has not changed) over and over again and what I need it to write to the csv file if the Hash has changed.
You did not show in your question where you create the hash. That's what we meant when we claimed that your code sample is incomplete.
I just updated the code, I hope you can help me. Thanks in advance

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.