本文介绍了如何根据powershell中对象的两个属性选择唯一对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一组具有 6 个属性的对象.看起来像这样:

I have an array of Objects that have 6 properties. That look like this:

$csvData
CURRENT DATE AND TIME : 07/10/2015 08:17:17 CST
USER NAME             : userName
COMPUTER NAME         : computerName
IP ADDRESS            : 192.168.1.1
LOGON SERVER          : logonServer
LOGON/OFF             : logon

我想创建一个不重复用户名和计算机名称的对象数组.如何在powershell中仅获得唯一的用户名/计算机名组合?最终,我想删除所有重复项并添加一个属性计数"来跟踪有多少重复项.

I want to create an array of objects where username and computer name are not duplicated. How can I get only the unique username/computername combo in powershell? Ultimately I would like to remove all duplicates and add a property 'Count' that keeps track of how many duplicates there are.

我试过了:

$csvDataUnique = $csvData | Select-Object 'User Name','Computer Name' -Unique
$csvDataUnique = $csvData | sort -Property 'User Name' | Get-Unique

推荐答案

与 Mathias 的回答非常相似,但将在输出中包含所有列:

Very similar to Mathias' answer, but will have all of the columns in the output:

$csvDataUnique = $csvData | 
  Group-Object 'User Name','Computer Name' | 
  %{ $_.Group | Select 'User Name','Computer Name' -First 1} | 
  Sort 'User Name','Computer Name' 

这篇关于如何根据powershell中对象的两个属性选择唯一对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 04:49