代码之家  ›  专栏  ›  技术社区  ›  Kevin Ray

使用powershell比较文件中的当前日期与日期字符串

  •  0
  • Kevin Ray  · 技术社区  · 8 年前

    我正在编写一些PS脚本,将时间记录到一个文本文件“登录”中。txt,使用以下代码:

    $logdir = "C:\FOLDER"
    $logfile = "$logdir\LastLogin.txt"
    $user = $env:USERNAME
    $date = Get-Date -Format "dd-MM-yyyy"
    if (!(Test-Path $logdir)){New-Item -ItemType Directory $logdir}else{}
    if (!(Test-Path $logfile)){New-Item $logfile}else{}
    if (Get-Content $logfile | Select-String $user -Quiet){write-host "exists"}else{"$user - $date" | Add-Content -path $logfile}
    (Get-Content $logfile) | Foreach-Object {$_ -replace "$user.+$", "$user - $date"; } | Set-Content $logfile
    

    这将在文本文件中创建一个条目,如:

    用户名-01-01-1999

    我想使用Powershell读取文本文件,将文本文件中的日期01-01-1999与当前日期进行比较,如果相差超过30天,则将用户名提取到一个变量中,以便稍后在脚本中使用。

    如果您能给我一些提示,告诉我如何做到以下几点,我将不胜感激:

    1. 将文本文件中的日期与当前日期进行比较。
    2. 如果差异超过30天,则选择用户名作为变量。

    如果您能给我提些建议,我将不胜感激。

    2 回复  |  直到 8 年前
        1
  •  2
  •   user6811411 user6811411    8 年前

    在带有命名捕获组的正则表达式的帮助下检查文件中的所有日期。

    $logdir = "C:\FOLDER"
    $logfile = Join-Path $logdir "LastLogin.txt"
    $Days = -30
    $Expires = (Get-Date).AddDays($Days)
    
    Get-Content $logfile | ForEach-Object {
      if ($_ -match "(?<User>[^ ]+) - (?<LastLogin>[0-9\-]+)") {
        $LastLogin = [datetime]::ParseExact($Matches.LastLogin,"dd-MM-yyyy",$Null)
        if ( $Expires -gt $LastLogin ) {
          "{0} last login {1} is {2:0} days ago" -F $Matches.User, $Matches.LastLogin,
             (New-TimeSpan -Start $LastLogin -End (Get-Date) ).TotalDays
        }
      }
    }
    

    样本输出

    username last login 31-12-1999 is 6690 days ago
    
        2
  •  0
  •   Vivek Kumar Singh    8 年前

    有一种方法可以使用 regex (正则表达式)。我假设 username 你在文本文件中得到的是 .(dot) 分开的。例如,用户名如下所示 john.doe jason.smith 文本文件中的条目如下所示 john.doe - 01-01-1999 jason.smith - 02-02-1999 .记住这些,我们的方法是-

    1. 使用正则表达式,我们可以得到 用户名 date entry 转换为单个变量。
    2. 接下来,我们将把步骤1中得到的模式分为两部分,即 用户名 零件和 date 部分
    3. 接下来,我们选择日期部分,如果差值超过30天,我们将选择另一部分( 用户名 )并将其存储在变量中。

    所以代码看起来像这样-

    $arr = @() #defining an array to store the username with date
    $pattern = "[a-z]*[.][a-z]*\s[-]\s[\d]{2}[-][\d]{2}[-][\d]{4}" #Regex pattern to match entires like "john.doe - 01-01-1999"
    
    Get-Content $logfile | Foreach {if ([Regex]::IsMatch($_, $pattern)) {
               $arr += [Regex]::Match($_, $pattern)
                }
            }
    $arr | Foreach {$_.Value} #Storing the matched pattern in $arr
    
    
    $UserNamewithDate = $arr.value -split ('\s[-]\s') #step 2 - Storing the username and date into a variable.
    
    $array = @() #Defining the array that would store the final usernames based on the time difference.
    
    for($i = 1; $i -lt $UserNamewithDate.Length;)
    {
        $datepart = [Datetime]$UserNamewithDate[$i] #Casting the date part to [datetime] format
        $CurrentDate = Get-Date
        $diff = $CurrentDate - $datepart
        if ($diff.Days -gt 30)
        {
            $array += $UserNamewithDate[$i -1] #If the difference between current date and the date received from the log is greater than 30 days, then store the corresponding username in $array
        }
        $i = $i + 2
    }
    

    现在您可以访问以下用户名 $array[0] ,则, $array[1] 等等希望有帮助!

    注意-regex模式将根据定义的用户名格式进行更改。 Here 是一个 regex library 这可能会有帮助。