Powershell:可以对数组进行排序,但无法获取唯一值
Powershell: Can sort an array, but can't get unique values
我正在从 AD 收集一些用户信息并尝试对其进行排序,然后删除重复项,但我似乎无法使 sort -unique 正常工作:
foreach ($pute in $puters){
$user = get-loggedinuser -computer $pute.name | select -expandproperty username
$fullname = get-aduser $user | select -expandproperty name
$fullnames += $fullname
}
$fullnames = $fullnames | sort -unique
这给我的是这样的:
亚当术士
亚当术士
布鲁斯·比加洛
弗兰克·库特尔
弗兰克·库特尔
托尼·沙鲁布
输出已排序,但保留重复项。
更新: 问题原来是$fullnames
没有初始化为数组, $fullnames = @()
(在没有这个初始化的情况下,+=
执行简单的 字符串连接 而不是将元素附加到数组,从概念上讲说话)。
下面的答案对于诊断相同的 看起来 字符串可能有多大差异可能仍然有用。
因为您的 Sort-Object
输入是 strings (并且每行名称输出意味着 $fullnames
确实是一个 array,即使你没有这样显示它的初始化),这意味着 the ostensible重复项必须有 空格(类型) 或 不可见控制字符 .
的不同
您可以尝试以下技巧来发现这种不可见的差异:
'Adam Warlock',
'Adam Warlock ', # trailing space
'Frank Coutel',
('Frank{0}Coutel' -f [char] 0xa0) | # no-break space instead of regular space
ForEach-Object { $_ + "`t" + [int[]] [char[]] $_ } |
Sort-Object -Unique
输出:
Adam Warlock 65 100 97 109 32 87 97 114 108 111 99 107
Adam Warlock 65 100 97 109 32 87 97 114 108 111 99 107 32 # Note the extra 32
Frank Coutel 70 114 97 110 107 160 67 111 117 116 101 108 # Note the 160
Frank Coutel 70 114 97 110 107 32 67 111 117 116 101 108
数字是组成每个字符串的字符的 Unicode 代码点,采用十进制格式。
或者,为了更好地可视化差异,使用来自 this Gist:
的辅助函数 Debug-String
- 注意:以下代码段会自动下载并定义函数。我个人可以向你保证这样做是安全的,但你应该始终先自己检查源代码。
# Download and define helper function Debug-String
# * See comments above.
# * To see instructions on how to make the function available in
# *future* sessions, remove >3 $null and re-run this command directly
# from the command line.
irm https://gist.github.com/mklement0/7f2f1e13ac9c2afaf0a0906d08b392d1/raw/Debug-String.ps1 | iex 3>$null
'Adam Warlock',
'Adam Warlock ', # trailing space
'Frank Coutel',
('Frank{0}Coutel' -f [char] 0xa0) | # no-break space instead of regular space
Debug-String |
Sort-Object -Unique
输出:
Adam·Warlock
Adam·Warlock· # Note the trailing ·
Frank·Coutel
Frank`u{a0}Coutel # Note the `u{a0} escape sequence
Debug-String
将常规空格可视化为 ·
,将 ASCII 范围外的 formatting/control 字符可视化为 Unicode 转义序列,例如`u{a0}
,其中 a0
是字符的 Unicode 代码点的十六进制形式,即 no-break space ( U+00A0
) 字符,在这种情况下。
我正在从 AD 收集一些用户信息并尝试对其进行排序,然后删除重复项,但我似乎无法使 sort -unique 正常工作:
foreach ($pute in $puters){
$user = get-loggedinuser -computer $pute.name | select -expandproperty username
$fullname = get-aduser $user | select -expandproperty name
$fullnames += $fullname
}
$fullnames = $fullnames | sort -unique
这给我的是这样的:
亚当术士
亚当术士
布鲁斯·比加洛
弗兰克·库特尔
弗兰克·库特尔
托尼·沙鲁布
输出已排序,但保留重复项。
更新: 问题原来是$fullnames
没有初始化为数组, $fullnames = @()
(在没有这个初始化的情况下,+=
执行简单的 字符串连接 而不是将元素附加到数组,从概念上讲说话)。
下面的答案对于诊断相同的 看起来 字符串可能有多大差异可能仍然有用。
因为您的 Sort-Object
输入是 strings (并且每行名称输出意味着 $fullnames
确实是一个 array,即使你没有这样显示它的初始化),这意味着 the ostensible重复项必须有 空格(类型) 或 不可见控制字符 .
您可以尝试以下技巧来发现这种不可见的差异:
'Adam Warlock',
'Adam Warlock ', # trailing space
'Frank Coutel',
('Frank{0}Coutel' -f [char] 0xa0) | # no-break space instead of regular space
ForEach-Object { $_ + "`t" + [int[]] [char[]] $_ } |
Sort-Object -Unique
输出:
Adam Warlock 65 100 97 109 32 87 97 114 108 111 99 107
Adam Warlock 65 100 97 109 32 87 97 114 108 111 99 107 32 # Note the extra 32
Frank Coutel 70 114 97 110 107 160 67 111 117 116 101 108 # Note the 160
Frank Coutel 70 114 97 110 107 32 67 111 117 116 101 108
数字是组成每个字符串的字符的 Unicode 代码点,采用十进制格式。
或者,为了更好地可视化差异,使用来自 this Gist:
的辅助函数Debug-String
- 注意:以下代码段会自动下载并定义函数。我个人可以向你保证这样做是安全的,但你应该始终先自己检查源代码。
# Download and define helper function Debug-String
# * See comments above.
# * To see instructions on how to make the function available in
# *future* sessions, remove >3 $null and re-run this command directly
# from the command line.
irm https://gist.github.com/mklement0/7f2f1e13ac9c2afaf0a0906d08b392d1/raw/Debug-String.ps1 | iex 3>$null
'Adam Warlock',
'Adam Warlock ', # trailing space
'Frank Coutel',
('Frank{0}Coutel' -f [char] 0xa0) | # no-break space instead of regular space
Debug-String |
Sort-Object -Unique
输出:
Adam·Warlock
Adam·Warlock· # Note the trailing ·
Frank·Coutel
Frank`u{a0}Coutel # Note the `u{a0} escape sequence
Debug-String
将常规空格可视化为 ·
,将 ASCII 范围外的 formatting/control 字符可视化为 Unicode 转义序列,例如`u{a0}
,其中 a0
是字符的 Unicode 代码点的十六进制形式,即 no-break space ( U+00A0
) 字符,在这种情况下。