比较包含REG_MULTI_SZ注册表值的变量

时间:2017-02-09 14:02:34

标签: powershell variables compare registry

我需要一个定期读取注册表值(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"}
  • 如果TestValue reg值为为空,则脚本输出" 不同"
  • 如果TestValue reg值仅 1个字符串,则脚本输出 "的相同"
  • 如果TestValue reg值的值超过1个字符串,则为 脚本输出" 相同"

Image

你能建议吗?

2 个答案:

答案 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

Msdn Compare-Object

  

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;。

相关问题