Powershell 从文本文件中获取小于日期/字符串的日期

Powershell 从文本文件中获取小于日期/字符串的日期,powershell,Powershell,我已经编写了一些代码,将网站上有关许可证的保修信息解析为.txt文件。我的目标(和问题)是将.txt文件中的保修到期日期与当前日期进行比较,以了解保修是否已到期 我是一名powershell初学者,因此我的代码可能不太符合逻辑,但到目前为止,我的代码是: #the Invoke-WebRequest is up here which outputs into $output_file #files $output_file = ‘c:\warrantyinfo.txt’ $warranty_f

我已经编写了一些代码,将网站上有关许可证的保修信息解析为.txt文件。我的目标(和问题)是将.txt文件中的保修到期日期与当前日期进行比较,以了解保修是否已到期

我是一名powershell初学者,因此我的代码可能不太符合逻辑,但到目前为止,我的代码是:

#the Invoke-WebRequest is up here which outputs into $output_file

#files
$output_file = ‘c:\warrantyinfo.txt’
$warranty_file = 'c:\warrantydate.txt'

#At this point all the warranty information is in $output_file
#With the code below I do a select string to get the information I want and 
#get rid of everything else
$warrantyUntil = Get-Content $output_file
$warrantyUntil | Select-String '("toCustomerDate":)["]\d{4}\-(0?[1- 
9]|1[012])\-(0?[1-9]|[12][0-9]|3[01])*' -AllMatches | Foreach-Object 
{$_.Matches} | Select value > $warranty_file

#I now have "toCustomerDate":"yyyy-mm-dd in a new .txt file 
#Below I try to grab just the date in the format yyyy-mm-dd in order to 
#compare with todays date. I think this is where I go wrong.
$warrantyDate=Select-String -Path $warranty_file -Pattern "\d{4}\-(0?[1- 
9]|1[012])\-(0?[1-9]|[12][0-9]|3[01])*" -AllMatches | Foreach-Object 
{$_.Matches} | Select value

#Current date in the format I want
$currentDate=Get-Date -UFormat "%Y-%m-%d"

#Compare warrantyDate and currentDate to decide if warranty has expired or 
#not
if ($currentDate -lt $warrantyDate) {
Write-Host "Warranty is valid"
} else {
Write-Host "Warranty has expired"
}

这是由于数据类型不匹配造成的


首先,您的
$currentDate
是字符串类型,而您可能希望它是
datetime
类型,这是因为执行了特定的格式设置。另外,
$carryDate
是字符串,甚至是字符串数组。你也需要记住这一点

您可能希望通过在变量名之前放置适当的类加速度来明确地设置初始化变量的数据类型。像这样:

[DateTime]$currentDate=Get-Date -UFormat "%Y-%m-%d"
[DateTime]$warrantyDate=Select-String -Path $warranty_file -Pattern "\d{4}\-(0?[1- 
9]|1[012])\-(0?[1-9]|[12][0-9]|3[01])*" -AllMatches | Foreach-Object 
{$_.Matches} | Select value
要查看什么是变量类型,请使用以下命令:

$currentDate.GetType().Name

使用文件
保修信息.txt

"toCustomerDate":"2018-04-22"
"toCustomerDate":"2018-04-24"
这个脚本:

$currentDate=Get-Date

$output_file = ‘.\warrantyinfo.txt’

$warrantyUntil = Get-Content $output_file | 
  Select-String '(?<="toCustomerDate":)"\d{4}\-(0?[1-9]|1[012])\-(0?[1-9]|[12][0-9]|3[01])"' -AllMatches

$warrantyUntil
$warrantyUntil.matches.value

$warrantyUntil.matches.value | ForEach {
    if ($currentDate -lt [datetime]$_.trim('"')) {
        Write-Host "Warranty $_ is valid"
    } else {
        Write-Host "Warranty $_ has expired"
    }
}

脚本使用了另一个带有for
的正则表达式(问题是您正在比较两个字符串,并希望进行日期比较:
$currentDate-lt$warrantyDate
。将
$currentDate
$warrantyDate
转换为
日期时间
对象(即使用
获取日期
),然后比较。我的答案可能会对您有所帮助。我相信$currentDate已经是DateTime对象了,因为我使用了Get Date?我认为问题与$QuartyDate有关,我必须研究如何将其转换为DateTime对象。谢谢
$currentDate
是一个字符串,因为您使用了
-UFormat
。请检查正式(
Output
部分)或运行
Get Date-UFormat“%Y-%m-%d”| Get Member
并查看输出-一个字符串。您真正需要的是
$currentDate=GetDate
当添加[DateTime]以保证日期时,它试图转换“{Value=2020-03-31}”,返回错误:"无法将所选类型的..System.Text.RegularExpressions.Match转换为System类型。DateTime@Vik
Select-value
->
Select-ExpandProperty-value
谢谢!这样做并添加-ExpandProperty-value后,一切都如期进行了测试作为替代方案,但我不明白您在中比较$currentDate的是什么这一行:如果($currentDate-lt[datetime]$\u0.trim('“')){?它会给我一个空值表达式这里
$warturyuntil.matches.value
附加了双引号,则.trim会删除它们。
"toCustomerDate":"2018-04-22"
"toCustomerDate":"2018-04-24"
"2018-04-22"
"2018-04-24"
Warranty "2018-04-22" has expired
Warranty "2018-04-24" is valid