使用powershell比较文件中的当前日期字符串
我正在编写一些PS脚本,使用以下代码将时间记录到文本文件login.txt中:使用powershell比较文件中的当前日期字符串,powershell,getdate,Powershell,Getdate,我正在编写一些PS脚本,使用以下代码将时间记录到文本文件login.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 $logfil
$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天,则将用户名提取到一个变量,以便稍后在脚本中使用
如果您能给我一些提示,告诉我如何做到以下几点,我将不胜感激:
如果您能给我一些建议,我将不胜感激。有一种方法可以使用(正则表达式)实现这一点。我将假定您在文本文件中得到的
用户名
是分开的(点)
。例如,用户名看起来像john.doe
或jason.smith
等,文本文件中的条目看起来像john.doe-01-01-1999
或jason.smith-02-02-1999
。记住这些,我们的方法是:
用户名
和日期条目
放入单个变量中用户名
部分和日期
部分username
)并将其存储在变量中$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模式将根据您定义的用户名格式进行更改。是一个
regex库
,可能会有帮助。有一种方法可以使用(正则表达式)实现这一点。我将假定您在文本文件中得到的用户名
是分开的(点)
。例如,用户名看起来像john.doe
或jason.smith
等,文本文件中的条目看起来像john.doe-01-01-1999
或jason.smith-02-02-1999
。记住这些,我们的方法是:
用户名
和日期条目
放入单个变量中用户名
部分和日期
部分username
)并将其存储在变量中$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模式将根据您定义的用户名格式进行更改。是一个
正则表达式库
,可能会有帮助。在带有命名捕获组的正则表达式的帮助下检查文件中的所有日期
$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
}
}
}
在带有命名捕获组的正则表达式的帮助下检查文件中的所有日期
$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
}
}
}
可以减去日期:
$diff=$($(获取日期)-$Date\u文件)。Days
获取从现在起到包含日期的变量$Date\u文件
。可以减去日期:$diff=$($(获取日期)-$Date\u文件).Days
获取从现在到名为$date\u file
包含日期的变量之间的天数。哇!我不知道你能说出你的抓捕小组的名字!太棒了@heedfullcrayon您可以阅读更多关于OMG主题的内容。非常感谢你。这很好用。我可以将用户名拉入一个变量,然后继续脚本的其余部分。。非常感谢你。这些正则表达式看起来很难理解。为了获得使用正则表达式的经验,我可以推荐该网站,它对每一步都进行了详尽的解释。顺便说一句,最好的坦克是一个和/或一个投票站。@LotPings-这确实是非常有用的。比我试过的干净多了。谢谢你的专业提示!:)哇!我不知道你能说出你的抓捕小组的名字!太棒了@heedfullcrayon您可以阅读更多关于OMG主题的内容。非常感谢你。这很好用。我可以将用户名拉入一个变量,然后继续脚本的其余部分。。非常感谢你。这些正则表达式看起来很难理解。为了获得使用正则表达式的经验,我可以推荐该网站,它对每一步都进行了详尽的解释。顺便说一句,最好的坦克是一个和/或一个投票站。@LotPings-这确实是非常有用的。比我试过的干净多了。谢谢你的专业提示!:)