Sort-Object -Unique



我正在编写一个脚本,从特定位置收集所有子键并将REG_BINARY键转换为文本,但由于某种原因,我无法删除重复的结果或按字母顺序排序。

PS:不幸的是,我需要从命令行执行的解决方案。

代码:

$List = ForEach ($i In (Get-ChildItem -Path 'HKCU:SOFTWARE00' -Recurse)) {$i.Property | ForEach-Object {([System.Text.Encoding]::Unicode.GetString($i.GetValue($_)))} | Select-String -Pattern ':'}; ForEach ($i In [char[]]'ABCDEFGHIJKLMNOPQRSTUVWXYZ') {$List = $($List -Replace("$i`:", "`n$i`:")).Trim()}; $List | Sort-Object -Unique

Test.reg:

Windows Registry Editor Version 5.00
[HKEY_CURRENT_USERSOFTWARE00Test1]
"HistorySZ1"="Test1"
"HistoryBIN1"=hex:43,00,3a,00,5c,00,54,00,65,00,73,00,74,00,5c,00,44,00,2e,00,
7a,00,69,00,70,00,5c,00,00,00,43,00,3a,00,5c,00,54,00,65,00,73,00,74,00,5c,
00,43,00,2e,00,7a,00,69,00,70,00,5c,00,00,00,43,00,3a,00,5c,00,54,00,65,00,
73,00,74,00,5c,00,42,00,2e,00,7a,00,69,00,70,00,5c,00,00,00,43,00,3a,00,5c,
00,54,00,65,00,73,00,74,00,5c,00,41,00,2e,00,7a,00,69,00,70,00,5c,00,00,00

[HKEY_CURRENT_USERSOFTWARE00Test2]
"HistorySZ2"="Test2"
"HistoryBIN2"=hex:4f,00,3a,00,5c,00,54,00,65,00,73,00,74,00,5c,00,44,00,2e,00,
7a,00,69,00,70,00,5c,00,00,00,43,00,3a,00,5c,00,54,00,65,00,73,00,74,00,5c,
00,43,00,2e,00,7a,00,69,00,70,00,5c,00,00,00,44,00,3a,00,5c,00,54,00,65,00,
73,00,74,00,5c,00,42,00,2e,00,7a,00,69,00,70,00,5c,00,00,00,41,00,3a,00,5c,
00,54,00,65,00,73,00,74,00,5c,00,41,00,2e,00,7a,00,69,00,70,00,5c,00,00,00

在字节数组中编码的路径字符串用NUL字符(代码点0x0)分隔。

因此,您需要字符串按此字符拆分为数组的单个路径,然后您可以在其上执行操作,如Sort-Object:

您可以在可扩展的PowerShell字符串中将NUL字符表示为"`0",或者在正则表达式中传递给-split操作符-:

# Convert the byte array stored in the registry to a string.
$text = [System.Text.Encoding]::Unicode.GetString($i.GetValue($_))
# Split the string into an *array* of strings by NUL.
# Note: -ne '' filters out empty elements (the one at the end, in your case).
$list = $text -split '' -ne ''
# Sort the list.
$list | Sort-Object -Unique

经过多次尝试后,我发现有必要使用Split命令使行断行,从而能够组织结果。

{$List = ($List -Replace("$i`:", "`n$i`:")) -Split("`n")}

最新更新