我缺少一些基本知识,对于问题所在我完全是盲目的,所以我真的想加深我的理解。真正令人沮丧的是,它应该非常简单(很可能是这样),并且我在其他地方的同一脚本中完美地运行了这种确切的语法/功能...我认为问题是我可能在混淆/混合哈希表,对象和数组? (不像PHP那样简单明了...)
我只是尝试使用$Config.Bookmarks.EventLog = $CollectionTime
更新PSObject笔记属性的值。我也尝试过$Config.Bookmarks | Add-member...
运气不好。
我得到的错误是:
PS C:\Windows\system32> $Config.Bookmarks.EventLog = $CollectionTime
The property 'EventLog' cannot be found on this object. Verify that the property exists and can be set.
At line:1 char:1
+ $Config.Bookmarks.EventLog = $CollectionTime
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : PropertyAssignmentException
但是我可以看到EventLog在对象$Config
中:
PS C:\Windows\system32> $Config |%{$_|gm -m noteproperty}
TypeName: System.Management.Automation.PSCustomObject
Name MemberType Definition
---- ---------- ----------
Authentication NoteProperty System.Management.Automation.PSCustomObject Authentication=@{token=ey.....
Bookmarks NoteProperty System.Management.Automation.PSCustomObject Bookmarks=@{EventLog=25 January 2019 11:54:07}
PS C:\Windows\system32> $Config.Bookmarks
EventLog
--------
25 January 2019 11:54:07
PS C:\Windows\system32> $Config.Bookmarks.EventLog
25 January 2019 11:54:07
代码:
$Config = Get-Content -Path "$ProgramDataPath\config.json" -Raw -Force | ConvertFrom-Json
if (!$Config.Bookmarks) {
# No timestamp found for last event log upload, making one up to get the last 14 days of logs.
$Config += @{ "Bookmarks" = @{"EventLog"=([string](get-date).AddDays(-14).DateTime)}}
}
if(!$Config.Bookmarks.EventLog){
$Config.Bookmarks = @{"EventLog"=((get-date).AddDays(-14).DateTime)}
}
# Keep date as string so it stays pretty in JSON
$CollectionTime = [string](get-date).DateTime
$LogData = Get-WinEvent -LogName 'System', 'Application', 'Setup' -ErrorAction SilentlyContinue | Select-Object ContainerLog, Message, Id, RecordId, Level, ProviderName, ProviderId, @{label='TimeCreated';expression={$_.TimeCreated.ToString("d MMMM yyyy HH:mm:ss")}} | Where-Object {$_.TimeCreated -ge $Config.Bookmarks.EventLog} | ConvertTo-JSON
#This line isn't working, says object cannot be found
$Config.Bookmarks.EventLog = $CollectionTime
$Config | ConvertTo-Json -Depth 5 | Set-Content -Path "$ProgramDataPath\config.json" -Force