PowerShell为自定义对象属性添加价值

发布于 2025-02-01 20:29:40 字数 1682 浏览 3 评论 0原文

我是PowerShell的新手,我有点丢失,我创建了一个新的psobject,该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属性。确保属性存在并可以设置。 对于此对象,找不到最后的知识属性。确保属性存在并可以设置。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

呆° 2025-02-08 20:29:40

假设文件夹中的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文件,将最后一个文件放哈希与您获得的当前文件进行比较getfilehash。如果它们不同,则当前文件哈希将写入CSV文件以及当前时间戳。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文