PowerShell为自定义对象属性添加价值
我是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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
假设文件夹中的CSV文件已经使用给定的标题,您可以用这样的内容替换第二个函数:
它将读取现有的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:
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.