json - Powershell 比较哈希表对象
问题描述
我正在尝试使用 Powershell 检索服务器状态,并查看它自上次运行以来是否发生了变化。我的服务器输出一个 json 哈希表。这个想法是导入数据并将其与以前的数据进行比较,如果它已更改,则将新数据保存到磁盘并做一些“事情”。
将 json 导入 PSObject 很容易,似乎将其存储到磁盘的常用方法是使用 Export-Clixml。但是使用 Import-Clixml 读回数据并不会给出相同的 PSObject。至少它们有不同的 TypeName 值。
我的代码归结为:
$newserverstatus = Get-Content C:\status.json | ConvertFrom-Json
#for real I am piping the output from the status command into
#ConvertFrom-Json, in testing I read the content from a file
$oldserverstatus = Import-Clixml C:\oldstatus.xml
#read the saved information from disk
if ($oldserverstatus -ne $newserverstatus)
{
#do "things" using the data#
$newserverstatus | Export-Clixml C:\oldstatus.xml
#and finish with saving the updated status to disk
}
如果这行得通,我就不会在这里发帖了。如前所述,我注意到对象具有不同的 TypeName 值,但数据似乎相同。
主要问题是是否有一种方法可以在不查看 PSObject 元数据的情况下比较对象数据?我不是程序员,所以自然有很多不同的更好的方法可以做到这一点,我只是认为这是一个可能的解决方案,将新数据导入对象并将其与从保存的数据创建的对象进行比较。我使用的 json 数据是这样的:
{
"clusterName": "CLUSTER11",
"defaultReplicaSet": {
"name": "default",
"primary": "192.168.2.30:3306",
"ssl": "DISABLED",
"status": "OK",
"statusText": "Cluster is ONLINE and can tolerate up to ONE failure.",
"topology": {
"192.168.2.30:3306": {
"address": "192.168.2.30:3306",
"mode": "R/W",
"readReplicas": {},
"replicationLag": null,
"role": "HA",
"status": "ONLINE",
"version": "8.0.19"
},
"192.168.2.31:3306": {
"address": "192.168.2.31:3306",
"mode": "R/O",
"readReplicas": {},
"replicationLag": null,
"role": "HA",
"status": "ONLINE",
"version": "8.0.19"
},
"192.168.3.139:3306": {
"address": "192.168.3.139:3306",
"mode": "R/O",
"readReplicas": {},
"replicationLag": null,
"role": "HA",
"status": "ONLINE",
"version": "8.0.19"
}
},
"topologyMode": "Single-Primary"
},
"groupInformationSourceMember": "192.168.2.30:3306"
}
我还尝试使用 ConvertTo-Json 将数据保存为 json 格式,并使用 ConvertFrom-Json 读取旧状态,但无济于事。将新服务器状态直接保存到文件中,并将该文件与旧副本进行比较的想法出现在我的脑海中,但对我来说,这似乎是一种非常丑陋的做法。
解决方案
推荐阅读
- python-2.7 - 在 Ubuntu 18.04 LTS 上构建 doom_py 时出现编译错误
- python - 熊猫数据框评估功能错误
- c# - 基类中的属性注入
- mysql - 在 MySQL 中使用循环将 UNION 选择转换为单选
- visual-studio - 在使用表单 QtCreator 时,是否有阻止 MVSC 创建发布和调试文件夹的方法?
- git - 如何在两个环境之间同步所有 git 操作
- javascript - 插入php变量时JQuery函数失败
- mysql - 插入前触发不起作用
- vb.net - 我想用表单中的两个文本框(textbox4.text 和 textbox5.text = variable)在 sqlite 中的两个日期之间进行查询
- python - CVXPY:使用两组不同格式的约束返回无