我需要一个定期读取注册表值(REG_MULTI_SZ)的脚本。如果值已更改→执行某些操作。
$itemList1 = Get-ItemPropertyValue -Path Registry::HKCU\SOFTWARE\Tester -Name TestValue
# ...
$itemList2 = Get-ItemPropertyValue -Path Registry::HKCU\SOFTWARE\Tester -Name TestValue
if ($itemList1 -eq $itemList2) {echo "identical"} else {echo "different"}
你能建议吗?
答案 0 :(得分:0)
PS C:>$a=1;$b=2;$c=1
PS C:> if (Compare-Object $a $b) { Write-Host Different } else { Write-Host Equal }
Different
PS C:\Users\amewald> if (Compare-Object $a $c) { Write-Host Different } else { Write-Host Equal }
Equal
OUTPUTS
无,或不同的对象
这意味着如果两个对象相同,Compare-Object
将返回$null
。
希望有所帮助
答案 1 :(得分:0)
REG_MULTI_SZ
值基本上是字符串数组。要比较数组,请使用Compare-Object
。
Compare-Object $itemList1 $itemList2
要从结果中获取布尔值,只需将其转换为bool并反转值:
![bool](Compare-Object $itemList1 $itemList2)
如果两个比较对象相等(<{1}}未使用),则 Compare-Object
返回空结果,而PowerShell可以auto-convert空/ -IncludeEqual
值为{{} 1}}。使用$null
或$false
取消值会使#34; -not
中的逻辑反转,如果为空&#34;到&#34; !
如果为空&#34;。