每个日期在一列中计算唯一字符串

时间:2016-11-02 04:04:20

标签: powershell

首先,我的数据看起来像这样(添加了更多行以便澄清)

Time, Type, Text
11/1/2016 10:52, LOGIN, User domain\userID1 has logged in
11/1/2016 10:53, LOGIN, User domain\userID2 has logged in
11/1/2016 10:53, AGENT, Agent has restarted (or whatever, not important)
10/31/2016 07:12, LOGIN, User domain\userID2 has logged in
10/30/2016 07:12, LOGIN, User domain\userID2 has logged in
10/30/2016 06:21, LOGIN, User domain\userID2 has logged in
10/30/2016 05:14, LOGIN, User domain\userID2 has logged in

我正在尝试创建一个powershell脚本,该脚本将列出每天唯一登录的数量。我想看看:

11/1/2016, 2
10/31/2016, 1
10/30/2016, 1

我现在的代码..

$ListOfDates = Get-Content 'C:\path\data.csv'
$ArrayOfDates =@()

ForEach ($LogEntry in $ListOfDates) {
 $DateSplit = $LogEntry -split '[\\ , ]'
   If (-not ($ArrayOfDates -contains $DateSplit[0])) {
      $ArrayOfDates += $DateSplit[0]
 }
}

$string = Get-Content 'c:\path\data.csv' |
Where-Object {$_.'Time' -contains $ArrayOfDates[1]} |
Sort-Object -Property 'Time'

$ ArrayOfDates确实返回各个日期,因此似乎工作正常。我只是不确定如何使用Date的变量进行检查,然后计算该特定日期的唯一条目。

任何帮助表示感谢。

感谢。

3 个答案:

答案 0 :(得分:0)

Import-Csv data.csv | Where-Object {$_.Type -match 'LOGIN'} | 
                      Group-Object -Property { $_.Time.Split()[0] } | 
                      Select-Object Name, Count

如果您在交互式工作提示符处键入内容,则可以使用较短版本:

ipcsv data.csv |? Type -match 'login' | group {$_.Time.Split()[0]} -NoElement

要评论您的原始脚本,如果没有哈希表的概念将每个日期与该日期的计数相关联,那么它将无法轻松工作。只使用数组会非常繁琐。

编辑:是的,这只能获得每天的总登录次数,而不是唯一的登录次数。这是一个获得独特登录的版本:

Import-Csv data.csv |
       # Filter only the LOGIN entries, ignore 'AGENT' and so on
       Where-Object {$_.Type -match 'LOGIN'} | 

       # Group the lines by date (day only, ignoring the time)
       Group-Object -Property { $_.Time.Split()[0] } | 

       # For the results, each group is named for whatever you were grouping by
                        # so the group name is the date value 
       Select-Object @{
                        Name='Date'
                        Expression={$_.Name}
                      },

                        # to get unique logins, take all the things in each group
                        # Group those by a property which is calculated to get 'domain\userID1'
                        # and count how many of those groups there are
                     @{
                        Name='Unique Login Count'
                        Expression={
                            ($_.Group | Group-Object -Property { $_.Text.Split()[1] }).Count}
                        }

其中:

  • Import-Csv为Import-Csv(在模块Microsoft.PowerShell.Utility中)
  • Where-Object为Where-Object
  • Group-Object为Group-Object(在模块Microsoft.PowerShell.Utility中)
  • Select是Select-Object的别名(在模块Microsoft.PowerShell.Utility中)

答案 1 :(得分:0)

  import-csv C:\temp\test.csv | where type -EQ "LOGIN" | select *, @{Name="DateOnly";Expression={([datetime]$_.Time).ToShortDateString() }} | group DateOnly

答案 2 :(得分:0)

因此,经过几个小时的努力解决问题,并从我的同事以及我的同事那里得到帮助。这是我最终的脚本,供参考。

它比问题中提到的要多一点,但它确实完成了手头的任务。

希望它帮助其他人做出类似的事情。

ReadKey
相关问题